blob: cf620bc66e85075e92f3448f9d75602a55e4911f [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 */
simond47ef9e2022-09-28 22:24:28 -040018'use strict';
Adrien Béraude74741b2021-04-19 13:22:54 -040019
simon07b4eb02022-09-29 17:50:26 -040020import CopyWebpackPlugin from 'copy-webpack-plugin';
simond47ef9e2022-09-28 22:24:28 -040021import dotenv from 'dotenv';
22import HtmlWebpackPlugin from 'html-webpack-plugin';
simon07b4eb02022-09-29 17:50:26 -040023import { dirname, resolve } from 'path';
simond47ef9e2022-09-28 22:24:28 -040024import { fileURLToPath } from 'url';
simonc7d52452022-09-23 02:09:42 -040025
Adrien Béraude74741b2021-04-19 13:22:54 -040026const __filename = fileURLToPath(import.meta.url);
27const __dirname = dirname(__filename);
Adrien Béraud4e287b92021-04-24 16:15:56 -040028
simond47ef9e2022-09-28 22:24:28 -040029dotenv.config({ path: resolve(__dirname, '..', '.env') });
Adrien Béraud4e287b92021-04-24 16:15:56 -040030
simond47ef9e2022-09-28 22:24:28 -040031const mode = process.env.NODE_ENV || 'development';
Adrien Béraud824a7132021-04-17 17:25:27 -040032
simon218d3d12022-10-01 17:27:01 -040033let entry = [resolve(__dirname, 'src', 'index.tsx')];
Adrien Béraud150b4782021-04-21 19:40:59 -040034let plugins = [
simonc7d52452022-09-23 02:09:42 -040035 new HtmlWebpackPlugin({
36 template: '!!raw-loader!' + resolve(__dirname, 'src', 'index.ejs'),
simond47ef9e2022-09-28 22:24:28 -040037 filename: 'index.ejs',
simonc7d52452022-09-23 02:09:42 -040038 }),
Adrien Béraud88a52442021-04-26 12:11:41 -040039 new CopyWebpackPlugin({
simond47ef9e2022-09-28 22:24:28 -040040 patterns: [
41 {
42 from: resolve(__dirname, 'public'),
43 to: resolve(__dirname, 'dist'),
44 },
45 ],
46 }),
47];
simonc7d52452022-09-23 02:09:42 -040048
simond47ef9e2022-09-28 22:24:28 -040049let devtool = undefined;
50let babelLoaderPlugins = ['@babel/plugin-transform-runtime'];
Adrien Béraud824a7132021-04-17 17:25:27 -040051
52if (mode === 'development') {
simond47ef9e2022-09-28 22:24:28 -040053 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éraud824a7132021-04-17 17:25:27 -040059}
simond47ef9e2022-09-28 22:24:28 -040060console.log(`Webpack configured for ${mode}`);
Adrien Béraudc4dd44a2021-04-08 01:05:24 -040061
Adrien Béraude74741b2021-04-19 13:22:54 -040062export default {
Adrien Béraud88a52442021-04-26 12:11:41 -040063 entry,
64 output: {
simond47ef9e2022-09-28 22:24:28 -040065 path: resolve(__dirname, 'dist'),
66 filename: 'bundle.js',
67 publicPath: '/',
Adrien Béraud88a52442021-04-26 12:11:41 -040068 },
69 devtool,
70 mode,
71 module: {
72 rules: [
73 {
simonc7d52452022-09-23 02:09:42 -040074 test: /\.[tj]sx?$/,
Adrien Béraud88a52442021-04-26 12:11:41 -040075 resolve: {
idillond858c182022-09-16 13:18:26 -040076 fullySpecified: false,
simonc7d52452022-09-23 02:09:42 -040077 extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
Adrien Béraud88a52442021-04-26 12:11:41 -040078 },
79 exclude: /node_modules/,
80 use: {
simond47ef9e2022-09-28 22:24:28 -040081 loader: 'babel-loader',
Adrien Béraud88a52442021-04-26 12:11:41 -040082 options: {
83 plugins: babelLoaderPlugins,
84 presets: [
idillond858c182022-09-16 13:18:26 -040085 [
simond47ef9e2022-09-28 22:24:28 -040086 '@babel/preset-env',
idillond858c182022-09-16 13:18:26 -040087 {
simond47ef9e2022-09-28 22:24:28 -040088 useBuiltIns: 'entry',
89 corejs: { version: '3.10', proposals: true },
idillond858c182022-09-16 13:18:26 -040090 },
91 ],
92 [
simond47ef9e2022-09-28 22:24:28 -040093 '@babel/preset-react',
idillond858c182022-09-16 13:18:26 -040094 {
simond47ef9e2022-09-28 22:24:28 -040095 runtime: 'automatic',
idillond858c182022-09-16 13:18:26 -040096 },
97 ],
simond47ef9e2022-09-28 22:24:28 -040098 ['@babel/preset-typescript'],
idillond858c182022-09-16 13:18:26 -040099 ],
100 },
101 },
Adrien Béraud88a52442021-04-26 12:11:41 -0400102 },
103 {
104 test: /\.s[ac]ss$/i,
simond47ef9e2022-09-28 22:24:28 -0400105 use: ['style-loader', 'css-loader', 'sass-loader'],
Adrien Béraud88a52442021-04-26 12:11:41 -0400106 },
107 {
108 test: /\.svg$/,
simond47ef9e2022-09-28 22:24:28 -0400109 use: ['@svgr/webpack'],
idillond858c182022-09-16 13:18:26 -0400110 },
111 {
112 // test: /\.tsx?$/,
113 test: /\.(js|jsx|ts|tsx)?$/,
114 exclude: /node_modules/,
simond47ef9e2022-09-28 22:24:28 -0400115 loader: 'ts-loader',
idillond858c182022-09-16 13:18:26 -0400116 },
117 ],
Adrien Béraud88a52442021-04-26 12:11:41 -0400118 },
idillond858c182022-09-16 13:18:26 -0400119 resolve: {
simond47ef9e2022-09-28 22:24:28 -0400120 modules: ['node_modules', resolve(__dirname)],
121 extensions: ['.ts', '.tsx', '.js', '.jsx'],
idillond858c182022-09-16 13:18:26 -0400122 },
123 plugins,
simonc7d52452022-09-23 02:09:42 -0400124};