Skip to content

Commit

Permalink
Handle module.require with scope-hoisting (#7012)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored Oct 5, 2021
1 parent 8671ded commit 198aebf
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output = module.require("./b.js");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.b = 2;
12 changes: 12 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -4395,6 +4395,18 @@ describe('scope hoisting', function() {
assert.strictEqual(output, 'Say other');
});

it('supports using module.require like require', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/commonjs/module-require/a.js',
),
);

let output = await run(b);
assert.strictEqual(output.b, 2);
});

it('support url imports in wrapped modules with interop', async function() {
let b = await bundle(
path.join(
Expand Down
4 changes: 3 additions & 1 deletion packages/transformers/js/core/src/dependency_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ impl<'a> Fold for DependencyCollector<'a> {
}
}
Member(member) => {
if self.config.is_browser
if match_member_expr(member, vec!["module", "require"], self.decls) {
DependencyKind::Require
} else if self.config.is_browser
&& match_member_expr(
member,
vec!["navigator", "serviceWorker", "register"],
Expand Down
4 changes: 4 additions & 0 deletions packages/transformers/js/core/src/hoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,10 @@ impl Visit for Collect {
return;
}

if match_member_expr(node, vec!["module", "require"], &self.decls) {
return;
}

let is_static = match &*node.prop {
Expr::Ident(_) => !node.computed,
Expr::Lit(Lit::Str(_)) => true,
Expand Down
11 changes: 11 additions & 0 deletions packages/transformers/js/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ pub fn match_require(

None
}
Expr::Member(member) => {
if match_member_expr(member, vec!["module", "require"], decls) {
if let Some(arg) = call.args.get(0) {
if let Expr::Lit(Lit::Str(str_)) = &*arg.expr {
return Some(str_.value.clone());
}
}
}

None
}
_ => None,
},
_ => None,
Expand Down

0 comments on commit 198aebf

Please sign in to comment.