Skip to content

Commit 674fbc1

Browse files
invert module collector config - no longer need to return it as it is passed in
1 parent 002f4b1 commit 674fbc1

File tree

10 files changed

+186
-106
lines changed

10 files changed

+186
-106
lines changed

packages/wrangler/src/api/pages/deploy.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ export async function deploy({
277277
stop: undefined,
278278
resolvedEntryPointPath: _workerPath,
279279
bundleType: "esm",
280-
moduleCollector: undefined,
281280
};
282281
}
283282
}

packages/wrangler/src/config/environment.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,19 @@ interface EnvironmentInheritable {
7777
main: string | undefined;
7878

7979
/**
80-
* The directory in which module rules should be evaluated in a `--no-bundle` worker
81-
* This defaults to dirname(main) when left undefined
80+
* If true then Wrangler will traverse the file tree below `base_dir`;
81+
* Any files that match `rules` will be included in the deployed worker.
82+
* Defaults to true if `no_bundle` is true, otherwise false.
83+
*
84+
* @inheritable
85+
*/
86+
find_additional_modules: boolean | undefined;
87+
88+
/**
89+
* The directory in which module rules should be evaluated when including additional files into a worker deployment.
90+
* This defaults to the directory containing the `main` entry point of the worker if not specified.
91+
*
92+
* @inheritable
8293
*/
8394
base_dir: string | undefined;
8495

packages/wrangler/src/config/validation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,14 @@ function normalizeAndValidateEnvironment(
11191119
),
11201120
deprecatedUpload
11211121
),
1122+
find_additional_modules: inheritable(
1123+
diagnostics,
1124+
topLevelEnv,
1125+
rawEnv,
1126+
"find_additional_modules",
1127+
isBoolean,
1128+
undefined
1129+
),
11221130
base_dir: normalizeAndValidateBaseDirField(
11231131
configPath,
11241132
inheritable(

packages/wrangler/src/deploy/deploy.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import {
1414
import { getBundleType } from "../deployment-bundle/bundle-type";
1515
import { createWorkerUploadForm } from "../deployment-bundle/create-worker-upload-form";
1616
import findAdditionalModules from "../deployment-bundle/find-additional-modules";
17+
import {
18+
createModuleCollector,
19+
getWrangler1xLegacyModuleReferences,
20+
} from "../deployment-bundle/module-collection";
1721
import { addHyphens } from "../deployments";
1822
import { confirm } from "../dialogs";
1923
import { getMigrationsToUpload } from "../durable";
@@ -453,6 +457,16 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
453457
);
454458
}
455459

460+
const entryDirectory = path.dirname(props.entry.file);
461+
const moduleCollector = createModuleCollector({
462+
wrangler1xLegacyModuleReferences: getWrangler1xLegacyModuleReferences(
463+
entryDirectory,
464+
props.entry.file
465+
),
466+
format: props.entry.format,
467+
rules: props.rules,
468+
});
469+
456470
const { modules, dependencies, resolvedEntryPointPath, bundleType } =
457471
props.noBundle
458472
? {
@@ -465,6 +479,10 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
465479
props.entry,
466480
typeof destination === "string" ? destination : destination.path,
467481
{
482+
bundle: true,
483+
findAdditionalModules: false,
484+
additionalModules: [],
485+
moduleCollector,
468486
serveAssetsFromWorker:
469487
!props.isWorkersSite && Boolean(props.assetPaths),
470488
doBindings: config.durable_objects.bindings,

packages/wrangler/src/deployment-bundle/bundle.ts

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getEntryPointFromMetafile } from "./entry-point-from-metafile";
1515
import { cloudflareInternalPlugin } from "./esbuild-plugins/cloudflare-internal";
1616
import { configProviderPlugin } from "./esbuild-plugins/config-provider";
1717
import { nodejsCompatPlugin } from "./esbuild-plugins/nodejs-compat";
18-
import createModuleCollector from "./module-collection";
18+
import { noopModuleCollector } from "./module-collection";
1919
import type { Config } from "../config";
2020
import type { DurableObjectBindings } from "../config/environment";
2121
import type { WorkerRegistry } from "../dev-registry";
@@ -47,12 +47,17 @@ export type BundleResult = {
4747
stop: (() => void) | undefined;
4848
sourceMapPath?: string | undefined;
4949
sourceMapMetadata?: SourceMapMetadata | undefined;
50-
moduleCollector: ModuleCollector | undefined;
5150
};
5251

5352
export type BundleOptions = {
5453
// When `bundle` is set to false, we apply shims to the Worker, but won't pull in any imports
55-
bundle?: boolean;
54+
bundle: boolean;
55+
// When `findAdditionalModules is true we will also traverse the file tree to find modules that match module rules.
56+
findAdditionalModules: boolean;
57+
// Known additional modules provided by the outside.
58+
additionalModules: CfModule[];
59+
// A module collector enables you to observe what modules are in the Worker.
60+
moduleCollector: ModuleCollector;
5661
serveAssetsFromWorker: boolean;
5762
assets?: Config["assets"];
5863
bypassAssetCache?: boolean;
@@ -77,9 +82,6 @@ export type BundleOptions = {
7782
loader?: Record<string, string>;
7883
sourcemap?: esbuild.CommonOptions["sourcemap"];
7984
plugins?: esbuild.Plugin[];
80-
additionalModules?: CfModule[];
81-
// TODO: Rip these out https://github.com/cloudflare/workers-sdk/issues/2153
82-
disableModuleCollection?: boolean;
8385
isOutfile?: boolean;
8486
forPages?: boolean;
8587
};
@@ -91,13 +93,14 @@ export async function bundleWorker(
9193
entry: Entry,
9294
destination: string,
9395
{
94-
bundle = true,
96+
bundle,
97+
moduleCollector = noopModuleCollector,
98+
additionalModules = [],
9599
serveAssetsFromWorker,
96100
doBindings,
97101
jsxFactory,
98102
jsxFragment,
99103
entryName,
100-
rules,
101104
watch,
102105
tsconfig,
103106
minify,
@@ -115,10 +118,8 @@ export async function bundleWorker(
115118
loader,
116119
sourcemap,
117120
plugins,
118-
disableModuleCollection,
119121
isOutfile,
120122
forPages,
121-
additionalModules = [],
122123
}: BundleOptions
123124
): Promise<BundleResult> {
124125
// We create a temporary directory for any one-off files we
@@ -128,33 +129,6 @@ export async function bundleWorker(
128129

129130
const entryFile = entry.file;
130131

131-
const entryDirectory = path.dirname(entryFile);
132-
let moduleCollector = createModuleCollector({
133-
wrangler1xlegacyModuleReferences: {
134-
rootDirectory: entryDirectory,
135-
fileNames: new Set(
136-
fs
137-
.readdirSync(entryDirectory, { withFileTypes: true })
138-
.filter(
139-
(dirEntry) =>
140-
dirEntry.isFile() && dirEntry.name !== path.basename(entryFile)
141-
)
142-
.map((dirEnt) => dirEnt.name)
143-
),
144-
},
145-
format: entry.format,
146-
rules,
147-
});
148-
if (disableModuleCollection) {
149-
moduleCollector = {
150-
modules: [],
151-
plugin: {
152-
name: moduleCollector.plugin.name,
153-
setup: () => {},
154-
},
155-
};
156-
}
157-
158132
// At this point, we take the opportunity to "wrap" the worker with middleware.
159133
const middlewareToLoad: MiddlewareLoader[] = [];
160134

@@ -394,6 +368,5 @@ export async function bundleWorker(
394368
tmpDir: tmpDir.path,
395369
entryDirectory: entry.directory,
396370
},
397-
moduleCollector,
398371
};
399372
}

packages/wrangler/src/deployment-bundle/module-collection.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import crypto from "node:crypto";
2+
import { readdirSync } from "node:fs";
23
import { readFile } from "node:fs/promises";
34
import path from "node:path";
45
import globToRegExp from "glob-to-regexp";
@@ -90,12 +91,20 @@ export type ModuleCollector = {
9091
plugin: esbuild.Plugin;
9192
};
9293

93-
export default function createModuleCollector(props: {
94+
export const noopModuleCollector = {
95+
modules: [],
96+
plugin: {
97+
name: "wrangler-module-collector",
98+
setup: () => {},
99+
},
100+
};
101+
102+
export function createModuleCollector(props: {
94103
format: CfScriptFormat;
95104
rules?: Config["rules"];
96105
// a collection of "legacy" style module references, which are just file names
97106
// we will eventually deprecate this functionality, hence the verbose greppable name
98-
wrangler1xlegacyModuleReferences: {
107+
wrangler1xLegacyModuleReferences?: {
99108
rootDirectory: string;
100109
fileNames: Set<string>;
101110
};
@@ -129,12 +138,15 @@ export default function createModuleCollector(props: {
129138
});
130139
});
131140

132-
if (props.wrangler1xlegacyModuleReferences.fileNames.size > 0) {
141+
if (
142+
props.wrangler1xLegacyModuleReferences &&
143+
props.wrangler1xLegacyModuleReferences.fileNames.size > 0
144+
) {
133145
build.onResolve(
134146
{
135147
filter: new RegExp(
136148
"^(" +
137-
[...props.wrangler1xlegacyModuleReferences.fileNames]
149+
[...props.wrangler1xLegacyModuleReferences.fileNames]
138150
.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
139151
.join("|") +
140152
")$"
@@ -160,7 +172,8 @@ export default function createModuleCollector(props: {
160172
// take the file and massage it to a
161173
// transportable/manageable format
162174
const filePath = path.join(
163-
props.wrangler1xlegacyModuleReferences.rootDirectory,
175+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
176+
props.wrangler1xLegacyModuleReferences!.rootDirectory,
164177
args.path
165178
);
166179
const fileContent = await readFile(filePath);
@@ -269,3 +282,20 @@ export default function createModuleCollector(props: {
269282
},
270283
};
271284
}
285+
286+
export function getWrangler1xLegacyModuleReferences(
287+
rootDirectory: string,
288+
entryPath: string
289+
) {
290+
return {
291+
rootDirectory,
292+
fileNames: new Set(
293+
readdirSync(rootDirectory, { withFileTypes: true })
294+
.filter(
295+
(dirEntry) =>
296+
dirEntry.isFile() && dirEntry.name !== path.basename(entryPath)
297+
)
298+
.map((dirEnt) => dirEnt.name)
299+
),
300+
};
301+
}

packages/wrangler/src/dev/start-server.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import { bundleWorker } from "../deployment-bundle/bundle";
77
import { getBundleType } from "../deployment-bundle/bundle-type";
88
import { dedupeModulesByName } from "../deployment-bundle/dedupe-modules";
99
import findAdditionalModules from "../deployment-bundle/find-additional-modules";
10+
import {
11+
createModuleCollector,
12+
getWrangler1xLegacyModuleReferences,
13+
noopModuleCollector,
14+
} from "../deployment-bundle/module-collection";
1015
import { runCustomBuild } from "../deployment-bundle/run-custom-build";
1116
import {
1217
getBoundRegisteredWorkers,
@@ -233,11 +238,25 @@ async function runEsbuild({
233238
]);
234239
}
235240

241+
const entryDirectory = path.dirname(entry.file);
242+
const moduleCollector = noBundle
243+
? noopModuleCollector
244+
: createModuleCollector({
245+
wrangler1xLegacyModuleReferences: getWrangler1xLegacyModuleReferences(
246+
entryDirectory,
247+
entry.file
248+
),
249+
format: entry.format,
250+
rules,
251+
});
252+
236253
const bundleResult =
237254
processEntrypoint || !noBundle
238255
? await bundleWorker(entry, destination, {
239256
bundle: !noBundle,
240-
disableModuleCollection: noBundle,
257+
findAdditionalModules: false,
258+
additionalModules,
259+
moduleCollector,
241260
serveAssetsFromWorker,
242261
jsxFactory,
243262
jsxFragment,
@@ -256,7 +275,6 @@ async function runEsbuild({
256275
targetConsumer: "dev", // We are starting a dev server
257276
testScheduled,
258277
doBindings,
259-
additionalModules,
260278
})
261279
: undefined;
262280

0 commit comments

Comments
 (0)