-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwebpack.config.js
148 lines (135 loc) · 3.5 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
/* eslint-env node */
const webpack = require(`webpack`)
const CleanWebpackPlugin = require(`clean-webpack-plugin`)
const CloudflareWorkerPlugin = require(`cloudflare-workers-webpack-plugin`)
const ESLintPlugin = require(`eslint-webpack-plugin`)
const { bootstrap, resolve } = require(`./lib`)
function createWebpackConfig(env) {
let params = bootstrap(env)
let {
disabledPatterns,
enabledPatterns,
exampleGreeting,
filename,
metadataPath,
minify,
printOutput: verbose,
useColors: colors,
useEmoji: emoji,
workerSrc: entry,
deploy,
reset,
debug,
site,
zone,
cfEmail,
cfApiKey,
scriptName,
} = params
return {
entry,
output: {
path: resolve(`dist`),
filename,
},
bail: true,
cache: false,
// Let Webpack know we mean business
mode: `production`,
// Let Webpack know the context in which our script will run
target: `webworker`,
module: {
rules: [
/*
This runs all JS through Babel to ensure compatibility with the
Cloudflare Worker (i.e. latest Chrome) runtime.
*/
{
test: /\.m?js$/,
loader: `babel-loader`,
exclude: /\/node_modules\//,
options: {
// requireConfigFile: false,
presets: [
[
`@babel/preset-env`,
{
modules: false,
loose: true,
useBuiltIns: `usage`,
debug,
corejs: 3,
targets: {
browsers: `last 1 Chrome versions`,
},
exclude: [
/web\.dom/, // We ain't got no DOM...
/generator|runtime/,
],
},
],
],
plugins: [],
},
},
],
},
// Prevent Webpack from getting angry if we bundle a large script
performance: {
hints: false,
},
// Prevent Webpack from shimming Node features and bloating our Worker
// scripts
node: false,
optimization: {
// Minify the final script if we're deploying and our config allows it
minimize: minify,
noEmitOnErrors: true,
},
// Only spit out errors if we have them...
stats: `errors-only`,
plugins: [
new ESLintPlugin({
failOnError: true,
failOnWarning: false,
emitError: true,
cache: false,
}),
// Remove any previous builds in the dist folder
new CleanWebpackPlugin({
dry: false,
verbose: debug,
cleanOnceBeforeBuildPatterns: [`*.js`],
cleanAfterEveryBuildPatterns: [`tmp`],
}),
/*
Injected variables are parsed as strings BEFORE injecting that means
strings must be double-quoted, so use JSON.stringify on the value of any
variables you wish to inject
*/
new webpack.DefinePlugin({
INJECTED_VARIABLE: JSON.stringify(exampleGreeting),
}),
/*
This deploys our worker script to Cloudflare and manages route patterns
*/
new CloudflareWorkerPlugin(cfEmail, cfApiKey, {
scriptName,
colors,
disabledPatterns,
emoji,
enabled: deploy,
enabledPatterns,
metadataPath,
reset,
site,
verbose,
zone,
}),
],
}
}
module.exports =
process.env.NODE_ENV === `testing`
? createWebpackConfig
: createWebpackConfig()