-
Notifications
You must be signed in to change notification settings - Fork 147
/
webpack.config.js
159 lines (145 loc) · 3.85 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
/* eslint strict: off, node/no-unsupported-features: ["error", { version: 6 }] */
"use strict"
const fs = require("fs-extra")
const path = require("path")
const webpack = require("webpack")
const {
BannerPlugin,
EnvironmentPlugin,
NormalModuleReplacementPlugin
} = webpack
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 WebpackRequirePlugin {
apply(compiler) {
compiler.hooks.compilation.tap("MainTemplate", (compilation) => {
compilation.mainTemplate.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 () => exported.a",
"}",
"__webpack_require__.r = function () {}"
].join("\n")
)
})
}
}
const { ESM_ENV } = process.env
const {
files: PKG_FILENAMES,
version: PKG_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"
}
]
},
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: [
'"use strict";\n',
"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({
PKG_FILENAMES,
PKG_VERSION
}),
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 WebpackRequirePlugin
],
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