From 27e8385167a4ef6eff9bb91cf0203184fbd16915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Somhairle=20MacLe=C3=B2id?= Date: Thu, 3 Oct 2024 00:10:38 +0100 Subject: [PATCH] Handle more module declaration cases (#6792) --- .changeset/popular-horses-hide.md | 5 +++++ .../src/__tests__/type-generation.test.ts | 17 +++++++++++++++++ packages/wrangler/src/type-generation/index.ts | 17 ++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 .changeset/popular-horses-hide.md diff --git a/.changeset/popular-horses-hide.md b/.changeset/popular-horses-hide.md new file mode 100644 index 000000000000..1fae2da8fa6f --- /dev/null +++ b/.changeset/popular-horses-hide.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +fix: Handle more module declaration cases diff --git a/packages/wrangler/src/__tests__/type-generation.test.ts b/packages/wrangler/src/__tests__/type-generation.test.ts index c41cd5ff2090..45be49a4c20b 100644 --- a/packages/wrangler/src/__tests__/type-generation.test.ts +++ b/packages/wrangler/src/__tests__/type-generation.test.ts @@ -1,6 +1,7 @@ import * as fs from "fs"; import * as TOML from "@iarna/toml"; import { + constructTSModuleGlob, constructType, constructTypeKey, generateImportSpecifier, @@ -48,6 +49,22 @@ describe("constructTypeKey", () => { }); }); +describe("constructTSModuleGlob() should return a valid TS glob ", () => { + it.each([ + ["**/*.wasm", "*.wasm"], + ["**/*.txt", "*.txt"], + ["**/foo", "*/foo"], + ["**/*foo", "*foo"], + ["file.foo", "file.foo"], + ["folder/file.foo", "folder/file.foo"], + ["folder/*", "folder/*"], + ["folder/**", "folder/*"], + ["folder/**/*", "folder/*"], + ])("$1 -> $2", (from, to) => { + expect(constructTSModuleGlob(from)).toBe(to); + }); +}); + describe("constructType", () => { it("should return a valid type", () => { expect(constructType("valid", "string")).toBe("valid: string;"); diff --git a/packages/wrangler/src/type-generation/index.ts b/packages/wrangler/src/type-generation/index.ts index 5ee58a73677a..905810a79db6 100644 --- a/packages/wrangler/src/type-generation/index.ts +++ b/packages/wrangler/src/type-generation/index.ts @@ -165,6 +165,19 @@ export function constructTypeKey(key: string) { return `"${key}"`; } +export function constructTSModuleGlob(glob: string) { + // Exact module reference, don't transform + if (!glob.includes("*")) { + return glob; + // Usually something like **/*.wasm. Turn into *.wasm + } else if (glob.includes(".")) { + return `*.${glob.split(".").at(-1)}`; + } else { + // Replace common patterns + return glob.replace("**/*", "*").replace("**/", "*/").replace("/**", "/*"); + } +} + /** * Generate a import specifier from one module to another */ @@ -426,9 +439,7 @@ async function generateTypes( moduleTypeMap[ruleObject.type as keyof typeof moduleTypeMap]; if (typeScriptType !== undefined) { ruleObject.globs.forEach((glob) => { - modulesTypeStructure.push(`declare module "*.${glob - .split(".") - .at(-1)}" { + modulesTypeStructure.push(`declare module "${constructTSModuleGlob(glob)}" { const value: ${typeScriptType}; export default value; }`);