simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 18 | 'use strict'; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 19 | |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 20 | import CopyWebpackPlugin from 'copy-webpack-plugin'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 21 | import dotenv from 'dotenv'; |
| 22 | import HtmlWebpackPlugin from 'html-webpack-plugin'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | import { dirname, resolve } from 'path'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 24 | import { fileURLToPath } from 'url'; |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 25 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 26 | const __filename = fileURLToPath(import.meta.url); |
| 27 | const __dirname = dirname(__filename); |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 28 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 29 | dotenv.config({ path: resolve(__dirname, '..', '.env') }); |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 30 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 31 | const mode = process.env.NODE_ENV || 'development'; |
Adrien Béraud | 824a713 | 2021-04-17 17:25:27 -0400 | [diff] [blame] | 32 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 33 | let entry = [resolve(__dirname, 'src', 'index.tsx')]; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 34 | let plugins = [ |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 35 | new HtmlWebpackPlugin({ |
| 36 | template: '!!raw-loader!' + resolve(__dirname, 'src', 'index.ejs'), |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 37 | filename: 'index.ejs', |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 38 | }), |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 39 | new CopyWebpackPlugin({ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 40 | patterns: [ |
| 41 | { |
| 42 | from: resolve(__dirname, 'public'), |
| 43 | to: resolve(__dirname, 'dist'), |
| 44 | }, |
| 45 | ], |
| 46 | }), |
| 47 | ]; |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 48 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 49 | let devtool = undefined; |
| 50 | let babelLoaderPlugins = ['@babel/plugin-transform-runtime']; |
Adrien Béraud | 824a713 | 2021-04-17 17:25:27 -0400 | [diff] [blame] | 51 | |
| 52 | if (mode === 'development') { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 53 | const webpack = (await import('webpack')).default; |
| 54 | const ReactRefreshWebpackPlugin = (await import('@pmmmwh/react-refresh-webpack-plugin')).default; |
| 55 | entry = ['webpack-hot-middleware/client', ...entry]; |
| 56 | plugins = [new webpack.HotModuleReplacementPlugin(), new ReactRefreshWebpackPlugin(), ...plugins]; |
| 57 | babelLoaderPlugins = [...babelLoaderPlugins, 'react-refresh/babel']; |
| 58 | devtool = 'inline-source-map'; |
Adrien Béraud | 824a713 | 2021-04-17 17:25:27 -0400 | [diff] [blame] | 59 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 60 | console.log(`Webpack configured for ${mode}`); |
Adrien Béraud | c4dd44a | 2021-04-08 01:05:24 -0400 | [diff] [blame] | 61 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 62 | export default { |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 63 | entry, |
| 64 | output: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 65 | path: resolve(__dirname, 'dist'), |
| 66 | filename: 'bundle.js', |
| 67 | publicPath: '/', |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 68 | }, |
| 69 | devtool, |
| 70 | mode, |
| 71 | module: { |
| 72 | rules: [ |
| 73 | { |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 74 | test: /\.[tj]sx?$/, |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 75 | resolve: { |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 76 | fullySpecified: false, |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 77 | extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'], |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 78 | }, |
| 79 | exclude: /node_modules/, |
| 80 | use: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 81 | loader: 'babel-loader', |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 82 | options: { |
| 83 | plugins: babelLoaderPlugins, |
| 84 | presets: [ |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 85 | [ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 86 | '@babel/preset-env', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 87 | { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 88 | useBuiltIns: 'entry', |
| 89 | corejs: { version: '3.10', proposals: true }, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 90 | }, |
| 91 | ], |
| 92 | [ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 93 | '@babel/preset-react', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 94 | { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 95 | runtime: 'automatic', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 96 | }, |
| 97 | ], |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 98 | ['@babel/preset-typescript'], |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 99 | ], |
| 100 | }, |
| 101 | }, |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 102 | }, |
| 103 | { |
| 104 | test: /\.s[ac]ss$/i, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 105 | use: ['style-loader', 'css-loader', 'sass-loader'], |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 106 | }, |
| 107 | { |
| 108 | test: /\.svg$/, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 109 | use: ['@svgr/webpack'], |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 110 | }, |
| 111 | { |
| 112 | // test: /\.tsx?$/, |
| 113 | test: /\.(js|jsx|ts|tsx)?$/, |
| 114 | exclude: /node_modules/, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 115 | loader: 'ts-loader', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 116 | }, |
| 117 | ], |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 118 | }, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 119 | resolve: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 120 | modules: ['node_modules', resolve(__dirname)], |
| 121 | extensions: ['.ts', '.tsx', '.js', '.jsx'], |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 122 | }, |
| 123 | plugins, |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 124 | }; |