From 8e14e25f26d00149720484ba340072a714aed772 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Tue, 8 Aug 2023 02:01:14 -0400 Subject: [PATCH] add the `__glob` runtime helper method --- .../snapshots/snapshots_default.txt | 20 +++++++------- .../snapshots/snapshots_splitting.txt | 26 +++++++++---------- internal/runtime/runtime.go | 7 +++++ scripts/js-api-tests.js | 4 +-- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/internal/bundler_tests/snapshots/snapshots_default.txt b/internal/bundler_tests/snapshots/snapshots_default.txt index c71533ba2af..34b5ec9d24c 100644 --- a/internal/bundler_tests/snapshots/snapshots_default.txt +++ b/internal/bundler_tests/snapshots/snapshots_default.txt @@ -3953,7 +3953,7 @@ copy import { __commonJS, __require -} from "./chunk-O3JHV3KG.js"; +} from "./chunk-WXLYCZIT.js"; // project/cjs.js var require_cjs = __commonJS({ @@ -3984,15 +3984,15 @@ console.log( e, __require("extern-cjs"), require_cjs(), - import("./dynamic-3QRP4J6Z.js") + import("./dynamic-TGITTCVZ.js") ); var exported; export { exported }; ----------- /out/dynamic-3QRP4J6Z.js ---------- -import "./chunk-O3JHV3KG.js"; +---------- /out/dynamic-TGITTCVZ.js ---------- +import "./chunk-WXLYCZIT.js"; // project/dynamic.js var dynamic_default = 5; @@ -4000,7 +4000,7 @@ export { dynamic_default as default }; ----------- /out/chunk-O3JHV3KG.js ---------- +---------- /out/chunk-WXLYCZIT.js ---------- export { __require, __commonJS @@ -4157,7 +4157,7 @@ d { "out/entry.js": { "imports": [ { - "path": "out/chunk-O3JHV3KG.js", + "path": "out/chunk-WXLYCZIT.js", "kind": "import-statement" }, { @@ -4179,7 +4179,7 @@ d { "external": true }, { - "path": "out/dynamic-3QRP4J6Z.js", + "path": "out/dynamic-TGITTCVZ.js", "kind": "dynamic-import" } ], @@ -4206,10 +4206,10 @@ d { }, "bytes": 642 }, - "out/dynamic-3QRP4J6Z.js": { + "out/dynamic-TGITTCVZ.js": { "imports": [ { - "path": "out/chunk-O3JHV3KG.js", + "path": "out/chunk-WXLYCZIT.js", "kind": "import-statement" } ], @@ -4224,7 +4224,7 @@ d { }, "bytes": 119 }, - "out/chunk-O3JHV3KG.js": { + "out/chunk-WXLYCZIT.js": { "imports": [], "exports": [ "__commonJS", diff --git a/internal/bundler_tests/snapshots/snapshots_splitting.txt b/internal/bundler_tests/snapshots/snapshots_splitting.txt index e257039a047..3976512cc97 100644 --- a/internal/bundler_tests/snapshots/snapshots_splitting.txt +++ b/internal/bundler_tests/snapshots/snapshots_splitting.txt @@ -259,19 +259,19 @@ TestSplittingDynamicAndNotDynamicCommonJSIntoES6 import { __toESM, require_foo -} from "./chunk-H2HESYLH.js"; +} from "./chunk-P5A5627R.js"; // entry.js var import_foo = __toESM(require_foo()); -import("./foo-OK6Y35CI.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b)); +import("./foo-N2LAC7TT.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b)); ----------- /out/foo-OK6Y35CI.js ---------- +---------- /out/foo-N2LAC7TT.js ---------- import { require_foo -} from "./chunk-H2HESYLH.js"; +} from "./chunk-P5A5627R.js"; export default require_foo(); ----------- /out/chunk-H2HESYLH.js ---------- +---------- /out/chunk-P5A5627R.js ---------- // foo.js var require_foo = __commonJS({ "foo.js"(exports) { @@ -314,9 +314,9 @@ export { TestSplittingDynamicCommonJSIntoES6 ---------- /out/entry.js ---------- // entry.js -import("./foo-PPQD77K4.js").then(({ default: { bar } }) => console.log(bar)); +import("./foo-LH6ELO2A.js").then(({ default: { bar } }) => console.log(bar)); ----------- /out/foo-PPQD77K4.js ---------- +---------- /out/foo-LH6ELO2A.js ---------- // foo.js var require_foo = __commonJS({ "foo.js"(exports) { @@ -371,7 +371,7 @@ TestSplittingHybridESMAndCJSIssue617 import { foo, init_a -} from "./chunk-NE324UYZ.js"; +} from "./chunk-P3Z6IJKQ.js"; init_a(); export { foo @@ -382,7 +382,7 @@ import { __toCommonJS, a_exports, init_a -} from "./chunk-NE324UYZ.js"; +} from "./chunk-P3Z6IJKQ.js"; // b.js var bar = (init_a(), __toCommonJS(a_exports)); @@ -390,7 +390,7 @@ export { bar }; ----------- /out/chunk-NE324UYZ.js ---------- +---------- /out/chunk-P3Z6IJKQ.js ---------- // a.js var a_exports = {}; __export(a_exports, { @@ -541,7 +541,7 @@ TestSplittingSharedCommonJSIntoES6 ---------- /out/a.js ---------- import { require_shared -} from "./chunk-KTJ3L72M.js"; +} from "./chunk-EJ4GJF3D.js"; // a.js var { foo } = require_shared(); @@ -550,13 +550,13 @@ console.log(foo); ---------- /out/b.js ---------- import { require_shared -} from "./chunk-KTJ3L72M.js"; +} from "./chunk-EJ4GJF3D.js"; // b.js var { foo } = require_shared(); console.log(foo); ----------- /out/chunk-KTJ3L72M.js ---------- +---------- /out/chunk-EJ4GJF3D.js ---------- // shared.js var require_shared = __commonJS({ "shared.js"(exports) { diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index ce6d1e65014..c3a0c69142f 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -133,6 +133,13 @@ func Source(unsupportedJSFeatures compat.JSFeature) logger.Source { throw Error('Dynamic require of "' + x + '" is not supported') }) + // This is used for glob imports + export var __glob = map => path => { + var fn = map[path] + if (fn) return fn() + throw new Error('Module not found in bundle: ' + path) + } + // For object rest patterns export var __restKey = key => typeof key === 'symbol' ? key : key + '' export var __objRest = (source, exclude) => { diff --git a/scripts/js-api-tests.js b/scripts/js-api-tests.js index 159b68817de..8e131467a6c 100644 --- a/scripts/js-api-tests.js +++ b/scripts/js-api-tests.js @@ -923,8 +923,8 @@ export { const names2 = result2.outputFiles.map(x => path.basename(x.path)).sort() // Check that the public path is included in chunk hashes but not asset hashes - assert.deepStrictEqual(names1, ['data-BYATPJRB.bin', 'in-IN5VRZMW.js']) - assert.deepStrictEqual(names2, ['data-BYATPJRB.bin', 'in-7HV645WS.js']) + assert.deepStrictEqual(names1, ['data-BYATPJRB.bin', 'in-7SVE3DTC.js']) + assert.deepStrictEqual(names2, ['data-BYATPJRB.bin', 'in-E6EBO534.js']) }, async fileLoaderPublicPath({ esbuild, testDir }) {