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