This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
webpack.config.js
184 lines (165 loc) · 4.6 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
/* eslint strict: off, node/no-unsupported-features: ["error", { version: 6 }] */
"use strict"
const arrayRemove = require("./script/array-remove.js")
const fs = require("fs-extra")
const path = require("path")
const webpack = require("webpack")
const {
BannerPlugin,
EnvironmentPlugin,
NormalModuleReplacementPlugin
} = webpack
const {
ModuleConcatenationPlugin
} = webpack.optimize
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer")
const OptimizeJsPlugin = require("optimize-js-plugin")
const TerserPlugin = require("terser-webpack-plugin")
const UnusedPlugin = require("unused-webpack-plugin")
class WebpackTemplatePlugin {
apply({ hooks: { compilation } }) {
compilation.tap("MainTemplate", ({ mainTemplate: { hooks } }) => {
// Simplify webpack module scaffolding.
hooks.requireExtensions.tap("MainTemplate", () =>
[
"__webpack_require__.d = function (exported, name, get) {",
" Reflect.defineProperty(exported, name, {",
" configurable: true,",
" enumerable: true,",
" get",
" })",
"}",
"__webpack_require__.n = function (exported) {",
" exported.a = exported",
" return function () { return exported }",
"}"
].join("\n")
)
// Remove webpack additions.
hooks.render.tap("ModuleTemplate", ({ children }) => {
const COMPATIBILITY_HELPER = "__webpack_require__.r(__webpack_exports__);\n"
const USE_STRICT = '"use strict";\n'
function shouldRemove(value) {
return value === COMPATIBILITY_HELPER ||
value === USE_STRICT
}
arrayRemove(children, shouldRemove)
for (let child of children) {
do {
arrayRemove(child.replacements, ({ content }) => shouldRemove(content))
} while ((child = child._source))
}
})
})
}
}
const { ESM_ENV } = process.env
const {
files: PACKAGE_FILENAMES,
version: PACKAGE_VERSION
} = fs.readJSONSync("./package.json")
const isProd = /production/.test(ESM_ENV)
const isTest = /test/.test(ESM_ENV)
const externals = [
"Array", "Buffer", "Error", "EvalError", "Function", "JSON", "Object",
"Promise", "RangeError", "ReferenceError", "Reflect", "SyntaxError",
"TypeError", "URIError", "eval"
]
const hosted = [
"console"
]
const babelOptions = require("./.babel.config.js")
const terserOptions = fs.readJSONSync("./.terserrc")
const config = {
devtool: false,
entry: {
esm: "./src/index.js"
},
mode: isProd ? "production" : "development",
module: {
rules: [
{
loader: "babel-loader",
options: babelOptions,
test: /\.js$/,
type: "javascript/auto"
}
]
},
node: false,
optimization: {
minimizer: [
new TerserPlugin({ terserOptions })
],
nodeEnv: false
},
output: {
filename: "[name].js",
libraryExport: "default",
libraryTarget: "commonjs2",
path: path.resolve("build"),
pathinfo: false
},
plugins: [
new BannerPlugin({
banner: [
"var __shared__;",
"const __non_webpack_module__ = module;",
"const __external__ = { " +
externals
.map((name) => name + ": global." + name)
.join(", ") +
" };",
"const " +
hosted
.map((name) => name + " = global." + name)
.join(", ") +
";\n"
].join("\n"),
entryOnly: true,
raw: true
}),
new BundleAnalyzerPlugin({
analyzerMode: "static",
defaultSizes: "gzip",
logLevel: "silent",
openAnalyzer: false,
reportFilename: "report.html"
}),
new EnvironmentPlugin({
PACKAGE_FILENAMES,
PACKAGE_VERSION
}),
new ModuleConcatenationPlugin,
new NormalModuleReplacementPlugin(
/acorn[\\/]src[\\/]regexp\.js/,
path.resolve("src/acorn/replacement/regexp.js")
),
new UnusedPlugin({
directories : [path.resolve("src")],
exclude: [
".*",
"*.json",
"**/vendor/*"
],
root : __dirname
}),
new WebpackTemplatePlugin
],
target: "node"
}
/* eslint-enable sort-keys */
if (isProd) {
config.plugins.push(
new OptimizeJsPlugin,
new EnvironmentPlugin({ NODE_DEBUG: false })
)
}
if (isTest) {
config.entry.compiler = "./src/compiler.js"
config.entry.entry = "./src/entry.js"
config.entry.runtime = "./src/runtime.js"
config.entry["get-file-path-from-url"] = "./src/util/get-file-path-from-url.js"
config.entry["get-url-from-file-path"] = "./src/util/get-url-from-file-path.js"
}
module.exports = config