-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.build.js
More file actions
92 lines (77 loc) · 2.59 KB
/
webpack.config.build.js
File metadata and controls
92 lines (77 loc) · 2.59 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
process.env.NODE_ENV = 'production';
import prettier from 'prettier';
import TerserPlugin from 'terser-webpack-plugin';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import {
headerBase,
configBase,
merge,
pkg
} from './webpack.config.infra.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const isAnalyzing = process.env.ANALYZE === 'true';
const header = headerBase({
__VERSION__: pkg.version
});
class format {
apply(compiler) {
compiler.hooks.afterEmit.tapPromise('format', async () => {
const filePath = path.join(
compiler.options.output.path,
compiler.options.output.filename
);
if (!fs.existsSync(filePath)) return;
const code = fs.readFileSync(filePath, 'utf8');
const body = code.replace( /^\/\/ ==UserScript==[\s\S]*?\/\/ ==\/UserScript==\n?/, '' );
const formatted = await prettier.format(body, {
parser: 'babel',
printWidth: Infinity,
tabWidth: 2,
semi: true,
singleQuote: true
});
const notice = [
' //',
' // WebScript is free and open source.',
' //',
' // If you purchased this script from a third party,',
' // you may have been misled or overcharged.',
' //',
' // Official repository:',
` // ${pkg.homepage}`,
' //',
].join('\n');
fs.writeFileSync(filePath, header + '\n\n' + formatted.replace(/}\)\s*\(\s*\)\s*;/, '$&\n' + notice));
});
}
}
export default merge(configBase(), {
mode: process.env.NODE_ENV,
plugins: [
...(isAnalyzing ? [new BundleAnalyzerPlugin({
analyzerHost: 'localhost',
analyzerPort: 9294,
openAnalyzer: true
})] : []),
new format()
],
optimization: {
concatenateModules: !isAnalyzing,
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: false
}
},
extractComments: false
})
]
},
devtool: false
});