Skip to content

Commit

Permalink
Fix require statements with string literal
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Nov 26, 2021
1 parent 9f0d52d commit 278fc76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _ = require('lodash');
const add = require(`lodash/add`);

module.exports = function (a, b) {
return _.add(a, b);
return add(a, _.add(a, b));
};
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()
})));
}
}

if kind == DependencyKind::ServiceWorker || kind == DependencyKind::Worklet {
let (source_type, opts) = if kind == DependencyKind::ServiceWorker {
match_worker_type(node.args.get(1))
Expand Down

0 comments on commit 278fc76

Please sign in to comment.