-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwebpack.config.js
162 lines (144 loc) · 4.17 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
const fs = require("fs");
const path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require("webpack");
const packageJson = require("./package.json");
const manifestJson = require("./src/manifest.json");
const AfterEmitPlugin = (fn) => ({
apply: (compiler) => {
compiler.hooks.afterEmit.tap("AfterEmitPlugin", fn);
},
});
const buildManifest = () => {
const manifest = Object.assign(manifestJson, {
version: packageJson.version,
});
fs.writeFileSync("build/manifest.json", JSON.stringify(manifest, null, 2));
};
const html = [
new HtmlWebpackPlugin({
chunks: ["panel"],
filename: "panel.html",
title: "Snippets",
template: "./src/panel.html",
}),
new HtmlWebpackPlugin({
chunks: ["devtools"],
filename: "devtools.html",
title: "Snippets",
}),
];
const devServerHtml = [
new HtmlWebpackPlugin({
template: "./src/panel.html",
chunks: ["test"],
filename: "index.html",
}),
];
const extractCssLoader = {
loader: MiniCssExtractPlugin.loader,
options: { hmr: false },
};
module.exports = (env, args) => {
const isDevServer = env && env.devServer;
const isProduction = args.mode === "production";
const entry = isDevServer
? {
test: "./src/test.ts",
"worker-javascript-eslint":
"./src/mode-javascript-eslint/worker-javascript-eslint.js",
}
: {
background: "./src/background.ts",
devtools: "./src/devtools.ts",
panel: "./src/panel.ts",
"worker-javascript-eslint":
"./src/mode-javascript-eslint/worker-javascript-eslint.js",
};
return {
mode: args.mode || "development",
devtool: !isProduction && "cheap-source-map",
entry,
output: {
path: path.resolve(__dirname, "build"),
},
module: {
strictExportPresence: true,
rules: [
{
oneOf: [
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: "url-loader",
options: {
limit: 10000,
fallback: "file-loader",
name: "[name].[ext]",
},
},
{
test: /\.(jsx?|tsx?)$/,
loader: "babel-loader",
options: { cacheDirectory: true },
},
{
test: /\.css$/,
use: [
isProduction ? extractCssLoader : "style-loader",
"css-loader",
],
},
{
test: /\.(ttf|eot|woff|woff2)$/,
loader: "file-loader",
options: { name: "fonts/[name].[ext]" },
},
{
// Exclude a few other extensions so they get processed by Webpack's
// internal loaders.
exclude: [/\.js$/, /\.html$/, /\.json$/, /\.ejs$/],
loader: "file-loader",
options: { name: "[name].[ext]" },
},
],
},
],
},
plugins: [
new webpack.DefinePlugin({
"process.env.SNIPPETS_VERSION": JSON.stringify(packageJson.version),
"process.env.NODE_ENV": JSON.stringify(args.mode || "development"),
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({ patterns: [{ from: "static", to: "./" }] }),
AfterEmitPlugin(buildManifest),
isProduction && new MiniCssExtractPlugin(),
...(isDevServer ? devServerHtml : html),
isDevServer && new ForkTsCheckerWebpackPlugin(),
].filter(Boolean),
optimization: {
minimize: isProduction,
},
performance: {
hints: false,
},
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx"],
mainFields: ["browser", "main", "module"],
},
node: {
module: "empty",
dgram: "empty",
dns: "mock",
fs: "empty",
http2: "empty",
net: "empty",
tls: "empty",
child_process: "empty",
},
};
};