Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix require statements with plain template literals #7369

Merged
merged 5 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const fn = 'add';

module.exports = function (a, b) {
const add = require(`lodash/${fn}`);

return add(a, b);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "commonjs-require",
"private": true,
"main": "dist/index.js",
"targets": {
"main": {
"context": "node"
}
},
"dependencies": {
"lodash": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const _ = require(`lodash`);

module.exports = function (a, b) {
return _.add(a, b);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "commonjs-require",
"private": true,
"main": "dist/index.js",
"targets": {
"main": {
"context": "node"
}
},
"dependencies": {
"lodash": "*"
}
}
39 changes: 39 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4422,6 +4422,45 @@ describe('javascript', function () {
assert.equal(await run(b), 2);
});

it('should detect requires in commonjs with plain template literals', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/commonjs-template-literal-plain/index.js',
),
);
let dist = await outputFS.readFile(
b.getBundles().find(b => b.type === 'js').filePath,
'utf8',
);
assert(dist.includes('$cPUKg$lodash = require("lodash");'));

let add = await run(b);
assert.equal(add(2, 3), 5);
});

it(`should detect requires in commonjs with plain template literals`, async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/commonjs-template-literal-interpolation/index.js',
),
);
let dist = await outputFS.readFile(
b.getBundles().find(b => b.type === 'js').filePath,
'utf8',
);

assert(
dist.includes(
'const add = require(`lodash/${$8cad8166811e0063$var$fn}`);',
),
);

let add = await run(b);
assert.equal(add(2, 3), 5);
});

it('only updates bundle names of changed bundles for browsers', async () => {
let fixtureDir = path.join(__dirname, '/integration/name-invalidation');
let _bundle = () =>
Expand Down
13 changes: 13 additions & 0 deletions packages/transformers/js/core/src/dependency_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,19 @@ impl<'a> Fold for DependencyCollector<'a> {
}

let node = if let Some(arg) = node.args.get(0) {
let mut arg = arg.clone();

// convert require(`./name`) to require("./name")
if let ast::Expr::Tpl(_tpl) = &*arg.expr {
if _tpl.quasis.len() == 1 && _tpl.exprs.is_empty() {
let tpl_str = &_tpl.quasis[0].raw;
arg.expr = Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str {
value: tpl_str.clone().value,
..tpl_str.clone()
})));
}
}

Comment on lines +542 to +554
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not really familiar with Rust so if this code is not optimized or can be rewritten to something better, I'll take it :)

if kind == DependencyKind::ServiceWorker || kind == DependencyKind::Worklet {
let (source_type, opts) = if kind == DependencyKind::ServiceWorker {
match_worker_type(node.args.get(1))
Expand Down
12 changes: 12 additions & 0 deletions packages/transformers/js/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ pub fn match_require(
if let Expr::Lit(Lit::Str(str_)) = &*arg.expr {
return Some(str_.value.clone());
}

if let ast::Expr::Tpl(_tpl) = &*arg.expr {
if _tpl.quasis.len() == 1 && _tpl.exprs.is_empty() {
return Some(_tpl.quasis[0].raw.clone().value);
}
}
}
}

Expand All @@ -120,6 +126,12 @@ pub fn match_require(
if let Expr::Lit(Lit::Str(str_)) = &*arg.expr {
return Some(str_.value.clone());
}

if let ast::Expr::Tpl(_tpl) = &*arg.expr {
if _tpl.quasis.len() == 1 && _tpl.exprs.is_empty() {
return Some(_tpl.quasis[0].raw.clone().value);
}
}
}
}

Expand Down