Skip to content

Commit

Permalink
fix "__require" to have no side effects
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Sep 22, 2021
1 parent 0b90b0a commit 61155d5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 42 deletions.
17 changes: 9 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@
Handling all of these edge cases is only possible with the [Proxy API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). So the implementation of `__require` now looks like this:

```js
var __require = typeof require !== 'undefined' ? require :
(x => typeof Proxy !== 'undefined' ? new Proxy(x, {
get: (a, b) => (typeof require !== 'undefined' ? require : a)[b],
}) : x)(function(x) {
if (typeof require !== 'undefined')
return require.apply(this, arguments);
throw new Error('Dynamic require of "' + x + '" is not supported');
});
var __require = (x =>
typeof require !== 'undefined' ? require :
typeof Proxy !== 'undefined' ? new Proxy(x, {
get: (a, b) => (typeof require !== 'undefined' ? require : a)[b]
}) : x
)(function(x) {
if (typeof require !== 'undefined') return require.apply(this, arguments);
throw new Error('Dynamic require of "' + x + '" is not supported');
});
```

* Consider `typeof x` to have no side effects
Expand Down
12 changes: 6 additions & 6 deletions 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 t=r(n=>{n.foo=function(){return 123}});var u=r((q,c)=>{c.exports={test:!0}});var{foo:f}=t();console.log(f(),u());
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());

================================================================================
TestMinifiedBundleES6
Expand All @@ -1833,21 +1833,21 @@ import("foo");import(foo());
TestMinifiedExportsAndModuleFormatCommonJS
---------- /out.js ----------
// foo/test.js
var e = {};
f(e, {
var t = {};
f(t, {
foo: () => l
});
var l = 123;

// bar/test.js
var s = {};
f(s, {
var r = {};
f(r, {
bar: () => m
});
var m = 123;

// entry.js
console.log(exports, module.exports, e, s);
console.log(exports, module.exports, t, r);

================================================================================
TestMinifyArguments
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 l = s((f, r) => {
r.exports = { foo: 123 };
console.log(l());
var r = n((t, e) => {
e.exports = { foo: 123 };
console.log(r());
});
module.exports = l();
module.exports = r();

================================================================================
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-RPD6DYOE.js";
} from "./chunk-NXTNQ63R.js";

// entry.js
var import_foo = __toModule(require_foo());
import("./foo-5X5OD2SM.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b));
import("./foo-246JBBCS.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b));

---------- /out/foo-5X5OD2SM.js ----------
---------- /out/foo-246JBBCS.js ----------
import {
require_foo
} from "./chunk-RPD6DYOE.js";
} from "./chunk-NXTNQ63R.js";
export default require_foo();

---------- /out/chunk-RPD6DYOE.js ----------
---------- /out/chunk-NXTNQ63R.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-HSI22XR3.js";
import "./chunk-U6GWLSPU.js";

// entry.js
import("./foo-3BKJFAW3.js").then(({ default: { bar } }) => console.log(bar));
import("./foo-XBEX5OV6.js").then(({ default: { bar } }) => console.log(bar));

---------- /out/foo-3BKJFAW3.js ----------
---------- /out/foo-XBEX5OV6.js ----------
import {
__commonJS
} from "./chunk-HSI22XR3.js";
} from "./chunk-U6GWLSPU.js";

// foo.js
var require_foo = __commonJS({
Expand All @@ -277,7 +277,7 @@ var require_foo = __commonJS({
});
export default require_foo();

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

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

---------- /out/chunk-ZIIA2BWE.js ----------
---------- /out/chunk-NCWNCRTK.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-XIEF2OJ5.js";
} from "./chunk-KFPHQBDL.js";

// a.js
console.log(foo());

---------- /out/b.js ----------
import {
bar
} from "./chunk-XIEF2OJ5.js";
} from "./chunk-KFPHQBDL.js";

// b.js
console.log(bar());

---------- /out/chunk-XIEF2OJ5.js ----------
---------- /out/chunk-KFPHQBDL.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-727XETMI.js";
} from "./chunk-4T6PTLAJ.js";

// a.js
var { foo } = require_shared();
Expand All @@ -506,13 +506,13 @@ console.log(foo);
---------- /out/b.js ----------
import {
require_shared
} from "./chunk-727XETMI.js";
} from "./chunk-4T6PTLAJ.js";

// b.js
var { foo } = require_shared();
console.log(foo);

---------- /out/chunk-727XETMI.js ----------
---------- /out/chunk-4T6PTLAJ.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 t=r(n=>{n.foo=function(){return 123}});var u=r((q,c)=>{c.exports={test:!0}});var{foo:f}=t();console.log(f(),u());
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());

================================================================================
TestTSMinifiedBundleES6
Expand Down
11 changes: 7 additions & 4 deletions internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,13 @@ func code(isES6 bool) string {
// shim to fall back to "globalThis.require" even if it's defined later
// (including property accesses such as "require.resolve") so we need to
// use a proxy (issue #1614).
export var __require = typeof require !== 'undefined' ? require :
/* @__PURE__ */ (x => typeof Proxy !== 'undefined' ? new Proxy(x, {
get: (a, b) => (typeof require !== 'undefined' ? require : a)[b],
}) : x)(function(x) {
export var __require =
/* @__PURE__ */ (x =>
typeof require !== 'undefined' ? require :
typeof Proxy !== 'undefined' ? new Proxy(x, {
get: (a, b) => (typeof require !== 'undefined' ? require : a)[b]
}) : x
)(function(x) {
if (typeof require !== 'undefined') return require.apply(this, arguments)
throw new Error('Dynamic require of "' + x + '" is not supported')
})
Expand Down

0 comments on commit 61155d5

Please sign in to comment.