-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
198 lines (171 loc) · 5.52 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
'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 debug = require('debug')(`${require('./package.json').name}: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`,
// ? These are used at various points (including at compile time by
// ? Next.js) to get mongo schema configuration and/or test dummy data.
// ! Must be defined if using @xunnamius/mongo-schema
'configverse/get-schema-config': `${__dirname}/src/backend/db.ts`,
// ! Must be defined if using @xunnamius/mongo-test
'configverse/get-dummy-data': `${__dirname}/test/db.ts`
};
let sanitizedEnv = {};
let { NODE_ENV: nodeEnv, ...sanitizedProcessEnv } = {
...process.env,
NODE_ENV: 'production'
};
try {
require('node:fs').accessSync('.env');
const { NODE_ENV: forceEnv, ...parsedEnv } = require('dotenv').config().parsed;
nodeEnv = forceEnv || nodeEnv;
sanitizedEnv = parsedEnv;
debug(`NODE_ENV: ${nodeEnv}`);
debug('sanitized .env vars: %O', sanitizedEnv);
} catch (error) {
debug(`.env support disabled; reason: ${error}`);
}
debug('sanitized process env: %O', sanitizedProcessEnv);
verifyEnvironment();
const envPlugins = [
// ? NODE_ENV is not a "default" (unlike below) but an explicit overwrite
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(nodeEnv)
}),
// ? 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 = [
nodeExternals(),
({ request }, callback) =>
// ? Externalize all .json imports (required as commonjs modules)
/\.json$/.test(request) ? callback(null, `commonjs ${request}`) : callback()
];
/* const libConfig = {
name: 'lib',
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 ONLY!
libraryTarget: 'commonjs2'
},
externals,
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]
}; */
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`,
'log-stats': `${__dirname}/external-scripts/log-stats.ts`,
'initialize-data': `${__dirname}/external-scripts/initialize-data/index.ts`
// 'simulate-activity': `${__dirname}/external-scripts/simulate-activity.ts`
},
output: {
filename: '[name].js',
path: `${__dirname}/external-scripts/bin`
},
externals,
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,
// * ▼ 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,
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,
// * ▼ For bundled CLI applications, make entry file executable w/ shebang
new BannerPlugin({ banner: '#!/usr/bin/env node', raw: true, entryOnly: true })
]
}; */
module.exports = [/*libConfig,*/ externalsConfig /*, cliConfig*/];
debug('exports: %O', module.exports);