This repository was archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.prod.js
More file actions
96 lines (81 loc) · 3.18 KB
/
Copy pathwebpack.config.prod.js
File metadata and controls
96 lines (81 loc) · 3.18 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
94
95
96
const webpack = require('webpack');
const merge = require('webpack-merge');
const ValidateHTMLLinksPlugin = require('validate-html-links-webpack-plugin');
const RemovePlugin = require('remove-files-webpack-plugin');
//#region Custom plugins.
const fs = require('fs');
const path = require('path');
class FixManifestPlugin {
constructor(platform) {
this.platform = platform;
}
apply(compiler) {
compiler.hooks.afterEmit.tapAsync('fix-manifest-plugin', (compilation, callback) => {
this.handleAfterEmitHook(compilation, callback);
});
}
handleAfterEmitHook(compilation, callback) {
console.log(this.platform);
const files = fs.readdirSync(path.join(__dirname, 'dist', this.platform, 'interaction', 'js'));
let manifest = fs.readFileSync(path.join(__dirname, 'dist', this.platform, 'manifest.json'), {encoding: 'utf-8'});
for (let name of ['content', 'background']) {
const regexp = new RegExp(`(${name})(.*)(\.js$)`, 'm');
for (let file of files) {
if (regexp.test(file)) {
manifest = manifest.replace(`${name}.js`, file);
break;
}
}
}
fs.writeFileSync(path.join(__dirname, 'dist', this.platform, 'manifest.json'), manifest);
callback();
}
}
//#endregion
module.exports = function(env) {
env = env || {};
const platform = env.platform ? env.platform : 'chromium';
const commonConfig = require('./webpack.config.common')(env);
return merge(commonConfig, {
mode: 'production',
devtool: 'nosources-source-map',
output: {
//filename: "[name].[chunkhash].min.js",
filename: '[name].min.js',
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}),
/*
* Webpack creates empty sources maps for css files.
* So, we remove them.
*/
env.dontRemove ? {apply: () => {return true}} : new RemovePlugin({
after: {
root: __dirname,
test: [
{
folder: `dist/${platform}/interface/css/styles`,
method: (filePath) => {
return new RegExp(/\.map$/, 'm').test(filePath);
}
},
{
folder: `dist/${platform}/interaction/css`,
method: (filePath) => {
return new RegExp(/(\.js|\.map)$/, 'm').test(filePath);
}
}
]
}
}),
/*
* HTML files contains invalid links for JS files.
* So, we replace [name].js to [name].[contenthash].min.js.
*/
new ValidateHTMLLinksPlugin(),
new FixManifestPlugin(platform)
]
});
}