simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 1 | 'use strict'; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 2 | |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 3 | import CopyWebpackPlugin from 'copy-webpack-plugin'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 4 | import dotenv from 'dotenv'; |
| 5 | import HtmlWebpackPlugin from 'html-webpack-plugin'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 6 | import { dirname, resolve } from 'path'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 7 | import { fileURLToPath } from 'url'; |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 8 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 9 | const __filename = fileURLToPath(import.meta.url); |
| 10 | const __dirname = dirname(__filename); |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 11 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 12 | dotenv.config({ path: resolve(__dirname, '..', '.env') }); |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 13 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 14 | const mode = process.env.NODE_ENV || 'development'; |
Adrien Béraud | 824a713 | 2021-04-17 17:25:27 -0400 | [diff] [blame] | 15 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 16 | let entry = [resolve(__dirname, 'src', 'index.tsx')]; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 17 | let plugins = [ |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 18 | new HtmlWebpackPlugin({ |
| 19 | template: '!!raw-loader!' + resolve(__dirname, 'src', 'index.ejs'), |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 20 | filename: 'index.ejs', |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 21 | }), |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 22 | new CopyWebpackPlugin({ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 23 | patterns: [ |
| 24 | { |
| 25 | from: resolve(__dirname, 'public'), |
| 26 | to: resolve(__dirname, 'dist'), |
| 27 | }, |
| 28 | ], |
| 29 | }), |
| 30 | ]; |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 31 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 32 | let devtool = undefined; |
| 33 | let babelLoaderPlugins = ['@babel/plugin-transform-runtime']; |
Adrien Béraud | 824a713 | 2021-04-17 17:25:27 -0400 | [diff] [blame] | 34 | |
| 35 | if (mode === 'development') { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 36 | const webpack = (await import('webpack')).default; |
| 37 | const ReactRefreshWebpackPlugin = (await import('@pmmmwh/react-refresh-webpack-plugin')).default; |
| 38 | entry = ['webpack-hot-middleware/client', ...entry]; |
| 39 | plugins = [new webpack.HotModuleReplacementPlugin(), new ReactRefreshWebpackPlugin(), ...plugins]; |
| 40 | babelLoaderPlugins = [...babelLoaderPlugins, 'react-refresh/babel']; |
| 41 | devtool = 'inline-source-map'; |
Adrien Béraud | 824a713 | 2021-04-17 17:25:27 -0400 | [diff] [blame] | 42 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 43 | console.log(`Webpack configured for ${mode}`); |
Adrien Béraud | c4dd44a | 2021-04-08 01:05:24 -0400 | [diff] [blame] | 44 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 45 | export default { |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 46 | entry, |
| 47 | output: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 48 | path: resolve(__dirname, 'dist'), |
| 49 | filename: 'bundle.js', |
| 50 | publicPath: '/', |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 51 | }, |
| 52 | devtool, |
| 53 | mode, |
| 54 | module: { |
| 55 | rules: [ |
| 56 | { |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 57 | test: /\.[tj]sx?$/, |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 58 | resolve: { |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 59 | fullySpecified: false, |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 60 | extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'], |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 61 | }, |
| 62 | exclude: /node_modules/, |
| 63 | use: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 64 | loader: 'babel-loader', |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 65 | options: { |
| 66 | plugins: babelLoaderPlugins, |
| 67 | presets: [ |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 68 | [ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 69 | '@babel/preset-env', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 70 | { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 71 | useBuiltIns: 'entry', |
| 72 | corejs: { version: '3.10', proposals: true }, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 73 | }, |
| 74 | ], |
| 75 | [ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 76 | '@babel/preset-react', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 77 | { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 78 | runtime: 'automatic', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 79 | }, |
| 80 | ], |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 81 | ['@babel/preset-typescript'], |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 82 | ], |
| 83 | }, |
| 84 | }, |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 85 | }, |
| 86 | { |
| 87 | test: /\.s[ac]ss$/i, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 88 | use: ['style-loader', 'css-loader', 'sass-loader'], |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 89 | }, |
| 90 | { |
| 91 | test: /\.svg$/, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 92 | use: ['@svgr/webpack'], |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 93 | }, |
| 94 | { |
| 95 | // test: /\.tsx?$/, |
| 96 | test: /\.(js|jsx|ts|tsx)?$/, |
| 97 | exclude: /node_modules/, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 98 | loader: 'ts-loader', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 99 | }, |
| 100 | ], |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 101 | }, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 102 | resolve: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 103 | modules: ['node_modules', resolve(__dirname)], |
| 104 | extensions: ['.ts', '.tsx', '.js', '.jsx'], |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 105 | }, |
| 106 | plugins, |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 107 | }; |