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
143 lines (128 loc) · 3.48 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
/* eslint strict: off, node/no-unsupported-features: ["error", { version: 6 }] */
"use strict"
const JSON6 = require("json-6")
const fs = require("fs-extra")
const path = require("path")
const webpack = require("webpack")
const readJSON = (filename) => JSON6.parse(fs.readFileSync(filename))
const {
BannerPlugin,
EnvironmentPlugin,
NormalModuleReplacementPlugin
} = webpack
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer")
const OptimizeJsPlugin = require("optimize-js-plugin")
const UglifyJSPlugin = require("uglifyjs-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) {",
" return exported.a = exported",
"}",
"__webpack_require__.r = function () {}"
].join("\n")
)
})
}
}
const { ESM_ENV } = process.env
const ESM_VERSION = readJSON("./package.json").version
const isProd = /production/.test(ESM_ENV)
const isTest = /test/.test(ESM_ENV)
const externals = [
"Array", "Buffer", "Error", "EvalError", "Function", "JSON", "Object",
"Promise", "RangeError", "ReferenceError", "SyntaxError", "TypeError",
"URIError", "eval"
]
const hosted = [
"console"
]
const uglifyOptions = readJSON("./.uglifyrc")
/* eslint-disable sort-keys */
const config = {
devtool: false,
entry: {
esm: "./src/index.js"
},
output: {
filename: "[name].js",
libraryExport: "default",
libraryTarget: "commonjs2",
path: path.resolve("build"),
pathinfo: false
},
mode: isProd ? "production" : "development",
module: {
rules: [
{
loader: "babel-loader",
test: /\.js$/,
type: "javascript/auto"
}
]
},
optimization: {
minimizer: [
new UglifyJSPlugin({ uglifyOptions })
],
nodeEnv: 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 NormalModuleReplacementPlugin(
/acorn\/src\/regexp\.js/,
path.resolve("src/acorn/replacement/regexp.js")
),
new EnvironmentPlugin({ ESM_VERSION }),
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"
}
module.exports = config