Skip to content

Commit

Permalink
fix(bundler): Use interop for common js dependencies (#2930)
Browse files Browse the repository at this point in the history
swc_bundler:
 - Update helpers to use interop for common js deps. (Closes #2789)
  • Loading branch information
kdy1 authored Dec 1, 2021
1 parent 1fabb03 commit 24de550
Show file tree
Hide file tree
Showing 22 changed files with 1,111 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc_bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include = ["Cargo.toml", "build.rs", "src/**/*.rs", "src/**/*.js"]
license = "Apache-2.0"
name = "swc_bundler"
repository = "https://github.com/swc-project/swc.git"
version = "0.82.2"
version = "0.82.3"

[package.metadata.docs.rs]
all-features = true
Expand Down
29 changes: 28 additions & 1 deletion crates/swc_bundler/src/bundler/helpers/_require.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
function __swcpack_require__(mod) {
// Ported from _interopRequireWildcard
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {};

if (obj != null) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {};

if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}

newObj.default = obj;
return newObj;
}
}


var cache;

if (cache) {
Expand All @@ -10,6 +37,6 @@ function __swcpack_require__(mod) {
};

mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function __swcpack_require__(mod) {
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
var cache;
if (cache) {
return cache;
Expand All @@ -8,7 +31,7 @@ function __swcpack_require__(mod) {
}
};
mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function __swcpack_require__(mod) {
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
var cache;
if (cache) {
return cache;
Expand All @@ -8,7 +31,7 @@ function __swcpack_require__(mod) {
}
};
mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function __swcpack_require__(mod) {
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
var cache;
if (cache) {
return cache;
Expand All @@ -8,7 +31,7 @@ function __swcpack_require__(mod) {
}
};
mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function __swcpack_require__(mod) {
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
var cache;
if (cache) {
return cache;
Expand All @@ -8,7 +31,7 @@ function __swcpack_require__(mod) {
}
};
mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function __swcpack_require__(mod) {
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
var cache;
if (cache) {
return cache;
Expand All @@ -8,7 +31,7 @@ function __swcpack_require__(mod) {
}
};
mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function __swcpack_require__(mod) {
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
var cache;
if (cache) {
return cache;
Expand All @@ -8,7 +31,7 @@ function __swcpack_require__(mod) {
}
};
mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function __swcpack_require__(mod) {
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
var cache;
if (cache) {
return cache;
Expand All @@ -8,7 +31,7 @@ function __swcpack_require__(mod) {
}
};
mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function __swcpack_require__(mod) {
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
var cache;
if (cache) {
return cache;
Expand All @@ -8,7 +31,7 @@ function __swcpack_require__(mod) {
}
};
mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
Expand Down
25 changes: 24 additions & 1 deletion crates/swc_node_bundler/tests/pass/cjs/mixed/output/entry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function __swcpack_require__(mod) {
function interop(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
var cache;
if (cache) {
return cache;
Expand All @@ -8,7 +31,7 @@ function __swcpack_require__(mod) {
}
};
mod(module, module.exports);
cache = module.exports;
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
Expand Down
Loading

2 comments on commit 24de550

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 24de550 Previous: 1fabb03 Ratio
base_tr_fixer 27350 ns/iter (± 2762) 32478 ns/iter (± 3310) 0.84
base_tr_resolver_and_hygiene 159270 ns/iter (± 9711) 169659 ns/iter (± 30622) 0.94
codegen_es2015 60550 ns/iter (± 3699) 57078 ns/iter (± 12464) 1.06
codegen_es2016 60800 ns/iter (± 3080) 57325 ns/iter (± 7994) 1.06
codegen_es2017 60227 ns/iter (± 4071) 58862 ns/iter (± 10028) 1.02
codegen_es2018 60144 ns/iter (± 4607) 58270 ns/iter (± 10164) 1.03
codegen_es2019 60423 ns/iter (± 3554) 57463 ns/iter (± 9477) 1.05
codegen_es2020 60298 ns/iter (± 4287) 58858 ns/iter (± 20591) 1.02
codegen_es3 60256 ns/iter (± 2985) 60224 ns/iter (± 10713) 1.00
codegen_es5 60639 ns/iter (± 3484) 61403 ns/iter (± 12293) 0.99
full_es2015 213932176 ns/iter (± 16990318) 198126346 ns/iter (± 17972545) 1.08
full_es2016 170783114 ns/iter (± 15148620) 171469484 ns/iter (± 26240810) 1.00
full_es2017 181519386 ns/iter (± 20688489) 177997485 ns/iter (± 22298200) 1.02
full_es2018 181654474 ns/iter (± 14851282) 177218785 ns/iter (± 19732202) 1.03
full_es2019 181300306 ns/iter (± 18818862) 173116983 ns/iter (± 18064915) 1.05
full_es2020 179080135 ns/iter (± 21907767) 172021605 ns/iter (± 12826040) 1.04
full_es3 256107167 ns/iter (± 19260070) 249995034 ns/iter (± 27453465) 1.02
full_es5 233134869 ns/iter (± 24250629) 224066949 ns/iter (± 20284597) 1.04
parser 809377 ns/iter (± 30445) 826803 ns/iter (± 155483) 0.98
ser_ast_node 184 ns/iter (± 9) 168 ns/iter (± 23) 1.10
ser_serde 185 ns/iter (± 7) 164 ns/iter (± 25) 1.13
emit_colors 22009069 ns/iter (± 28603231) 10918628 ns/iter (± 17107096) 2.02
emit_large 133091768 ns/iter (± 198285329) 74137658 ns/iter (± 103039986) 1.80
base_clone 2971634 ns/iter (± 337586) 2674042 ns/iter (± 386058) 1.11
fold_span 4967912 ns/iter (± 633761) 4433762 ns/iter (± 841767) 1.12
fold_span_panic 5099644 ns/iter (± 511455) 4630843 ns/iter (± 674279) 1.10
visit_mut_span 3592886 ns/iter (± 298943) 3408499 ns/iter (± 466849) 1.05
visit_mut_span_panic 3643228 ns/iter (± 397608) 3405169 ns/iter (± 678880) 1.07
ast_clone 21298 ns/iter (± 2053) 20817 ns/iter (± 2187) 1.02
ast_clone_to_stable 60184 ns/iter (± 9243) 76286 ns/iter (± 14135) 0.79
ast_clone_to_stable_then_to_unstable 115766 ns/iter (± 8529) 130081 ns/iter (± 18683) 0.89
json_deserialize 2274968 ns/iter (± 30398) 2529540 ns/iter (± 404378) 0.90
json_serialize 109507 ns/iter (± 1940) 111761 ns/iter (± 32046) 0.98
boxing_boxed 159 ns/iter (± 7) 145 ns/iter (± 25) 1.10
boxing_boxed_clone 80 ns/iter (± 1) 77 ns/iter (± 8) 1.04
boxing_unboxed 131 ns/iter (± 4) 134 ns/iter (± 27) 0.98
boxing_unboxed_clone 75 ns/iter (± 1) 76 ns/iter (± 13) 0.99
time_10 428 ns/iter (± 17) 348 ns/iter (± 62) 1.23
time_15 851 ns/iter (± 13) 801 ns/iter (± 161) 1.06
time_20 1373 ns/iter (± 28) 1375 ns/iter (± 168) 1.00
time_40 4821 ns/iter (± 173) 5111 ns/iter (± 661) 0.94
time_5 120 ns/iter (± 6) 114 ns/iter (± 15) 1.05
time_60 10254 ns/iter (± 327) 11895 ns/iter (± 2023) 0.86

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 24de550 Previous: 1fabb03 Ratio
emit_colors 22009069 ns/iter (± 28603231) 10918628 ns/iter (± 17107096) 2.02

This comment was automatically generated by workflow using github-action-benchmark.

CC: @kdy1

Please sign in to comment.