Merge branch 'INDA-330-pii-update' into 'main'
[ProtonMail-WebClient.git] / applications / mail / webpack.config.ts
blob3eb4e48b5d5e01970c445429562f01ab87770973
1 import HtmlWebpackPlugin from 'html-webpack-plugin';
2 import { produce, setAutoFreeze } from 'immer';
3 import path from 'path';
4 import type { Configuration} from 'webpack';
5 import { ProvidePlugin } from 'webpack';
6 import { InjectManifest } from 'workbox-webpack-plugin';
8 import getConfig from '@proton/pack/webpack.config';
9 import { addDevEntry, getIndexChunks, getSupportedEntry, mergeEntry } from '@proton/pack/webpack/entries';
11 const result = (env: any): Configuration => {
12     setAutoFreeze(false);
14     return produce(getConfig(env), (config) => {
15         config.plugins = config.plugins || [];
16         config.resolve = config.resolve || {};
17         config.resolve.fallback = config.resolve.fallback || {};
19         // @ts-ignore
20         config.resolve.fallback.buffer = require.resolve('buffer');
21         config.plugins.push(
22             new ProvidePlugin({
23                 // It's required by the lib rfc2047 which is used by mimemessage.js
24                 // Without it any mimemessage with an attachment including special char will fail
25                 Buffer: [require.resolve('buffer'), 'Buffer'],
26             })
27         );
29         config.resolve.alias = {
30             'proton-mail': path.resolve(__dirname, 'src/app/'),
31             perf_hooks: path.resolve(__dirname, './perf_hooks_polyfill.ts'),
32         };
34         // if (config.mode !== 'development') {
35         config.plugins.push(
36             new InjectManifest({
37                 swSrc: './src/service-worker.js',
38                 swDest: 'service-worker.js',
39                 // Any other config if needed.
40                 maximumFileSizeToCacheInBytes: 10000000,
41             })
42         );
43         // }
45         // The order is important so that the unsupported file is loaded after
46         config.entry = mergeEntry(config.entry, {
47             ['eo-index']: [path.resolve('./src/app/eo.tsx'), getSupportedEntry()],
48         });
50         // @ts-ignore
51         config.devServer.historyApiFallback.rewrites = [{ from: /^\/eo/, to: '/eo.html' }];
53         const htmlPlugin = config.plugins.find((plugin): plugin is HtmlWebpackPlugin => {
54             return plugin instanceof HtmlWebpackPlugin;
55         });
56         if (!htmlPlugin) {
57             throw new Error('Missing html plugin');
58         }
59         const htmlIndex = config.plugins.indexOf(htmlPlugin);
61         if (env.appMode === 'standalone') {
62             addDevEntry(config);
63         }
65         // We keep the order because the other plugins have an impact
66         // Replace the old html webpackplugin with this
67         config.plugins.splice(
68             htmlIndex,
69             1,
70             new HtmlWebpackPlugin({
71                 filename: 'index.html',
72                 template: path.resolve('./src/app.ejs'),
73                 templateParameters: htmlPlugin.userOptions.templateParameters,
74                 scriptLoading: 'defer',
75                 chunks: getIndexChunks('index'),
76                 inject: 'body',
77             })
78         );
79         // Add another webpack plugin on top
80         config.plugins.splice(
81             htmlIndex,
82             0,
83             new HtmlWebpackPlugin({
84                 filename: 'eo.html',
85                 template: path.resolve('./src/eo.ejs'),
86                 templateParameters: htmlPlugin.userOptions.templateParameters,
87                 scriptLoading: 'defer',
88                 chunks: getIndexChunks('eo-index'),
89                 inject: 'body',
90             })
91         );
93         config.experiments = { asyncWebAssembly: true };
94     });
97 export default result;