Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
80 lines (77 loc) · 2.78 KB
/
Copy pathwebpack.config.js
File metadata and controls
80 lines (77 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const fs = require('node:fs');
const path = require('node:path');
const browserModules = {
fs: () => {
const unavailable = () => {
throw new Error('Filesystem access is unavailable in the MJML browser compiler');
};
return { readFileSync: unavailable, existsSync: () => false, lstatSync: unavailable };
},
// Keep MJML's filesystem/configuration branches disabled, including in Node.
'detect-node': () => false,
// Match mjml-browser: preserve embedded JavaScript during HTML minification.
'uglify-js': () => ({ minify: (source) => ({ code: source }) }),
'./helpers/mjmlconfig': () => {
const emptyConfig = () => ({ mjmlConfig: { packages: [], options: {} } });
return {
default: emptyConfig,
readMjmlConfig: emptyConfig,
handleMjmlConfigComponents: () => ({ success: [], failures: [] }),
};
},
};
module.exports = ({ config, pkg, webpack }) => {
const TerserPlugin = require('terser-webpack-plugin');
config.resolve.alias = {
...config.resolve.alias,
path: require.resolve('path-browserify'),
};
config.resolve.fallback = {
...config.resolve.fallback,
buffer: require.resolve('buffer/'),
process: require.resolve('process/browser'),
os: false,
util: false,
stream: false,
http: false,
https: false,
url: require.resolve('url/'),
crypto: false,
};
config.plugins.push(
new webpack.BannerPlugin({
banner: fs.readFileSync(path.join(path.dirname(require.resolve('mjml-core/package.json')), 'LICENSE.md'), 'utf8'),
entryOnly: true,
}),
new webpack.ProvidePlugin({
process: require.resolve('process/browser'),
Buffer: [require.resolve('buffer/'), 'Buffer'],
}),
new webpack.NormalModuleReplacementPlugin(
/^(fs|detect-node|uglify-js|\.\/helpers\/mjmlconfig)$/,
(resource) => {
const factory = browserModules[resource.request];
// Webpack supports inline CommonJS modules through data URLs.
const source = `module.exports = (${factory.toString()})();`;
resource.request = `data:application/node;base64,${Buffer.from(source).toString('base64')}`;
},
),
);
// Node-only skeleton loading is disabled by the detect-node replacement.
config.ignoreWarnings = [
...(config.ignoreWarnings || []),
{ module: /mjml-core[\\/]lib[\\/]index\.js$/, message: /Critical dependency/ },
];
config.optimization.minimizer = [new TerserPlugin({
extractComments: true,
terserOptions: {
// MJML derives tag names from class names unless componentName is set.
keep_classnames: true,
keep_fnames: true,
compress: { evaluate: false },
format: { comments: false, preamble: `/*! ${pkg.name} - ${pkg.version} */` },
},
})];
return config;
};
module.exports.browserModules = browserModules;