From b08ef9cb340a46c17ba388f6f7f97ce8cfeaba5b Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Sun, 28 Nov 2021 23:20:39 +0100 Subject: [PATCH] Fix require statements with plain template literals (#7369) --- .../index.js | 7 ++++ .../package.json | 13 +++++++ .../yarn.lock | 0 .../commonjs-template-literal-plain/index.js | 5 +++ .../package.json | 13 +++++++ .../commonjs-template-literal-plain/yarn.lock | 0 .../core/integration-tests/test/javascript.js | 39 +++++++++++++++++++ .../js/core/src/dependency_collector.rs | 13 +++++++ packages/transformers/js/core/src/utils.rs | 12 ++++++ 9 files changed, 102 insertions(+) create mode 100644 packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/index.js create mode 100644 packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/package.json create mode 100644 packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/yarn.lock create mode 100644 packages/core/integration-tests/test/integration/commonjs-template-literal-plain/index.js create mode 100644 packages/core/integration-tests/test/integration/commonjs-template-literal-plain/package.json create mode 100644 packages/core/integration-tests/test/integration/commonjs-template-literal-plain/yarn.lock diff --git a/packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/index.js b/packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/index.js new file mode 100644 index 00000000000..b80f1a1b536 --- /dev/null +++ b/packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/index.js @@ -0,0 +1,7 @@ +const fn = 'add'; + +module.exports = function (a, b) { + const add = require(`lodash/${fn}`); + + return add(a, b); +}; diff --git a/packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/package.json b/packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/package.json new file mode 100644 index 00000000000..73c71ecd336 --- /dev/null +++ b/packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/package.json @@ -0,0 +1,13 @@ +{ + "name": "commonjs-require", + "private": true, + "main": "dist/index.js", + "targets": { + "main": { + "context": "node" + } + }, + "dependencies": { + "lodash": "*" + } +} diff --git a/packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/yarn.lock b/packages/core/integration-tests/test/integration/commonjs-template-literal-interpolation/yarn.lock new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/core/integration-tests/test/integration/commonjs-template-literal-plain/index.js b/packages/core/integration-tests/test/integration/commonjs-template-literal-plain/index.js new file mode 100644 index 00000000000..ed75a624b63 --- /dev/null +++ b/packages/core/integration-tests/test/integration/commonjs-template-literal-plain/index.js @@ -0,0 +1,5 @@ +const _ = require(`lodash`); + +module.exports = function (a, b) { + return _.add(a, b); +}; diff --git a/packages/core/integration-tests/test/integration/commonjs-template-literal-plain/package.json b/packages/core/integration-tests/test/integration/commonjs-template-literal-plain/package.json new file mode 100644 index 00000000000..73c71ecd336 --- /dev/null +++ b/packages/core/integration-tests/test/integration/commonjs-template-literal-plain/package.json @@ -0,0 +1,13 @@ +{ + "name": "commonjs-require", + "private": true, + "main": "dist/index.js", + "targets": { + "main": { + "context": "node" + } + }, + "dependencies": { + "lodash": "*" + } +} diff --git a/packages/core/integration-tests/test/integration/commonjs-template-literal-plain/yarn.lock b/packages/core/integration-tests/test/integration/commonjs-template-literal-plain/yarn.lock new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/core/integration-tests/test/javascript.js b/packages/core/integration-tests/test/javascript.js index 45fa45996c6..3d1c3741aeb 100644 --- a/packages/core/integration-tests/test/javascript.js +++ b/packages/core/integration-tests/test/javascript.js @@ -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 = () => diff --git a/packages/transformers/js/core/src/dependency_collector.rs b/packages/transformers/js/core/src/dependency_collector.rs index 6b2c4a34185..e2cfce09032 100644 --- a/packages/transformers/js/core/src/dependency_collector.rs +++ b/packages/transformers/js/core/src/dependency_collector.rs @@ -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() + }))); + } + } + if kind == DependencyKind::ServiceWorker || kind == DependencyKind::Worklet { let (source_type, opts) = if kind == DependencyKind::ServiceWorker { match_worker_type(node.args.get(1)) diff --git a/packages/transformers/js/core/src/utils.rs b/packages/transformers/js/core/src/utils.rs index 83e2758c9ce..7610d317467 100644 --- a/packages/transformers/js/core/src/utils.rs +++ b/packages/transformers/js/core/src/utils.rs @@ -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); + } + } } } @@ -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); + } + } } }