Skip to content

Commit

Permalink
Implemented preserve_file_names config (#4742)
Browse files Browse the repository at this point in the history
* 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 <ben@beny.pro>
Co-authored-by: Peter Bacon Darwin <pbacondarwin@cloudflare.com>
  • Loading branch information
3 people authored Feb 23, 2024
1 parent 64236b0 commit c2f3f1e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .changeset/happy-tools-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"wrangler": minor
---

feat: allow preserving file names when defining rules for non-js modules

The developer is now able to specify the `preserve_file_names property in wrangler.toml
which specifies whether Wrangler will preserve the file names additional modules that are
added to the deployment bundle of a Worker.

If not set to true, files will be named using the pattern ${fileHash}-${basename}.
For example, `34de60b44167af5c5a709e62a4e20c4f18c9e3b6-favicon.ico`.

Resolves [#4741](https://github.com/cloudflare/workers-sdk/issues/4741)
63 changes: 63 additions & 0 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7418,6 +7418,69 @@ addEventListener('fetch', event => {});`
`);
});

it("should be able to preserve file names when defining rules for uploading non-js modules (sw)", async () => {
writeWranglerToml({
rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
preserve_file_names: true,
});
fs.writeFileSync("./index.js", `import TEXT from './text.file';`);
fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
mockSubDomainRequest();
mockUploadWorkerRequest({
expectedType: "sw",
expectedBindings: [
{
name: "__text_file",
part: "__text_file",
type: "text_blob",
},
],
expectedModules: {
__text_file: "SOME TEXT CONTENT",
},
});
await runWrangler("deploy index.js");
expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Deployment ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

it("should be able to preserve file names when defining rules for uploading non-js modules (esm)", async () => {
writeWranglerToml({
rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
preserve_file_names: true,
});
fs.writeFileSync(
"./index.js",
`import TEXT from './text.file'; export default {};`
);
fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
mockSubDomainRequest();
mockUploadWorkerRequest({
expectedType: "esm",
expectedBindings: [],
expectedModules: {
"./text.file": "SOME TEXT CONTENT",
},
});
await runWrangler("deploy index.js");
expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Deployment ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

describe("inject process.env.NODE_ENV", () => {
let actualProcessEnvNodeEnv: string | undefined;
beforeEach(() => {
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ interface EnvironmentInheritable {
*/
find_additional_modules: boolean | undefined;

/**
* Determines whether Wrangler will preserve bundled file names.
* Defaults to false.
* If left unset, files will be named using the pattern ${fileHash}-${basename},
* for example, `34de60b44167af5c5a709e62a4e20c4f18c9e3b6-favicon.ico`.
*/
preserve_file_names: boolean | undefined;

/**
* The directory in which module rules should be evaluated when including additional files into a worker deployment.
* This defaults to the directory containing the `main` entry point of the worker if not specified.
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,14 @@ function normalizeAndValidateEnvironment(
isBoolean,
undefined
),
preserve_file_names: inheritable(
diagnostics,
topLevelEnv,
rawEnv,
"preserve_file_names",
isBoolean,
undefined
),
base_dir: normalizeAndValidateBaseDirField(
configPath,
inheritable(
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
// `findAdditionalModules` always defaults to `false`
findAdditionalModules: config.find_additional_modules ?? false,
rules: props.rules,
preserveFileNames: config.preserve_file_names ?? false,
});

const { modules, dependencies, resolvedEntryPointPath, bundleType } =
Expand Down
6 changes: 4 additions & 2 deletions packages/wrangler/src/deployment-bundle/module-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ export function createModuleCollector(props: {
.createHash("sha1")
.update(fileContent)
.digest("hex");
const fileName = `./${fileHash}-${path.basename(args.path)}`;
const fileName = props.preserveFileNames
? args.path
: `./${fileHash}-${path.basename(args.path)}`;

const { rule } =
rulesMatchers.find(({ regex }) => regex.test(fileName)) || {};
Expand Down Expand Up @@ -338,7 +340,7 @@ export function createModuleCollector(props: {
.update(fileContent)
.digest("hex");
const fileName = props.preserveFileNames
? filePath
? args.path
: `./${fileHash}-${path.basename(args.path)}`;

// add the module to the array
Expand Down

0 comments on commit c2f3f1e

Please sign in to comment.