generated from MattIPv4/template
-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
webpack.config.js
78 lines (70 loc) · 2.78 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
import { fileURLToPath } from 'url';
import { createRequire } from 'module';
const NODE_ENV = process.env.NODE_ENV || 'development';
import dotenv from 'dotenv';
const env = dotenv.config({ path: fileURLToPath(new URL(`${NODE_ENV}.env`, import.meta.url)) });
import webpack from 'webpack';
import WorkersSentryWebpackPlugin from 'workers-sentry/webpack.js';
import { registerCommands } from 'workers-discord';
import commands from './src/commands/index.js';
console.log(`Using ${NODE_ENV} environment for build...`);
export default {
mode: 'none',
target: 'webworker',
entry: fileURLToPath(new URL('src/index.js', import.meta.url)),
output: {
path: fileURLToPath(new URL('dist', import.meta.url)),
filename: 'worker.js',
// Generate an ESM module output for Cloudflare
module: true,
chunkFormat: 'module',
library: { type: 'module' },
},
experiments: { outputModule: true },
plugins: [
// Hook in the commands registrations process before each Webpack run
{
apply: compiler => compiler.hooks.beforeRun.tapPromise(
'RegisterCommandsBeforeWebpack',
() => registerCommands(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
commands,
true,
process.env.TEST_GUILD_ID,
).then(res => {
console.log(`Registered ${res.length} commands...`);
// console.dir(res, { depth: null });
}),
),
},
// Expose our environment in the worker
new webpack.DefinePlugin(Object.entries(env.parsed).reduce((obj, [ key, val ]) => {
obj[`process.env.${key}`] = JSON.stringify(val);
return obj;
}, { 'process.env.NODE_ENV': JSON.stringify(NODE_ENV) })),
// Ensure single chunk
new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
// Publish source maps to Sentry on each build
process.env.SENTRY_AUTH_TOKEN
&& process.env.SENTRY_ORG
&& process.env.SENTRY_PROJECT
&& new WorkersSentryWebpackPlugin(
process.env.SENTRY_AUTH_TOKEN,
process.env.SENTRY_ORG,
process.env.SENTRY_PROJECT,
),
].filter(Boolean),
// Don't webpack node-fetch, rely on fetch global
externals: { 'node-fetch': 'fetch' },
externalsType: 'global',
// We need to polyfill buffer for DNS packets
resolve: {
fallback: {
buffer: createRequire(import.meta.url).resolve('buffer/'),
},
},
// Always expose a source map
// WorkersSentryWebpackPlugin will do the same when there is a Sentry token
devtool: 'source-map',
};