Skip to content

Commit

Permalink
fix(es/modules): Fix local scoped amd require (#4800)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj authored May 25, 2022
1 parent 54fd602 commit 2b03047
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 32 deletions.
32 changes: 10 additions & 22 deletions crates/swc_ecma_transforms_module/src/amd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,10 @@ impl LocalScopedRequireVisitor {
}

impl Visit for LocalScopedRequireVisitor {
fn visit_call_expr(&mut self, call_expr: &CallExpr) {
let callee = &call_expr.callee;
if let Callee::Expr(expr) = callee {
match &**expr {
Expr::Ident(ident) => {
if self.require_ident.is_none() && &*ident.sym == "require" {
self.require_ident = Some(ident.clone());
}
}
Expr::Member(MemberExpr { obj, .. }) => {
let expr = &**obj;
if let Expr::Ident(ident) = expr {
if self.require_ident.is_none() && &*ident.sym == "require" {
self.require_ident = Some(ident.clone());
}
}
}
_ => {}
}
fn visit_ident(&mut self, ident: &Ident) {
if self.require_ident.is_none() && &*ident.sym == "require" {
self.require_ident = Some(ident.clone());
}

call_expr.visit_children_with(self);
}
}

Expand Down Expand Up @@ -532,13 +514,19 @@ impl Fold for Amd {
let mut scope_ref_mut = self.scope.borrow_mut();
let scope = &mut *scope_ref_mut;
let mut factory_params = Vec::with_capacity(scope.imports.len() + 1);
if has_export {

// inject local scoped `require` regardless of having exports or not, as long as
// it can be considered as module (either having import or export)
if !scope.imports.is_empty() || has_export {
define_deps_arg.elems.push(Some("require".as_arg()));
factory_params.push(Param {
span: DUMMY_SP,
decorators: Default::default(),
pat: scoped_local_require_ident.into(),
});
}

if has_export {
define_deps_arg.elems.push(Some("exports".as_arg()));
factory_params.push(Param {
span: DUMMY_SP,
Expand Down
69 changes: 59 additions & 10 deletions crates/swc_ecma_transforms_module/tests/amd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test!(
"import bar from 'bar';
obj[bar('bas')] = '123'",
"define([\"bar\"], function(_bar) {
"define([\"require\", \"bar\"], function(require, _bar) {
\"use strict\";
_bar = _interopRequireDefault(_bar);
obj[(0, _bar).default('bas')] = '123';
Expand Down Expand Up @@ -105,6 +105,55 @@ define(["require", "exports"], function(require, _exports) {
"#
);

test!(
syntax(),
|_| tr(Config {
config: util::Config {
strict: true,
..Default::default()
},
..Default::default()
}),
inner_scoped_local_require_member_expr,
r#"export const DefaultIconPath = FileAccess.asBrowserUri('./media/defaultIcon.png', require).toString(true);"#,
r#"
define([
"require",
"exports",
], function(require, _exports) {
"use strict";
_exports.DefaultIconPath = void 0;
const DefaultIconPath = FileAccess.asBrowserUri('./media/defaultIcon.png', require).toString(true);
_exports.DefaultIconPath = DefaultIconPath;
});
"#
);

test!(
syntax(),
|_| tr(Config {
config: util::Config {
strict: true,
..Default::default()
},
..Default::default()
}),
inner_scoped_local_require_non_export_module,
r#"
import { getPathFromAmdModule } from 'vs/base/test/node/testUtils';
const fixturesFolder = getPathFromAmdModule(require, './fixtures');
"#,
r#"
define([
"require",
"vs/base/test/node/testUtils"
], function(require, _testUtils) {
"use strict";
const fixturesFolder = (0, _testUtils).getPathFromAmdModule(require, './fixtures');
});
"#
);

test!(
syntax(),
|_| tr(Config {
Expand Down Expand Up @@ -140,7 +189,7 @@ test!(
r#"import * as foo from 'foo';
import bar from 'bar';"#,
r#"
define(["foo", "bar"], function(foo, _bar) {
define(["require", "foo", "bar"], function(require, foo, _bar) {
"use strict";
});
"#
Expand All @@ -157,7 +206,7 @@ import React from 'react'
window.React = React;
"#,
r#"
define(["react"], function(_react) {
define(["require", "react"], function(require, _react) {
"use strict";
_react = _interopRequireDefault(_react);
window.React = _react.default;
Expand Down Expand Up @@ -282,7 +331,7 @@ foo2;
"#,
r#"
define(["foo"], function (_foo) {
define(["require", "foo"], function (require, _foo) {
"use strict";
_foo = _interopRequireDefault(_foo);
Expand All @@ -306,7 +355,7 @@ xyz;
"#,
r#"
define(["foo"], function (_foo) {
define(["require", "foo"], function (require, _foo) {
"use strict";
_foo = _interopRequireWildcard(_foo);
Expand Down Expand Up @@ -540,7 +589,7 @@ foo;
"#,
r#"
define(["foo"], function (foo) {
define(["require", "foo"], function (require, foo) {
"use strict";
foo = _interopRequireWildcard(foo);
Expand Down Expand Up @@ -628,7 +677,7 @@ import "./directory/foo-bar";
"#,
r#"
define(["foo", "foo-bar", "./directory/foo-bar"], function (_foo, _fooBar, _fooBar1) {
define(["require", "foo", "foo-bar", "./directory/foo-bar"], function (require, _foo, _fooBar, _fooBar1) {
"use strict";
});
Expand Down Expand Up @@ -964,7 +1013,7 @@ import { qux } from './qux';
"#,
r#"
define(["./foo", "./bar", "./derp", "./qux"], function (_foo, _bar, _derp, _qux) {
define(["require", "./foo", "./bar", "./derp", "./qux"], function (require, _foo, _bar, _derp, _qux) {
"use strict";
_bar = _interopRequireDefault(_bar);
Expand Down Expand Up @@ -1073,7 +1122,7 @@ xyz;
"#,
r#"
define(["foo"], function (_foo) {
define(["require", "foo"], function (require, _foo) {
"use strict";
_foo.bar;
Expand Down Expand Up @@ -1239,7 +1288,7 @@ console.log(elm);
"#,
r#"
define(["foo"], function (_foo) {
define(["require", "foo"], function (require, _foo) {
"use strict";
for(let _i = 0; _i < _foo.array.length; _i++){
Expand Down

1 comment on commit 2b03047

@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: 2b03047 Previous: 8894e77 Ratio
es/full/minify/libraries/antd 2390954549 ns/iter (± 114904339) 2312738173 ns/iter (± 80770868) 1.03
es/full/minify/libraries/d3 572922027 ns/iter (± 22948661) 670646965 ns/iter (± 25228648) 0.85
es/full/minify/libraries/echarts 3034230637 ns/iter (± 55233201) 3042831760 ns/iter (± 43600945) 1.00
es/full/minify/libraries/jquery 118103645 ns/iter (± 3625034) 121318449 ns/iter (± 2048905) 0.97
es/full/minify/libraries/lodash 178063510 ns/iter (± 5573661) 180154317 ns/iter (± 3570807) 0.99
es/full/minify/libraries/moment 70606955 ns/iter (± 2486487) 71983300 ns/iter (± 1356923) 0.98
es/full/minify/libraries/react 22918999 ns/iter (± 628310) 23258615 ns/iter (± 421539) 0.99
es/full/minify/libraries/terser 758371576 ns/iter (± 26826287) 781604723 ns/iter (± 13934741) 0.97
es/full/minify/libraries/three 676012639 ns/iter (± 49811324) 764108885 ns/iter (± 12148297) 0.88
es/full/minify/libraries/typescript 5398930469 ns/iter (± 53356890) 5383316203 ns/iter (± 30404066) 1.00
es/full/minify/libraries/victory 914423225 ns/iter (± 47873665) 980384816 ns/iter (± 32640702) 0.93
es/full/minify/libraries/vue 182546935 ns/iter (± 3773220) 181009325 ns/iter (± 3565019) 1.01
es/full/codegen/es3 39219 ns/iter (± 2834) 38199 ns/iter (± 1213) 1.03
es/full/codegen/es5 38867 ns/iter (± 3358) 38243 ns/iter (± 1470) 1.02
es/full/codegen/es2015 37895 ns/iter (± 2625) 39120 ns/iter (± 2006) 0.97
es/full/codegen/es2016 38868 ns/iter (± 2668) 38427 ns/iter (± 4157) 1.01
es/full/codegen/es2017 38001 ns/iter (± 4145) 38836 ns/iter (± 2591) 0.98
es/full/codegen/es2018 39443 ns/iter (± 2928) 38808 ns/iter (± 1397) 1.02
es/full/codegen/es2019 37817 ns/iter (± 2471) 38275 ns/iter (± 1751) 0.99
es/full/codegen/es2020 37168 ns/iter (± 2852) 38181 ns/iter (± 1759) 0.97
es/full/all/es3 212118027 ns/iter (± 7915980) 209106314 ns/iter (± 4265774) 1.01
es/full/all/es5 200449194 ns/iter (± 7247729) 198075933 ns/iter (± 5949281) 1.01
es/full/all/es2015 158800441 ns/iter (± 7055434) 157998363 ns/iter (± 4123541) 1.01
es/full/all/es2016 159277731 ns/iter (± 5728191) 156819882 ns/iter (± 3369565) 1.02
es/full/all/es2017 157690983 ns/iter (± 5879812) 155382243 ns/iter (± 4301415) 1.01
es/full/all/es2018 156163229 ns/iter (± 5620895) 152691214 ns/iter (± 3871640) 1.02
es/full/all/es2019 155496553 ns/iter (± 5465667) 153422718 ns/iter (± 4480866) 1.01
es/full/all/es2020 145978743 ns/iter (± 5872363) 145883802 ns/iter (± 4266897) 1.00
es/full/parser 717221 ns/iter (± 62345) 775183 ns/iter (± 79017) 0.93
es/full/base/fixer 34385 ns/iter (± 2374) 35094 ns/iter (± 1454) 0.98
es/full/base/resolver_and_hygiene 139367 ns/iter (± 8392) 144035 ns/iter (± 4853) 0.97
serialization of ast node 187 ns/iter (± 15) 201 ns/iter (± 5) 0.93
serialization of serde 191 ns/iter (± 13) 203 ns/iter (± 6) 0.94

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

Please sign in to comment.