-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwebpack.config.js
154 lines (148 loc) · 4.46 KB
/
webpack.config.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const fs = require("fs");
const {join, resolve} = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const GenerateJsonPlugin = require("generate-json-webpack-plugin");
const ZipWebpackPlugin = require("zip-webpack-plugin");
const {BundleAnalyzerPlugin} = require("webpack-bundle-analyzer");
const {CleanWebpackPlugin} = require("clean-webpack-plugin");
const SSGPlugin = require("static-site-generator-webpack-plugin");
const {getIfUtils, removeEmpty} = require("webpack-config-utils");
const manifest = require("./src/manifest.json");
const globals = require("./build/mock/globals");
const ShortName = "QuicKey";
const FullName = `${ShortName} – The quick tab switcher`;
module.exports = (env, argv) => {
const {ifProduction, ifNotProduction, ifBuildPopup} = getIfUtils(env, ["production", "buildPopup"]);
const outputPath = resolve(__dirname, ifProduction("build/out", "dist"));
const mode = ifProduction("production", "development");
const baseConfig = {
mode,
devtool: ifProduction(false, "source-map"),
resolve: {
alias: {
"@": resolve(__dirname, "src/js"),
lodash: resolve(__dirname, "src/js/lib/lodont"),
cp: resolve(__dirname, "src/js/lib/cp"),
}
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"]
}
]
},
stats: {
builtAt: true,
},
performance: {
// increase the limits so webpack doesn't spam the console with
// complaints about the bundle sizes
maxEntrypointSize: 500_000,
maxAssetSize: 500_000,
}
};
const output = {
filename: "js/[name].js",
path: outputPath,
clean: true,
};
const buildTime = new Date().toISOString();
const htmlOptions = (name) => ({
template: `./src/${name}.html`,
filename: `${name}.html`,
chunks: ["error-queue", name],
minify: false,
// create a timestamp that's injected into an HTML comment via the plugins
buildTime
});
// update the manifest to use prod values. if we're in prod mode, this
// version will be output below.
manifest.name = FullName;
manifest.short_name = ShortName;
manifest.action.default_title = ShortName;
// manifest.content_security_policy = manifest.content_security_policy.replace(" 'unsafe-eval'", "");
console.log(`Build mode: ${mode} ${ifBuildPopup("buildPopup", "")} (${buildTime})`);
return removeEmpty([
{
...baseConfig,
entry: {
// put this entry point first, so it gets loaded before the
// main module on each page
"error-queue": "./src/js/lib/error-queue.js",
popup: "./src/js/popup/main.js",
options: "./src/js/options/main.js",
background: "./src/js/background/background.js",
sw: "./src/js/background/sw.js",
},
plugins: removeEmpty([
new CopyWebpackPlugin({
patterns: removeEmpty([
{ from: "src/css/", to: "css" },
{ from: "src/img/", to: "img" },
{ from: "src/js/lib/pinyin.js", to: "js/lib" },
{ from: "src/js/popup/init.js", to: "js/popup" },
ifNotProduction("src/manifest.json"),
])
}),
new HtmlWebpackPlugin(htmlOptions("popup")),
new HtmlWebpackPlugin(htmlOptions("options")),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: removeEmpty([
ifProduction(join(__dirname, "build/out/**")),
join(__dirname, "build/temp/**"),
]),
}),
new BundleAnalyzerPlugin({
analyzerMode: "static",
openAnalyzer: false,
reportFilename: resolve(__dirname, "build/temp/report.html"),
}),
ifProduction(new GenerateJsonPlugin(
"manifest.json",
manifest,
null,
"\t"
)),
ifProduction(new ZipWebpackPlugin({
path: join(__dirname, `release/${manifest.version}`),
filename: `${ShortName}.zip`
})),
]),
optimization: {
// splitChunks: {
// chunks: "all",
// },
minimize: ifProduction(true, false),
},
output
},
ifBuildPopup({
...baseConfig,
entry: {
// output this bundle to build/temp/
"../temp/ssg": "./build/scripts/build-popup.js",
},
plugins: [
new SSGPlugin({
entry: "../temp/ssg.js",
paths: ["../temp/popup.html"],
// pass the fs module to the render function, since it can't
// seem to import that module on its own
locals: { fs },
globals,
}),
],
output: {
...output,
// don't include the js/ path in the filename since the ssg
// entry above includes a path
filename: "[name].js",
libraryTarget: "umd"
}
})
]);
};