blob: 23781a2e4aa47a73801d5a166de53e022c9daf66 [file] [log] [blame]
simond47ef9e2022-09-28 22:24:28 -04001'use strict';
Adrien Béraude74741b2021-04-19 13:22:54 -04002
simon07b4eb02022-09-29 17:50:26 -04003import CopyWebpackPlugin from 'copy-webpack-plugin';
simond47ef9e2022-09-28 22:24:28 -04004import dotenv from 'dotenv';
5import HtmlWebpackPlugin from 'html-webpack-plugin';
simon07b4eb02022-09-29 17:50:26 -04006import { dirname, resolve } from 'path';
simond47ef9e2022-09-28 22:24:28 -04007import { fileURLToPath } from 'url';
simonc7d52452022-09-23 02:09:42 -04008
Adrien Béraude74741b2021-04-19 13:22:54 -04009const __filename = fileURLToPath(import.meta.url);
10const __dirname = dirname(__filename);
Adrien Béraud4e287b92021-04-24 16:15:56 -040011
simond47ef9e2022-09-28 22:24:28 -040012dotenv.config({ path: resolve(__dirname, '..', '.env') });
Adrien Béraud4e287b92021-04-24 16:15:56 -040013
simond47ef9e2022-09-28 22:24:28 -040014const mode = process.env.NODE_ENV || 'development';
Adrien Béraud824a7132021-04-17 17:25:27 -040015
simon218d3d12022-10-01 17:27:01 -040016let entry = [resolve(__dirname, 'src', 'index.tsx')];
Adrien Béraud150b4782021-04-21 19:40:59 -040017let plugins = [
simonc7d52452022-09-23 02:09:42 -040018 new HtmlWebpackPlugin({
19 template: '!!raw-loader!' + resolve(__dirname, 'src', 'index.ejs'),
simond47ef9e2022-09-28 22:24:28 -040020 filename: 'index.ejs',
simonc7d52452022-09-23 02:09:42 -040021 }),
Adrien Béraud88a52442021-04-26 12:11:41 -040022 new CopyWebpackPlugin({
simond47ef9e2022-09-28 22:24:28 -040023 patterns: [
24 {
25 from: resolve(__dirname, 'public'),
26 to: resolve(__dirname, 'dist'),
27 },
28 ],
29 }),
30];
simonc7d52452022-09-23 02:09:42 -040031
simond47ef9e2022-09-28 22:24:28 -040032let devtool = undefined;
33let babelLoaderPlugins = ['@babel/plugin-transform-runtime'];
Adrien Béraud824a7132021-04-17 17:25:27 -040034
35if (mode === 'development') {
simond47ef9e2022-09-28 22:24:28 -040036 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éraud824a7132021-04-17 17:25:27 -040042}
simond47ef9e2022-09-28 22:24:28 -040043console.log(`Webpack configured for ${mode}`);
Adrien Béraudc4dd44a2021-04-08 01:05:24 -040044
Adrien Béraude74741b2021-04-19 13:22:54 -040045export default {
Adrien Béraud88a52442021-04-26 12:11:41 -040046 entry,
47 output: {
simond47ef9e2022-09-28 22:24:28 -040048 path: resolve(__dirname, 'dist'),
49 filename: 'bundle.js',
50 publicPath: '/',
Adrien Béraud88a52442021-04-26 12:11:41 -040051 },
52 devtool,
53 mode,
54 module: {
55 rules: [
56 {
simonc7d52452022-09-23 02:09:42 -040057 test: /\.[tj]sx?$/,
Adrien Béraud88a52442021-04-26 12:11:41 -040058 resolve: {
idillond858c182022-09-16 13:18:26 -040059 fullySpecified: false,
simonc7d52452022-09-23 02:09:42 -040060 extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
Adrien Béraud88a52442021-04-26 12:11:41 -040061 },
62 exclude: /node_modules/,
63 use: {
simond47ef9e2022-09-28 22:24:28 -040064 loader: 'babel-loader',
Adrien Béraud88a52442021-04-26 12:11:41 -040065 options: {
66 plugins: babelLoaderPlugins,
67 presets: [
idillond858c182022-09-16 13:18:26 -040068 [
simond47ef9e2022-09-28 22:24:28 -040069 '@babel/preset-env',
idillond858c182022-09-16 13:18:26 -040070 {
simond47ef9e2022-09-28 22:24:28 -040071 useBuiltIns: 'entry',
72 corejs: { version: '3.10', proposals: true },
idillond858c182022-09-16 13:18:26 -040073 },
74 ],
75 [
simond47ef9e2022-09-28 22:24:28 -040076 '@babel/preset-react',
idillond858c182022-09-16 13:18:26 -040077 {
simond47ef9e2022-09-28 22:24:28 -040078 runtime: 'automatic',
idillond858c182022-09-16 13:18:26 -040079 },
80 ],
simond47ef9e2022-09-28 22:24:28 -040081 ['@babel/preset-typescript'],
idillond858c182022-09-16 13:18:26 -040082 ],
83 },
84 },
Adrien Béraud88a52442021-04-26 12:11:41 -040085 },
86 {
87 test: /\.s[ac]ss$/i,
simond47ef9e2022-09-28 22:24:28 -040088 use: ['style-loader', 'css-loader', 'sass-loader'],
Adrien Béraud88a52442021-04-26 12:11:41 -040089 },
90 {
91 test: /\.svg$/,
simond47ef9e2022-09-28 22:24:28 -040092 use: ['@svgr/webpack'],
idillond858c182022-09-16 13:18:26 -040093 },
94 {
95 // test: /\.tsx?$/,
96 test: /\.(js|jsx|ts|tsx)?$/,
97 exclude: /node_modules/,
simond47ef9e2022-09-28 22:24:28 -040098 loader: 'ts-loader',
idillond858c182022-09-16 13:18:26 -040099 },
100 ],
Adrien Béraud88a52442021-04-26 12:11:41 -0400101 },
idillond858c182022-09-16 13:18:26 -0400102 resolve: {
simond47ef9e2022-09-28 22:24:28 -0400103 modules: ['node_modules', resolve(__dirname)],
104 extensions: ['.ts', '.tsx', '.js', '.jsx'],
idillond858c182022-09-16 13:18:26 -0400105 },
106 plugins,
simonc7d52452022-09-23 02:09:42 -0400107};