-
Notifications
You must be signed in to change notification settings - Fork 742
/
bundle.ts
756 lines (691 loc) · 20.6 KB
/
bundle.ts
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import assert from "node:assert";
import * as fs from "node:fs";
import { builtinModules } from "node:module";
import * as path from "node:path";
import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill";
import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
import * as esbuild from "esbuild";
import tmp from "tmp-promise";
import createModuleCollector from "./module-collection";
import { getBasePath, toUrlPath } from "./paths";
import type { Config } from "./config";
import type { WorkerRegistry } from "./dev-registry";
import type { Entry } from "./entry";
import type { CfModule } from "./worker";
type BundleResult = {
modules: CfModule[];
resolvedEntryPointPath: string;
bundleType: "esm" | "commonjs";
stop: (() => void) | undefined;
sourceMapPath?: string | undefined;
};
type StaticAssetsConfig =
| (Config["assets"] & {
bypassCache: boolean | undefined;
})
| undefined;
/**
* Searches for any uses of node's builtin modules, and throws an error if it
* finds anything. This plugin is only used when nodeCompat is not enabled.
* Supports both regular node builtins, and the new "node:<MODULE>" format.
*/
const checkForNodeBuiltinsPlugin = {
name: "checkForNodeBuiltins",
setup(build: esbuild.PluginBuild) {
build.onResolve(
{
filter: new RegExp(
"^(" +
builtinModules.join("|") +
"|" +
builtinModules.map((module) => "node:" + module).join("|") +
")$"
),
},
() => {
throw new Error(
`Detected a Node builtin module import while Node compatibility is disabled.\nAdd node_compat = true to your wrangler.toml file to enable Node compatibility.`
);
}
);
},
};
/**
* Generate a bundle for the worker identified by the arguments passed in.
*/
export async function bundleWorker(
entry: Entry,
destination: string,
options: {
serveAssetsFromWorker: boolean;
assets: StaticAssetsConfig;
betaD1Shims?: string[];
jsxFactory: string | undefined;
jsxFragment: string | undefined;
rules: Config["rules"];
watch?: esbuild.WatchMode;
tsconfig: string | undefined;
minify: boolean | undefined;
nodeCompat: boolean | undefined;
define: Config["define"];
checkFetch: boolean;
services: Config["services"] | undefined;
workerDefinitions: WorkerRegistry | undefined;
firstPartyWorkerDevFacade: boolean | undefined;
targetConsumer: "dev" | "publish";
local: boolean;
testScheduled?: boolean | undefined;
experimentalLocalStubCache: boolean | undefined;
}
): Promise<BundleResult> {
const {
serveAssetsFromWorker,
betaD1Shims,
jsxFactory,
jsxFragment,
rules,
watch,
tsconfig,
minify,
nodeCompat,
checkFetch,
local,
assets,
workerDefinitions,
services,
firstPartyWorkerDevFacade,
targetConsumer,
testScheduled,
experimentalLocalStubCache,
} = options;
// We create a temporary directory for any oneoff files we
// need to create. This is separate from the main build
// directory (`destination`).
const tmpDir = await tmp.dir({ unsafeCleanup: true });
const entryDirectory = path.dirname(entry.file);
const moduleCollector = createModuleCollector({
wrangler1xlegacyModuleReferences: {
rootDirectory: entryDirectory,
fileNames: new Set(
fs
.readdirSync(entryDirectory, { withFileTypes: true })
.filter(
(dirEntry) =>
dirEntry.isFile() && dirEntry.name !== path.basename(entry.file)
)
.map((dirEnt) => dirEnt.name)
),
},
format: entry.format,
rules,
});
// In dev, we want to patch `fetch()` with a special version that looks
// for bad usages and can warn the user about them; so we inject
// `checked-fetch.js` to do so. However, with yarn 3 style pnp,
// we need to extract that file to an accessible place before injecting
// it in, hence this code here.
const checkedFetchFileToInject = path.join(tmpDir.path, "checked-fetch.js");
if (checkFetch && !fs.existsSync(checkedFetchFileToInject)) {
fs.mkdirSync(tmpDir.path, {
recursive: true,
});
fs.writeFileSync(
checkedFetchFileToInject,
fs.readFileSync(path.resolve(getBasePath(), "templates/checked-fetch.js"))
);
}
// At this point, we take the opportunity to "wrap" any input workers
// with any extra functionality we may want to add. This is done by
// passing the entry point through a pipeline of functions that return
// a new entry point, that we call "middleware" or "facades".
// Look at implementations of these functions to learn more.
// We also have middleware that uses a more "traditional" middleware stack,
// which is all loaded as one in a stack.
const middlewareToLoad: MiddlewareLoader[] = [];
if (testScheduled) {
middlewareToLoad.push({
path: "templates/middleware/middleware-scheduled.ts",
});
}
type MiddlewareFn = (arg0: Entry) => Promise<Entry>;
const middleware: (false | undefined | MiddlewareFn)[] = [
// serve static assets
serveAssetsFromWorker &&
((currentEntry: Entry) => {
return applyStaticAssetFacade(currentEntry, tmpDir.path, assets);
}),
// format errors nicely
// We use an env var here because we don't actually
// want to expose this to the user. It's only used internally to
// experiment with middleware as a teaching exercise.
process.env.FORMAT_WRANGLER_ERRORS === "true" &&
((currentEntry: Entry) => {
return applyFormatDevErrorsFacade(currentEntry, tmpDir.path);
}),
// bind to other dev instances/service bindings
workerDefinitions &&
Object.keys(workerDefinitions).length > 0 &&
services &&
services.length > 0 &&
((currentEntry: Entry) => {
return applyMultiWorkerDevFacade(
currentEntry,
tmpDir.path,
services,
workerDefinitions
);
}),
// Simulate internal environment when using first party workers in dev
firstPartyWorkerDevFacade === true &&
((currentEntry: Entry) => {
return applyFirstPartyWorkerDevFacade(currentEntry, tmpDir.path);
}),
Array.isArray(betaD1Shims) &&
betaD1Shims.length > 0 &&
((currentEntry: Entry) => {
return applyD1BetaFacade(currentEntry, tmpDir.path, betaD1Shims, local);
}),
// Middleware loader: to add middleware, we add the path to the middleware
// Currently for demonstration purposes we have two example middlewares
// Middlewares are togglable by changing the `publish` (default=false) and `dev` (default=true) options
// As we are not yet supporting user created middlewares yet, if no wrangler applied middleware
// are found, we will not load any middleware. We also need to check if there are middlewares compatible with
// the target consumer (dev / publish).
(middlewareToLoad.filter(
(m) =>
(m.publish && targetConsumer === "publish") ||
(m.dev !== false && targetConsumer === "dev")
).length > 0 ||
process.env.EXPERIMENTAL_MIDDLEWARE === "true") &&
((currentEntry: Entry) => {
return applyMiddlewareLoaderFacade(
currentEntry,
tmpDir.path,
middlewareToLoad.filter(
// We dynamically filter the middleware depending on where we are bundling for
(m) =>
(targetConsumer === "dev" && m.dev !== false) ||
(m.publish && targetConsumer === "publish")
),
moduleCollector.plugin
);
}),
].filter(Boolean);
let inputEntry = entry;
for (const middlewareFn of middleware as MiddlewareFn[]) {
inputEntry = await middlewareFn(inputEntry);
}
// At this point, inputEntry points to the entry point we want to build.
const inject: string[] = [];
if (checkFetch) inject.push(checkedFetchFileToInject);
if (experimentalLocalStubCache) {
inject.push(
path.resolve(getBasePath(), "templates/experimental-local-cache-stubs.js")
);
}
const result = await esbuild.build({
entryPoints: [inputEntry.file],
bundle: true,
absWorkingDir: entry.directory,
outdir: destination,
inject,
external: ["__STATIC_CONTENT_MANIFEST"],
format: entry.format === "modules" ? "esm" : "iife",
target: "es2020",
sourcemap: true,
// Include a reference to the output folder in the sourcemap.
// This is omitted by default, but we need it to properly resolve source paths in error output.
sourceRoot: destination,
minify,
metafile: true,
conditions: ["worker", "browser"],
...(process.env.NODE_ENV && {
define: {
// use process.env["NODE_ENV" + ""] so that esbuild doesn't replace it
// when we do a build of wrangler. (re: https://github.com/cloudflare/wrangler2/issues/1477)
"process.env.NODE_ENV": `"${process.env["NODE_ENV" + ""]}"`,
...(nodeCompat ? { global: "globalThis" } : {}),
...(checkFetch ? { fetch: "checkedFetch" } : {}),
...options.define,
},
}),
loader: {
".js": "jsx",
".mjs": "jsx",
".cjs": "jsx",
},
plugins: [
// We run the moduleCollector plugin for service workers as part of the middleware loader
// so we only run here for modules or with no middleware to load
...(entry.format === "modules" || middlewareToLoad.length === 0
? [moduleCollector.plugin]
: []),
...(nodeCompat
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
: // we use checkForNodeBuiltinsPlugin to throw a nicer error
// if we find node builtins when nodeCompat isn't turned on
[checkForNodeBuiltinsPlugin]),
],
...(jsxFactory && { jsxFactory }),
...(jsxFragment && { jsxFragment }),
...(tsconfig && { tsconfig }),
watch,
});
const entryPointOutputs = Object.entries(result.metafile.outputs).filter(
([_path, output]) => output.entryPoint !== undefined
);
assert(
entryPointOutputs.length > 0,
`Cannot find entry-point "${entry.file}" in generated bundle.` +
listEntryPoints(entryPointOutputs)
);
assert(
entryPointOutputs.length < 2,
"More than one entry-point found for generated bundle." +
listEntryPoints(entryPointOutputs)
);
const entryPointExports = entryPointOutputs[0][1].exports;
const bundleType = entryPointExports.length > 0 ? "esm" : "commonjs";
const sourceMapPath = Object.keys(result.metafile.outputs).filter((_path) =>
_path.includes(".map")
)[0];
return {
modules: moduleCollector.modules,
resolvedEntryPointPath: path.resolve(
entry.directory,
entryPointOutputs[0][0]
),
bundleType,
stop: result.stop,
sourceMapPath,
};
}
/**
* A simple plugin to alias modules and mark them as external
*/
export function esbuildAliasExternalPlugin(
aliases: Record<string, string>
): esbuild.Plugin {
return {
name: "alias",
setup(build) {
build.onResolve({ filter: /.*/g }, (args) => {
// If it's the entrypoint, let it be as is
if (args.kind === "entry-point") {
return {
path: args.path,
};
}
// If it's not a recognised alias, then throw an error
if (!Object.keys(aliases).includes(args.path)) {
throw new Error("unrecognized module: " + args.path);
}
// Otherwise, return the alias
return {
path: aliases[args.path as keyof typeof aliases],
external: true,
};
});
},
};
}
/**
* A middleware that catches any thrown errors, and instead formats
* them to be rendered in a browser. This middleware is for demonstration
* purposes only, and is not intended to be used in production (or even dev!)
*/
async function applyFormatDevErrorsFacade(
entry: Entry,
tmpDirPath: string
): Promise<Entry> {
const targetPath = path.join(tmpDirPath, "format-dev-errors.entry.js");
await esbuild.build({
entryPoints: [
path.resolve(getBasePath(), "templates/format-dev-errors.ts"),
],
bundle: true,
sourcemap: true,
format: "esm",
plugins: [
esbuildAliasExternalPlugin({
__ENTRY_POINT__: entry.file,
}),
],
outfile: targetPath,
});
return {
...entry,
file: targetPath,
};
}
/**
* A facade that acts as a "middleware loader".
* Instead of needing to apply a facade for each individual middleware, this allows
* middleware to be written in a more traditional manner and then be applied all
* at once, requiring just two esbuild steps, rather than 1 per middleware.
*/
interface MiddlewareLoader {
path: string;
// By default all middleware will run on dev, but will not be run when published
publish?: boolean;
dev?: boolean;
}
async function applyMiddlewareLoaderFacade(
entry: Entry,
tmpDirPath: string,
middleware: MiddlewareLoader[], // a list of paths to middleware files
moduleCollectorPlugin: esbuild.Plugin
): Promise<Entry> {
// Firstly we need to insert the middleware array into the project,
// and then we load the middleware - this insertion and loading is
// different for each format.
// STEP 1: Insert the middleware
const targetPathInsertion = path.join(
tmpDirPath,
"middleware-insertion.entry.js"
);
// We need to import each of the middlewares, so we need to generate a
// random, unique identifier that we can use for the import.
// Middlewares are required to be default exports so we can import to any name.
const middlewareIdentifiers = middleware.map(
(_, index) => `__MIDDLEWARE_${index}__`
);
const dynamicFacadePath = path.join(
tmpDirPath,
"middleware-insertion-facade.js"
);
if (entry.format === "modules") {
// We use a facade to expose the required middleware alongside any user defined
// middleware on the worker object
const imports = middlewareIdentifiers
.map((m) => `import ${m} from "${m}";`)
.join("\n");
// write a file with all of the imports required
fs.writeFileSync(
dynamicFacadePath,
`import worker from "__ENTRY_POINT__";
${imports}
const facade = {
...worker,
middleware: [
${middlewareIdentifiers.join(",")}${middlewareIdentifiers.length > 0 ? "," : ""}
...(worker.middleware ? worker.middleware : []),
]
}
export * from "__ENTRY_POINT__";
export default facade;`
);
await esbuild.build({
entryPoints: [path.resolve(getBasePath(), dynamicFacadePath)],
bundle: true,
sourcemap: true,
format: "esm",
plugins: [
esbuildAliasExternalPlugin({
__ENTRY_POINT__: entry.file,
...Object.fromEntries(
middleware.map((val, index) => [
middlewareIdentifiers[index],
toUrlPath(path.resolve(getBasePath(), val.path)),
])
),
}),
],
outfile: targetPathInsertion,
});
} else {
// We handle service workers slightly differently as we have to overwrite
// the event listeners and reimplement them
await esbuild.build({
entryPoints: [entry.file],
bundle: true,
sourcemap: true,
define: {
"process.env.NODE_ENV": `"${process.env["NODE_ENV" + ""]}"`,
},
format: "esm",
outfile: targetPathInsertion,
plugins: [moduleCollectorPlugin],
});
const imports = middlewareIdentifiers
.map(
(m, i) =>
`import ${m} from "${toUrlPath(
path.resolve(getBasePath(), middleware[i].path)
)}";`
)
.join("\n");
// We add the new modules with imports and then register using the
// addMiddleware function (which gets rewritten in the next build step)
// We choose to run middleware inserted in wrangler before user inserted
// middleware in the stack
// To do this, we either need to execute the addMiddleware function first
// before any user middleware, or use a separate handling function.
// We choose to do the latter as to prepend, we would have to load the entire
// script into memory as a prepend function doesn't exist or work in the same
// way that an append function does.
fs.copyFileSync(targetPathInsertion, dynamicFacadePath);
fs.appendFileSync(
dynamicFacadePath,
`
${imports}
addMiddlewareInternal([${middlewareIdentifiers.join(",")}])
`
);
}
// STEP 2: Load the middleware
// We want to get the filename of the orginal entry point
let targetPathLoader = path.join(tmpDirPath, path.basename(entry.file));
if (path.extname(entry.file) === "") targetPathLoader += ".js";
const loaderPath =
entry.format === "modules"
? path.resolve(getBasePath(), "templates/middleware/loader-modules.ts")
: dynamicFacadePath;
await esbuild.build({
entryPoints: [loaderPath],
bundle: true,
sourcemap: true,
format: "esm",
...(entry.format === "service-worker"
? {
inject: [
path.resolve(getBasePath(), "templates/middleware/loader-sw.ts"),
],
define: {
addEventListener: "__facade_addEventListener__",
removeEventListener: "__facade_removeEventListener__",
dispatchEvent: "__facade_dispatchEvent__",
addMiddleware: "__facade_register__",
addMiddlewareInternal: "__facade_registerInternal__",
},
}
: {
plugins: [
esbuildAliasExternalPlugin({
__ENTRY_POINT__: targetPathInsertion,
"./common": path.resolve(
getBasePath(),
"templates/middleware/common.ts"
),
}),
],
}),
outfile: targetPathLoader,
});
return {
...entry,
file: targetPathLoader,
};
}
/**
* A middleware that serves static assets from a worker.
* This powers --assets / config.assets
*/
async function applyStaticAssetFacade(
entry: Entry,
tmpDirPath: string,
assets: StaticAssetsConfig
): Promise<Entry> {
const targetPath = path.join(tmpDirPath, "serve-static-assets.entry.js");
await esbuild.build({
entryPoints: [
path.resolve(getBasePath(), "templates/serve-static-assets.ts"),
],
bundle: true,
format: "esm",
sourcemap: true,
plugins: [
esbuildAliasExternalPlugin({
__ENTRY_POINT__: entry.file,
__KV_ASSET_HANDLER__: path.join(getBasePath(), "kv-asset-handler.js"),
__STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
}),
],
define: {
__CACHE_CONTROL_OPTIONS__: JSON.stringify(
typeof assets === "object"
? {
browserTTL:
assets.browser_TTL || 172800 /* 2 days: 2* 60 * 60 * 24 */,
bypassCache: assets.bypassCache,
}
: {}
),
__SERVE_SINGLE_PAGE_APP__: JSON.stringify(
typeof assets === "object" ? assets.serve_single_page_app : false
),
},
outfile: targetPath,
});
return {
...entry,
file: targetPath,
};
}
/**
* A middleware that enables service bindings to be used in dev,
* binding to other love wrangler dev instances
*/
async function applyMultiWorkerDevFacade(
entry: Entry,
tmpDirPath: string,
services: Config["services"],
workerDefinitions: WorkerRegistry
) {
const targetPath = path.join(tmpDirPath, "multiworker-dev-facade.entry.js");
const serviceMap = Object.fromEntries(
(services || []).map((serviceBinding) => [
serviceBinding.binding,
workerDefinitions[serviceBinding.service] || null,
])
);
await esbuild.build({
entryPoints: [
path.join(
getBasePath(),
entry.format === "modules"
? "templates/service-bindings-module-facade.js"
: "templates/service-bindings-sw-facade.js"
),
],
bundle: true,
sourcemap: true,
format: "esm",
plugins: [
esbuildAliasExternalPlugin({
__ENTRY_POINT__: entry.file,
}),
],
define: {
__WORKERS__: JSON.stringify(serviceMap),
},
outfile: targetPath,
});
return {
...entry,
file: targetPath,
};
}
/**
* A middleware that makes first party workers "work" in
* our dev environments. Is applied during wrangler dev
* when config.first_party_worker is true
*/
async function applyFirstPartyWorkerDevFacade(
entry: Entry,
tmpDirPath: string
) {
if (entry.format !== "modules") {
throw new Error(
"First party workers must be in the modules format. See https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/"
);
}
const targetPath = path.join(
tmpDirPath,
"first-party-worker-module-facade.entry.js"
);
await esbuild.build({
entryPoints: [
path.resolve(
getBasePath(),
"templates/first-party-worker-module-facade.ts"
),
],
bundle: true,
format: "esm",
sourcemap: true,
plugins: [
esbuildAliasExternalPlugin({
__ENTRY_POINT__: entry.file,
}),
],
outfile: targetPath,
});
return {
...entry,
file: targetPath,
};
}
/**
* A middleware that injects the beta D1 API in JS.
*
* This code be removed from here when the API is in Workers core,
* but moved inside Miniflare for simulating D1.
*/
async function applyD1BetaFacade(
entry: Entry,
tmpDirPath: string,
betaD1Shims: string[],
local: boolean
): Promise<Entry> {
const targetPath = path.join(tmpDirPath, "d1-beta-facade.entry.js");
await esbuild.build({
entryPoints: [path.resolve(getBasePath(), "templates/d1-beta-facade.js")],
bundle: true,
format: "esm",
sourcemap: true,
plugins: [
esbuildAliasExternalPlugin({
__ENTRY_POINT__: entry.file,
}),
],
define: {
__D1_IMPORTS__: JSON.stringify(betaD1Shims),
__LOCAL_MODE__: JSON.stringify(local),
},
outfile: targetPath,
});
return {
...entry,
file: targetPath,
};
}
/**
* Generate a string that describes the entry-points that were identified by esbuild.
*/
function listEntryPoints(
outputs: [string, ValueOf<esbuild.Metafile["outputs"]>][]
): string {
return outputs.map(([_input, output]) => output.entryPoint).join("\n");
}
type ValueOf<T> = T[keyof T];