-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebpack.config.js
More file actions
93 lines (88 loc) · 2.69 KB
/
webpack.config.js
File metadata and controls
93 lines (88 loc) · 2.69 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
81
82
83
84
85
86
87
88
89
90
91
92
93
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { updateContent } = require('./middleware.js');
const redirects = JSON.parse(fs.readFileSync('./redirects.json', 'utf8'));
module.exports = (mode, distPath) => {
const plugins = [
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new CopyPlugin({
patterns: [
"public",
],
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/html/portal.html',
inject: false,
})
];
// Object.keys(redirects).filter(sourceUrl => !sourceUrl.endsWith('*')).forEach((sourceUrl) => {
// plugins.push(new HtmlWebpackPlugin({
// filename: sourceUrl.replace(/^\//, '').replace(/\.html$/, '') + '.html',
// redirect: redirects[sourceUrl],
// template: 'src/html/redirect.html',
// inject: false,
// }));
// });
if (mode === 'development') {
plugins.push(new webpack.HotModuleReplacementPlugin());
// Custom plugin to apply updateContent to webpack-generated HTML in dev mode
plugins.push({
apply: (compiler) => {
compiler.hooks.compilation.tap('UpdateContentPlugin', (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
'UpdateContentPlugin',
(data, cb) => {
// Apply the same transformations as the middleware
data.html = updateContent(data.html, {
relativePath: data.outputName,
genSearchWidgetConfigId: process.env.GEN_SEARCH_WIDGET_ID || '',
gtmId: process.env.GTM_ID || '',
environment: mode
});
cb(null, data);
}
);
});
}
});
}
const srcPath = path.resolve(__dirname, 'src');
const entryPoints = {};
fs.readdirSync(path.resolve(srcPath, 'js')).forEach((fileName) => {
const match = fileName.match(/^(.*)\.js$/);
if (match) {
if (mode === 'development') {
entryPoints[match[1]] = ['webpack-hot-middleware/client', path.join('js', fileName)];
} else {
entryPoints[match[1]] = path.join('js', fileName);
}
}
});
return {
mode,
resolve: {
modules: [srcPath, 'node_modules'],
},
plugins,
entry: entryPoints,
output: {
path: distPath,
filename: '[name].js',
},
module: {
rules: [
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
],
},
};
};