-
Notifications
You must be signed in to change notification settings - Fork 18
/
webpack.config.js
236 lines (217 loc) · 6.67 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const path = require("path");
const webpack = require("webpack");
const packageJson = require("./package.json");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const ZipPlugin = require("zip-webpack-plugin");
// Regexes for style files
const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
function getStyleLoaders({ cssOptions = {}, preProcessor } = {}) {
const loaders = [
// Turn CSS into JS modules that inject <style> tags
"style-loader",
{
// Resolve paths in CSS files
loader: "css-loader",
options: cssOptions,
},
{
// Run PostCSS actions
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [require("autoprefixer")],
},
},
},
];
if (preProcessor) {
loaders.push({
loader: preProcessor,
});
}
return loaders;
}
module.exports = (env, argv) => {
const isDev = argv.mode === "development";
return {
entry: {
background: "./src/background/background.ts",
popup: "./src/components/Popup/index.tsx",
options: "./src/components/Options/index.tsx",
welcome: "./src/components/Welcome/index.tsx",
example: "./src/contentScripts/example.ts",
},
// Use built-in optimizations based on mode
// https://webpack.js.org/configuration/mode/
mode: "development",
// Source maps must be inline to work with TypeScript
devtool: "cheap-module-inline-source-map",
module: {
rules: [
// JS
{
test: /\.js(x?)$/,
exclude: /node_modules/,
use: ["babel-loader", "eslint-loader"],
},
// Typescript
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{ loader: "babel-loader" },
{
// ts-loader is used for transpiling to generate source maps
loader: "ts-loader",
options: {
// Skip type checking while developing but enforce for production
transpileOnly: isDev,
},
},
{ loader: "eslint-loader" },
],
},
// CSS
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders(),
},
{
test: cssModuleRegex,
use: getStyleLoaders({ cssOptions: { modules: true } }),
},
{
test: sassRegex,
exclude: sassModuleRegex,
use: getStyleLoaders({ preProcessor: "sass-loader" }),
},
{
test: sassModuleRegex,
use: getStyleLoaders({
cssOptions: { modules: true },
preProcessor: "sass-loader",
}),
},
// Assets
{
test: /\.(png|svg|mp4)$/,
loader: "file-loader",
options: {
name: "[name].[ext]",
},
},
{
test: /\.(woff|woff2)$/,
use: {
loader: "file-loader",
options: {
name: "[name].[ext]",
outputPath: "fonts/",
esModule: false,
},
},
},
],
},
resolve: {
// Allow leaving off extensions when importing
extensions: [".js", ".jsx", ".ts", ".tsx"],
// Allow importing directly from these directories
modules: [path.resolve(__dirname, "src"), "node_modules"],
},
output: {
path: path.resolve(__dirname, isDev ? "dist-dev" : "dist-prod"),
filename: "[name].bundle.js",
},
devServer: {
contentBase: path.join(__dirname, "dist-dev"),
port: 3000,
hot: true,
// Display all files when navigating to /
writeToDisk: true,
// Allow live reloading / HMR when extension is loaded in Chrome
disableHostCheck: true,
},
plugins: [
new webpack.DefinePlugin({
PLATFORM:
env && env.platform === "firefox"
? JSON.stringify("firefox")
: JSON.stringify("chrome"),
}),
// Clean build folder
new CleanWebpackPlugin({
// Prevent cleaning copied files since copy-webpack-plugin does not regenerate unchanged files
cleanStaleWebpackAssets: false,
}),
// Copy manifest.json to build folder
new CopyWebpackPlugin({
patterns: [
{
from: "src/manifest.json",
transform: (content) => {
const manifest = JSON.parse(content.toString());
// Use fields from package.json for manifest
const manifestFields = {
description: packageJson.description,
version: packageJson.version,
};
// Use 'unsafe-eval' to allow eval() generated by webpack in development mode
manifestFields["content_security_policy"] = `script-src 'self' ${
isDev ? "'unsafe-eval'" : ""
}; object-src 'self'`;
if (env && env.platform === "firefox") {
// Incognito 'split' mode is not supported on Firefox
if (manifest.incognito === "split") {
delete manifest.incognito;
}
// Assign an add-on ID to use storage.sync
// https://extensionworkshop.com/documentation/develop/testing-persistent-and-restart-features/
if (isDev) {
manifestFields.applications = {
gecko: {
id: "[email protected]",
},
};
}
}
// Add fields to the manifest file using package.json
return Buffer.from(
JSON.stringify({
...manifestFields,
...manifest,
})
);
},
},
],
}),
// Generate HTML files
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "index.html"),
filename: "popup.html",
chunks: ["popup"],
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "index.html"),
filename: "options.html",
chunks: ["options"],
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "index.html"),
filename: "welcome.html",
chunks: ["welcome"],
}),
!isDev &&
new ZipPlugin({
filename: `${packageJson.name}-v${packageJson.version}.zip`,
}),
].filter((plugin) => !!plugin),
};
};