Skip to content

Commit c2f3f1e

Browse files
benycodesBenypetebacondarwin
authored
Implemented preserve_file_names config (#4742)
* Update module-collection.ts - add preserveFileNames to legacy Added the preserveFileNames option to legacy rules * Update environment.ts - added preserve_file_names * Update deploy.ts - added config.preserve_file_names Added option config.preserve_file_names to moduleCollector * Update validation.ts - added preserve_file_names * Fixed formatting issues * Preserve file name tests * Add changeset * do not pass whole path to filename --------- Co-authored-by: Beny <[email protected]> Co-authored-by: Peter Bacon Darwin <[email protected]>
1 parent 64236b0 commit c2f3f1e

File tree

6 files changed

+98
-2
lines changed

6 files changed

+98
-2
lines changed

.changeset/happy-tools-approve.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
feat: allow preserving file names when defining rules for non-js modules
6+
7+
The developer is now able to specify the `preserve_file_names property in wrangler.toml
8+
which specifies whether Wrangler will preserve the file names additional modules that are
9+
added to the deployment bundle of a Worker.
10+
11+
If not set to true, files will be named using the pattern ${fileHash}-${basename}.
12+
For example, `34de60b44167af5c5a709e62a4e20c4f18c9e3b6-favicon.ico`.
13+
14+
Resolves [#4741](https://github.com/cloudflare/workers-sdk/issues/4741)

packages/wrangler/src/__tests__/deploy.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7418,6 +7418,69 @@ addEventListener('fetch', event => {});`
74187418
`);
74197419
});
74207420

7421+
it("should be able to preserve file names when defining rules for uploading non-js modules (sw)", async () => {
7422+
writeWranglerToml({
7423+
rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
7424+
preserve_file_names: true,
7425+
});
7426+
fs.writeFileSync("./index.js", `import TEXT from './text.file';`);
7427+
fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
7428+
mockSubDomainRequest();
7429+
mockUploadWorkerRequest({
7430+
expectedType: "sw",
7431+
expectedBindings: [
7432+
{
7433+
name: "__text_file",
7434+
part: "__text_file",
7435+
type: "text_blob",
7436+
},
7437+
],
7438+
expectedModules: {
7439+
__text_file: "SOME TEXT CONTENT",
7440+
},
7441+
});
7442+
await runWrangler("deploy index.js");
7443+
expect(std.out).toMatchInlineSnapshot(`
7444+
"Total Upload: xx KiB / gzip: xx KiB
7445+
Uploaded test-name (TIMINGS)
7446+
Published test-name (TIMINGS)
7447+
https://test-name.test-sub-domain.workers.dev
7448+
Current Deployment ID: Galaxy-Class"
7449+
`);
7450+
expect(std.err).toMatchInlineSnapshot(`""`);
7451+
expect(std.warn).toMatchInlineSnapshot(`""`);
7452+
});
7453+
7454+
it("should be able to preserve file names when defining rules for uploading non-js modules (esm)", async () => {
7455+
writeWranglerToml({
7456+
rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
7457+
preserve_file_names: true,
7458+
});
7459+
fs.writeFileSync(
7460+
"./index.js",
7461+
`import TEXT from './text.file'; export default {};`
7462+
);
7463+
fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
7464+
mockSubDomainRequest();
7465+
mockUploadWorkerRequest({
7466+
expectedType: "esm",
7467+
expectedBindings: [],
7468+
expectedModules: {
7469+
"./text.file": "SOME TEXT CONTENT",
7470+
},
7471+
});
7472+
await runWrangler("deploy index.js");
7473+
expect(std.out).toMatchInlineSnapshot(`
7474+
"Total Upload: xx KiB / gzip: xx KiB
7475+
Uploaded test-name (TIMINGS)
7476+
Published test-name (TIMINGS)
7477+
https://test-name.test-sub-domain.workers.dev
7478+
Current Deployment ID: Galaxy-Class"
7479+
`);
7480+
expect(std.err).toMatchInlineSnapshot(`""`);
7481+
expect(std.warn).toMatchInlineSnapshot(`""`);
7482+
});
7483+
74217484
describe("inject process.env.NODE_ENV", () => {
74227485
let actualProcessEnvNodeEnv: string | undefined;
74237486
beforeEach(() => {

packages/wrangler/src/config/environment.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ interface EnvironmentInheritable {
9393
*/
9494
find_additional_modules: boolean | undefined;
9595

96+
/**
97+
* Determines whether Wrangler will preserve bundled file names.
98+
* Defaults to false.
99+
* If left unset, files will be named using the pattern ${fileHash}-${basename},
100+
* for example, `34de60b44167af5c5a709e62a4e20c4f18c9e3b6-favicon.ico`.
101+
*/
102+
preserve_file_names: boolean | undefined;
103+
96104
/**
97105
* The directory in which module rules should be evaluated when including additional files into a worker deployment.
98106
* This defaults to the directory containing the `main` entry point of the worker if not specified.

packages/wrangler/src/config/validation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,14 @@ function normalizeAndValidateEnvironment(
11541154
isBoolean,
11551155
undefined
11561156
),
1157+
preserve_file_names: inheritable(
1158+
diagnostics,
1159+
topLevelEnv,
1160+
rawEnv,
1161+
"preserve_file_names",
1162+
isBoolean,
1163+
undefined
1164+
),
11571165
base_dir: normalizeAndValidateBaseDirField(
11581166
configPath,
11591167
inheritable(

packages/wrangler/src/deploy/deploy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
481481
// `findAdditionalModules` always defaults to `false`
482482
findAdditionalModules: config.find_additional_modules ?? false,
483483
rules: props.rules,
484+
preserveFileNames: config.preserve_file_names ?? false,
484485
});
485486

486487
const { modules, dependencies, resolvedEntryPointPath, bundleType } =

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ export function createModuleCollector(props: {
223223
.createHash("sha1")
224224
.update(fileContent)
225225
.digest("hex");
226-
const fileName = `./${fileHash}-${path.basename(args.path)}`;
226+
const fileName = props.preserveFileNames
227+
? args.path
228+
: `./${fileHash}-${path.basename(args.path)}`;
227229

228230
const { rule } =
229231
rulesMatchers.find(({ regex }) => regex.test(fileName)) || {};
@@ -338,7 +340,7 @@ export function createModuleCollector(props: {
338340
.update(fileContent)
339341
.digest("hex");
340342
const fileName = props.preserveFileNames
341-
? filePath
343+
? args.path
342344
: `./${fileHash}-${path.basename(args.path)}`;
343345

344346
// add the module to the array

0 commit comments

Comments
 (0)