Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / drive / webpack.config.ts
blob171c2caac1b362371862e0a55281f314c1b62521
1 import HtmlWebpackPlugin from 'html-webpack-plugin';
2 import webpack from 'webpack';
3 import { WebpackManifestPlugin } from 'webpack-manifest-plugin';
5 import getConfig from '@proton/pack/webpack.config';
6 import { addDevEntry, getIndexChunks } from '@proton/pack/webpack/entries';
8 /**
9  * There are some specific references to Buffer in the drive application,
10  * e.g. MimeTypes so it has to be polyfilled
11  */
12 const result = (env: any): webpack.Configuration => {
13     const config = getConfig(env);
15     config.plugins = config.plugins || [];
17     const htmlPlugin = config.plugins.find((plugin): plugin is HtmlWebpackPlugin => {
18         return plugin instanceof HtmlWebpackPlugin;
19     });
20     if (!htmlPlugin) {
21         throw new Error('Missing html plugin');
22     }
23     const htmlIndex = config.plugins.indexOf(htmlPlugin);
25     config.plugins.splice(
26         htmlIndex,
27         1,
28         new HtmlWebpackPlugin({
29             filename: 'index.html',
30             template: 'ejs-webpack-loader!src/app.ejs',
31             templateParameters: htmlPlugin.userOptions.templateParameters,
32             scriptLoading: 'defer',
33             inject: 'body',
34             chunks: getIndexChunks('index'),
35         })
36     );
38     config.plugins.splice(
39         htmlIndex,
40         0,
41         new HtmlWebpackPlugin({
42             filename: 'urls.html',
43             template: `ejs-webpack-loader!src/urls.ejs`,
44             templateParameters: htmlPlugin.userOptions.templateParameters,
45             scriptLoading: 'defer',
46             inject: 'body',
47             chunks: getIndexChunks('index'),
48         })
49     );
51     if (env.appMode === 'standalone') {
52         addDevEntry(config);
53     }
55     config.plugins.push(
56         new WebpackManifestPlugin({
57             fileName: 'assets/offline.json',
58             filter: (file) => {
59                 /** exclude sourcemaps and certain assets */
60                 if (
61                     file.name.includes('.map') ||
62                     file.name.includes('date-fns') ||
63                     file.name.includes('locales') ||
64                     file.name.includes('downloadSW') ||
65                     file.name.includes('.json')
66                 ) {
67                     return false;
68                 }
69                 if (file.name.includes('.js') || file.name.includes('.css')) {
70                     return true;
71                 }
72                 return false;
73             },
74         })
75     );
77     return {
78         ...config,
79         resolve: {
80             ...config.resolve,
81             extensions: ['.js', '.tsx', '.ts', '...'], // ... is there to include default extensions for proper building of type script web workers.
82             fallback: {
83                 ...config.resolve?.fallback,
84                 buffer: require.resolve('buffer'),
85                 path: require.resolve('path-browserify'),
86             },
87         },
88         plugins: [
89             ...config.plugins,
90             new webpack.ProvidePlugin({
91                 Buffer: [require.resolve('buffer'), 'Buffer'],
92             }),
93         ],
94     };
97 export default result;