Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimizing the __require function #1580

Merged
merged 2 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/bundler/snapshots/snapshots_importstar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 19 additions & 19 deletions internal/bundler/snapshots/snapshots_splitting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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({
Expand All @@ -277,7 +277,7 @@ var require_foo = __commonJS({
});
export default require_foo();

---------- /out/chunk-U6GWLSPU.js ----------
---------- /out/chunk-HSI22XR3.js ----------
export {
__commonJS
};
Expand Down Expand Up @@ -328,7 +328,7 @@ TestSplittingHybridESMAndCJSIssue617
import {
foo,
init_a
} from "./chunk-NCWNCRTK.js";
} from "./chunk-ZIIA2BWE.js";
init_a();
export {
foo
Expand All @@ -338,15 +338,15 @@ export {
import {
a_exports,
init_a
} from "./chunk-NCWNCRTK.js";
} from "./chunk-ZIIA2BWE.js";

// b.js
var bar = (init_a(), a_exports);
export {
bar
};

---------- /out/chunk-NCWNCRTK.js ----------
---------- /out/chunk-ZIIA2BWE.js ----------
// a.js
var a_exports = {};
__export(a_exports, {
Expand Down Expand Up @@ -399,20 +399,20 @@ TestSplittingMissingLazyExport
---------- /out/a.js ----------
import {
foo
} from "./chunk-KFPHQBDL.js";
} from "./chunk-XIEF2OJ5.js";

// a.js
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);
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/bundler/snapshots/snapshots_ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

Expand Down