From 599dab26891f7305ffbcfb000854f1f9629c6fcf Mon Sep 17 00:00:00 2001 From: screetBloom <15306225869@163.com> Date: Wed, 8 Sep 2021 10:35:07 +0800 Subject: [PATCH 1/2] feat: Optimizing the __require function --- internal/runtime/runtime.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index 8f5dddc793b..5d2dd342db9 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -117,8 +117,7 @@ func code(isES6 bool) string { export var __name = (target, value) => __defProp(target, 'name', { value, configurable: true }) // This fallback "require" function exists so that "typeof require" can naturally be "function" - export var __require = x => { - if (typeof require !== 'undefined') return require(x) + export var __require = typeof require !== 'undefined' ? require : x => { throw new Error('Dynamic require of "' + x + '" is not supported') } From 4efeadf9af560dce4108feae291a51f3dbfa8b43 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Wed, 8 Sep 2021 08:26:46 -0400 Subject: [PATCH 2/2] update test snapshots --- .../bundler/snapshots/snapshots_default.txt | 2 +- .../snapshots/snapshots_importstar.txt | 8 ++-- .../bundler/snapshots/snapshots_splitting.txt | 38 +++++++++---------- internal/bundler/snapshots/snapshots_ts.txt | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/internal/bundler/snapshots/snapshots_default.txt b/internal/bundler/snapshots/snapshots_default.txt index 34e491ffc77..5cd8c547985 100644 --- a/internal/bundler/snapshots/snapshots_default.txt +++ b/internal/bundler/snapshots/snapshots_default.txt @@ -1812,7 +1812,7 @@ console.log(shared_default); ================================================================================ TestMinifiedBundleCommonJS ---------- /out.js ---------- -var n=e(r=>{r.foo=function(){return 123}});var t=e((j,s)=>{s.exports={test:!0}});var{foo:c}=n();console.log(c(),t()); +var t=r(n=>{n.foo=function(){return 123}});var u=r((l,s)=>{s.exports={test:!0}});var{foo:c}=t();console.log(c(),u()); ================================================================================ TestMinifiedBundleES6 diff --git a/internal/bundler/snapshots/snapshots_importstar.txt b/internal/bundler/snapshots/snapshots_importstar.txt index 59281fe0faf..2e18229d5d2 100644 --- a/internal/bundler/snapshots/snapshots_importstar.txt +++ b/internal/bundler/snapshots/snapshots_importstar.txt @@ -111,11 +111,11 @@ var foo = 123; TestExportSelfCommonJSMinified ---------- /out.js ---------- // entry.js -var r = n((t, e) => { - e.exports = { foo: 123 }; - console.log(r()); +var l = n((c, r) => { + r.exports = { foo: 123 }; + console.log(l()); }); -module.exports = r(); +module.exports = l(); ================================================================================ TestExportSelfES6 diff --git a/internal/bundler/snapshots/snapshots_splitting.txt b/internal/bundler/snapshots/snapshots_splitting.txt index 679ca392f28..bc8b1fb0936 100644 --- a/internal/bundler/snapshots/snapshots_splitting.txt +++ b/internal/bundler/snapshots/snapshots_splitting.txt @@ -205,19 +205,19 @@ TestSplittingDynamicAndNotDynamicCommonJSIntoES6 import { __toModule, require_foo -} from "./chunk-NXTNQ63R.js"; +} from "./chunk-RPD6DYOE.js"; // entry.js var import_foo = __toModule(require_foo()); -import("./foo-246JBBCS.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b)); +import("./foo-5X5OD2SM.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b)); ----------- /out/foo-246JBBCS.js ---------- +---------- /out/foo-5X5OD2SM.js ---------- import { require_foo -} from "./chunk-NXTNQ63R.js"; +} from "./chunk-RPD6DYOE.js"; export default require_foo(); ----------- /out/chunk-NXTNQ63R.js ---------- +---------- /out/chunk-RPD6DYOE.js ---------- // foo.js var require_foo = __commonJS({ "foo.js"(exports) { @@ -259,15 +259,15 @@ export { ================================================================================ TestSplittingDynamicCommonJSIntoES6 ---------- /out/entry.js ---------- -import "./chunk-U6GWLSPU.js"; +import "./chunk-HSI22XR3.js"; // entry.js -import("./foo-XBEX5OV6.js").then(({ default: { bar } }) => console.log(bar)); +import("./foo-3BKJFAW3.js").then(({ default: { bar } }) => console.log(bar)); ----------- /out/foo-XBEX5OV6.js ---------- +---------- /out/foo-3BKJFAW3.js ---------- import { __commonJS -} from "./chunk-U6GWLSPU.js"; +} from "./chunk-HSI22XR3.js"; // foo.js var require_foo = __commonJS({ @@ -277,7 +277,7 @@ var require_foo = __commonJS({ }); export default require_foo(); ----------- /out/chunk-U6GWLSPU.js ---------- +---------- /out/chunk-HSI22XR3.js ---------- export { __commonJS }; @@ -328,7 +328,7 @@ TestSplittingHybridESMAndCJSIssue617 import { foo, init_a -} from "./chunk-NCWNCRTK.js"; +} from "./chunk-ZIIA2BWE.js"; init_a(); export { foo @@ -338,7 +338,7 @@ export { import { a_exports, init_a -} from "./chunk-NCWNCRTK.js"; +} from "./chunk-ZIIA2BWE.js"; // b.js var bar = (init_a(), a_exports); @@ -346,7 +346,7 @@ export { bar }; ----------- /out/chunk-NCWNCRTK.js ---------- +---------- /out/chunk-ZIIA2BWE.js ---------- // a.js var a_exports = {}; __export(a_exports, { @@ -399,7 +399,7 @@ TestSplittingMissingLazyExport ---------- /out/a.js ---------- import { foo -} from "./chunk-KFPHQBDL.js"; +} from "./chunk-XIEF2OJ5.js"; // a.js console.log(foo()); @@ -407,12 +407,12 @@ console.log(foo()); ---------- /out/b.js ---------- import { bar -} from "./chunk-KFPHQBDL.js"; +} from "./chunk-XIEF2OJ5.js"; // b.js console.log(bar()); ----------- /out/chunk-KFPHQBDL.js ---------- +---------- /out/chunk-XIEF2OJ5.js ---------- // empty.js var empty_exports = {}; __markAsModule(empty_exports); @@ -497,7 +497,7 @@ TestSplittingSharedCommonJSIntoES6 ---------- /out/a.js ---------- import { require_shared -} from "./chunk-4T6PTLAJ.js"; +} from "./chunk-727XETMI.js"; // a.js var { foo } = require_shared(); @@ -506,13 +506,13 @@ console.log(foo); ---------- /out/b.js ---------- import { require_shared -} from "./chunk-4T6PTLAJ.js"; +} from "./chunk-727XETMI.js"; // b.js var { foo } = require_shared(); console.log(foo); ----------- /out/chunk-4T6PTLAJ.js ---------- +---------- /out/chunk-727XETMI.js ---------- // shared.js var require_shared = __commonJS({ "shared.js"(exports) { diff --git a/internal/bundler/snapshots/snapshots_ts.txt b/internal/bundler/snapshots/snapshots_ts.txt index 972830cbc3b..5cc0171ecd4 100644 --- a/internal/bundler/snapshots/snapshots_ts.txt +++ b/internal/bundler/snapshots/snapshots_ts.txt @@ -316,7 +316,7 @@ console.log(a, b, c, d, e, real); ================================================================================ TestTSMinifiedBundleCommonJS ---------- /out.js ---------- -var n=e(r=>{r.foo=function(){return 123}});var t=e((j,s)=>{s.exports={test:!0}});var{foo:c}=n();console.log(c(),t()); +var t=r(n=>{n.foo=function(){return 123}});var u=r((l,s)=>{s.exports={test:!0}});var{foo:c}=t();console.log(c(),u()); ================================================================================ TestTSMinifiedBundleES6