-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.build.js
executable file
·78 lines (76 loc) · 3.56 KB
/
webpack.build.js
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
const webpack = require('webpack');
const merge = require('webpack-merge');
const common = require('./webpack.nondev.js');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const PrerenderSPAPlugin = require('prerender-spa-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const pretty = require('pretty');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const publicPath = '/~/media/data-visualizations/interactives/2019/statedebt/' // <<== set this for each project
module.exports = env => {
return merge(common(env), {
devtool: false,
optimization: {
minimizer: [
new TerserPlugin({
sourceMap: true, // Must be set to true if using source-maps in production
terserOptions: {
compress: {
drop_console: true,
},
},
})
],
splitChunks: {
automaticNameDelimiter: '-',
chunks: 'all'
}
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'title title title',
inject: false,
template: './src/index.html'
}),
new PrerenderSPAPlugin({
// Required - The path to the webpack-outputted app to prerender.
staticDir: path.join(__dirname, 'dist'),
// Required - Routes to render.
routes: ['/'],
renderer: new PrerenderSPAPlugin.PuppeteerRenderer({
injectProperty: 'IS_PRERENDERING',
inject: true,
//headless: false,
//sloMo: 10000,
renderAfterTime: 1000
}),
postProcess: function(renderedRoute){
renderedRoute.html = renderedRoute.html.replace(/class="emitted-css" href="(.*?)"/,'class="emitted-css" href="' + publicPath + '$1' + '"');
renderedRoute.html = renderedRoute.html.replace(/class="emitted-bundle" src="(.*?)"/g,'class="emitted-bundle" src="' + publicPath + '$1' + '"');
//renderedRoute.html = renderedRoute.html.replace('src="js/index.js"','src="' + publicPath + 'js/index.js"');
renderedRoute.html = renderedRoute.html.replace(/<head>[\s\S].*<\/head>/,'').replace(/<\/?html.*?>|<\/?body.*?>/g,'');
renderedRoute.html = pretty(renderedRoute.html);
return renderedRoute;
}
}),
new webpack.DefinePlugin({
'PUBLICPATH': '"' + publicPath + '"', // from https://webpack.js.org/plugins/define-plugin/: Note that because the plugin does a direct text replacement, the value given to it must include actual quotes inside of the string itself. Typically, this is done either with alternate quotes, such as '"production"', or by using JSON.stringify('production').
}),
new webpack.EnvironmentPlugin({
'NODE_ENV': env
}),
new webpack.SourceMapDevToolPlugin({
filename: '[name]js.map',
})
],
output: {
filename: '[name].js?v=[hash:6]',
path: path.resolve(__dirname, 'dist'),
// publicPath
}
});
};