Skip to content

Commit

Permalink
refactor(runtime): unwrap the function in ir (web-infra-dev#2860)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi authored and siyou committed May 14, 2023
1 parent 1847555 commit db4fe18
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 106 deletions.
84 changes: 41 additions & 43 deletions crates/rspack/tests/fixtures/dynamic-import/expected/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,56 +39,54 @@ __webpack_require__.e = function (chunkId) {
})();
// ir
(function() {
(function () {
function _getRequireCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
function _getRequireCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;

var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireCache = function (nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireCache = function (nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}

__webpack_require__.ir = function (obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
__webpack_require__.ir = function (obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}

if (
obj === null ||
(typeof obj !== "object" && typeof obj !== "function")
) {
return { default: obj };
}
if (
obj === null ||
(typeof obj !== "object" && typeof obj !== "function")
) {
return { default: obj };
}

var cache = _getRequireCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var cache = _getRequireCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}

var newObj = {};
var hasPropertyDescriptor =
Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
var newObj = {};
var hasPropertyDescriptor =
Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
};
})();
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
};

})();
// webpack/runtime/load_chunk_with_module
Expand Down
26 changes: 12 additions & 14 deletions crates/rspack_plugin_runtime/src/runtime/common/_export_star.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
(function () {
__webpack_require__.es = function (from, to) {
Object.keys(from).forEach(function (k) {
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k))
Object.defineProperty(to, k, {
enumerable: true,
get: function () {
return from[k];
}
});
});
return from;
};
})();
__webpack_require__.es = function (from, to) {
Object.keys(from).forEach(function (k) {
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k))
Object.defineProperty(to, k, {
enumerable: true,
get: function () {
return from[k];
}
});
});
return from;
};
84 changes: 41 additions & 43 deletions crates/rspack_plugin_runtime/src/runtime/common/_interop_require.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
(function () {
function _getRequireCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
function _getRequireCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;

var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireCache = function (nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireCache = function (nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}

__webpack_require__.ir = function (obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
__webpack_require__.ir = function (obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}

if (
obj === null ||
(typeof obj !== "object" && typeof obj !== "function")
) {
return { default: obj };
}
if (
obj === null ||
(typeof obj !== "object" && typeof obj !== "function")
) {
return { default: obj };
}

var cache = _getRequireCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var cache = _getRequireCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}

var newObj = {};
var hasPropertyDescriptor =
Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
var newObj = {};
var hasPropertyDescriptor =
Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
};
})();
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

exports[`HashTestCases should print correct hash for package-path-change-0 1`] = `
[
"runtime.ced6a4f438676fab.js",
"runtime.a17237b43fe1b025.js",
"main.8ba66a6d562c9f41.js",
"0.28a2cb9e99b496d6.js",
]
`;

exports[`HashTestCases should print correct hash for package-path-change-1 1`] = `
[
"runtime.bbbe2a6c0d629b10.js",
"runtime.d522d0dcd5f062c3.js",
"main.b11af1dbcee032b1.js",
"0.1984e8e54b0d14de.js",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ exports[`StatsTestCases should print correct stats for filename 1`] = `
},
"errors": [],
"errorsCount": 0,
"hash": "972260076b16494b",
"hash": "1896652891bba34c",
"modules": [
{
"chunks": [
Expand Down Expand Up @@ -218,7 +218,7 @@ exports[`StatsTestCases should print correct stats for filename 1`] = `
`;

exports[`StatsTestCases should print correct stats for filename 2`] = `
"Hash: 972260076b16494b
"Hash: 1896652891bba34c
Asset Size Chunks Chunk Names
main.xxxx.js 1.39 KiB main [emitted] main
dynamic_js.xxxx.js 218 bytes dynamic_js [emitted]
Expand Down Expand Up @@ -1557,7 +1557,7 @@ exports[`StatsTestCases should print correct stats for resolve-unexpected-export
},
],
"errorsCount": 1,
"hash": "a7d46af5c732ee4a",
"hash": "178bdd60e81ac756",
"modules": [
{
"chunks": [
Expand Down Expand Up @@ -1631,7 +1631,7 @@ exports[`StatsTestCases should print correct stats for resolve-unexpected-export
`;
exports[`StatsTestCases should print correct stats for resolve-unexpected-exports-in-pkg 2`] = `
"Hash: a7d46af5c732ee4a
"Hash: 178bdd60e81ac756
Asset Size Chunks Chunk Names
bundle.js 972 bytes main [emitted] main
Entrypoint main = bundle.js
Expand Down

0 comments on commit db4fe18

Please sign in to comment.