-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
261 lines (225 loc) · 7.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
'use strict';
// This webpack config is used to transpile src to dist, compile externals,
// compile executables, etc
const { EnvironmentPlugin, DefinePlugin, BannerPlugin } = require('webpack');
const { verifyEnvironment } = require('./expect-env');
const nodeExternals = require('webpack-node-externals');
const pkgName = require('./package.json').name;
const debug = require('debug')(`${pkgName}:webpack-config`);
const IMPORT_ALIASES = {
universe: `${__dirname}/src/`,
multiverse: `${__dirname}/lib/`,
testverse: `${__dirname}/test/`,
externals: `${__dirname}/external-scripts/`,
types: `${__dirname}/types/`,
package: `${__dirname}/package.json`
};
let sanitizedEnv = {};
let { NODE_ENV: nodeEnv, ...sanitizedProcessEnv } = {
...process.env,
NODE_ENV: 'production'
};
try {
require('fs').accessSync('.env');
const { NODE_ENV: forceEnv, ...parsedEnv } = require('dotenv').config().parsed;
nodeEnv = forceEnv || nodeEnv;
sanitizedEnv = parsedEnv;
debug(`NODE_ENV: ${nodeEnv}`);
debug('sanitized env: %O', sanitizedEnv);
} catch (e) {
debug(`env support disabled; reason: ${e}`);
}
debug('sanitized process env: %O', sanitizedProcessEnv);
verifyEnvironment();
const envPlugins = ({ esm /*: boolean */ }) => [
// ? NODE_ENV is not a "default" (unlike below) but an explicit overwrite
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(nodeEnv)
}),
// ? NODE_ESM is true when we're compiling in ESM mode (useful in source)
...(esm
? [
new DefinePlugin({
'process.env.NODE_ESM': String(esm)
})
]
: []),
// ? Load our .env results as the defaults (overridden by process.env)
new EnvironmentPlugin({ ...sanitizedEnv, ...sanitizedProcessEnv }),
// ? Create shim process.env for undefined vars
// ! The above already replaces all process.env.X occurrences in the code
// ! first, so plugin order is important here
new DefinePlugin({ 'process.env': '{}' })
];
const externals = ({ esm /*: boolean */ }) => [
nodeExternals({ importType: esm ? 'node-commonjs' : 'commonjs' }),
({ request }, cb) => {
if (request == 'package') {
// ? Externalize special "package" (alias of package.json) imports
cb(null, `${esm ? 'node-commonjs' : 'commonjs'} ${pkgName}/package.json`);
} else if (/\.json$/.test(request)) {
// ? Externalize all other .json imports
cb(null, `${esm ? 'node-commonjs' : 'commonjs'} ${request}`);
} else cb();
}
];
const libCjsConfig = {
name: 'cjs',
mode: 'production',
target: 'node',
node: false,
entry: `${__dirname}/src/index.ts`,
output: {
filename: 'index.js',
path: `${__dirname}/dist`,
// ! ▼ Only required for libraries
// ! ▼ Note: ESM outputs are handled by Babel (bundle) and Webpack (below)
library: {
type: 'commonjs2'
}
},
externals: externals({ esm: false }),
externalsPresets: { node: true },
stats: {
orphanModules: true,
providedExports: true,
usedExports: true,
errorDetails: true
},
resolve: {
extensions: ['.ts', '.wasm', '.mjs', '.cjs', '.js', '.json'],
// ! If changed, also update these aliases in tsconfig.json,
// ! jest.config.js, next.config.ts, and .eslintrc.js
alias: IMPORT_ALIASES
},
module: {
rules: [{ test: /\.(ts|js)x?$/, loader: 'babel-loader', exclude: /node_modules/ }]
},
optimization: { usedExports: true },
plugins: [...envPlugins({ esm: false })]
};
const libEsmConfig = {
name: 'esm',
mode: 'production',
target: 'node',
node: false,
entry: `${__dirname}/src/index.ts`,
output: {
module: true,
filename: 'index.mjs',
path: `${__dirname}/dist/esm`,
chunkFormat: 'module',
// ! ▼ Only required for libraries
// ! ▼ Note: ESM outputs are handled by Babel ONLY!
library: {
type: 'module'
}
},
experiments: {
outputModule: true
},
externals: externals({ esm: true }),
externalsPresets: { node: true },
stats: {
orphanModules: true,
providedExports: true,
usedExports: true,
errorDetails: true
},
resolve: {
extensions: ['.ts', '.wasm', '.mjs', '.cjs', '.js', '.json'],
// ! If changed, also update these aliases in tsconfig.json,
// ! jest.config.js, next.config.ts, and .eslintrc.js
alias: IMPORT_ALIASES
},
module: {
rules: [{ test: /\.(ts|js)x?$/, loader: 'babel-loader', exclude: /node_modules/ }]
},
optimization: { usedExports: true },
plugins: [...envPlugins({ esm: true })]
};
/* const externalsConfig = {
name: 'externals',
mode: 'production',
target: 'node',
node: false,
entry: {
'ban-hammer': `${__dirname}/external-scripts/ban-hammer.ts`,
'prune-data': `${__dirname}/external-scripts/prune-data.ts`,
// 'initialize-data': `${__dirname}/external-scripts/initialize-data/index.ts`,
// 'worker-friends': `${__dirname}/external-scripts/initialize-data/worker-friends.ts`,
// 'worker-memes': `${__dirname}/external-scripts/initialize-data/worker-memes.ts`,
// 'worker-interactions': `${__dirname}/external-scripts/initialize-data/worker-interactions.ts`,
// 'worker-chats': `${__dirname}/external-scripts/initialize-data/worker-chats.ts`,
// 'simulate-activity': `${__dirname}/external-scripts/simulate-activity/index.ts`
},
output: {
filename: '[name].js',
path: `${__dirname}/external-scripts/bin`
},
externals: externals({ esm: false }),
externalsPresets: { node: true },
stats: {
orphanModules: true,
providedExports: true,
usedExports: true,
errorDetails: true
},
resolve: {
extensions: ['.ts', '.wasm', '.mjs', '.cjs', '.js', '.json'],
// ! If changed, also update these aliases in tsconfig.json,
// ! jest.config.js, next.config.ts, and .eslintrc.js
alias: IMPORT_ALIASES
},
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: 'babel-loader'
}
]
},
optimization: { usedExports: true },
plugins: [
...envPlugins({ esm: false }),
// * ▼ For non-bundled externals, make entry file executable w/ shebang
new BannerPlugin({ banner: '#!/usr/bin/env node', raw: true, entryOnly: true })
]
}; */
const cliConfig = {
name: 'cli',
mode: 'production',
target: 'node',
node: false,
entry: `${__dirname}/src/cli.ts`,
output: {
filename: 'cli.js',
path: `${__dirname}/dist`
},
externals: externals({ esm: false }),
externalsPresets: { node: true },
stats: {
orphanModules: true,
providedExports: true,
usedExports: true,
errorDetails: true
},
resolve: {
extensions: ['.ts', '.wasm', '.mjs', '.cjs', '.js', '.json'],
// ! If changed, also update these aliases in tsconfig.json,
// ! jest.config.js, next.config.ts, and .eslintrc.js
alias: IMPORT_ALIASES
},
module: {
rules: [{ test: /\.(ts|js)x?$/, loader: 'babel-loader', exclude: /node_modules/ }]
},
optimization: { usedExports: true },
plugins: [
...envPlugins({ esm: false }),
// * ▼ For bundled CLI applications, make entry file executable w/ shebang
new BannerPlugin({ banner: '#!/usr/bin/env node', raw: true, entryOnly: true })
]
};
module.exports = [libCjsConfig, libEsmConfig, /*externalsConfig,*/ cliConfig];
debug('exports: %O', module.exports);