From 2bc1aa36bf7db7ad5f81928913f6c1cda8bd9da7 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Tue, 26 Dec 2017 10:57:59 +0100 Subject: [PATCH] Support static template literals Closes #265. --- src/transform.js | 4 ++-- test/form/constant-template-literal/input.js | 2 ++ test/form/constant-template-literal/output.js | 11 +++++++++++ test/form/dynamic-template-literal/input.js | 3 +++ test/form/dynamic-template-literal/output.js | 12 ++++++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/form/constant-template-literal/input.js create mode 100644 test/form/constant-template-literal/output.js create mode 100644 test/form/dynamic-template-literal/input.js create mode 100644 test/form/dynamic-template-literal/output.js diff --git a/src/transform.js b/src/transform.js index eb12e45..8387cdb 100644 --- a/src/transform.js +++ b/src/transform.js @@ -91,14 +91,14 @@ export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequi if ( !node ) return; if ( node.type !== 'CallExpression' ) return; if ( node.callee.name !== 'require' || scope.contains( 'require' ) ) return; - if ( node.arguments.length !== 1 || node.arguments[0].type !== 'Literal' ) return; // TODO handle these weird cases? + if ( node.arguments.length !== 1 || (node.arguments[0].type !== 'Literal' && (node.arguments[0].type !== 'TemplateLiteral' || node.arguments[0].expressions.length > 0) ) ) return; // TODO handle these weird cases? if ( ignoreRequire( node.arguments[0].value ) ) return; return true; } function getRequired ( node, name ) { - const source = node.arguments[0].value; + const source = node.arguments[0].type === 'Literal' ? node.arguments[0].value : node.arguments[0].quasis[0].value.cooked; const existing = required[ source ]; if ( existing === undefined ) { diff --git a/test/form/constant-template-literal/input.js b/test/form/constant-template-literal/input.js new file mode 100644 index 0000000..2a43283 --- /dev/null +++ b/test/form/constant-template-literal/input.js @@ -0,0 +1,2 @@ +var foo = require(`tape`); +console.log(foo); diff --git a/test/form/constant-template-literal/output.js b/test/form/constant-template-literal/output.js new file mode 100644 index 0000000..781cf36 --- /dev/null +++ b/test/form/constant-template-literal/output.js @@ -0,0 +1,11 @@ +import 'tape'; +import foo from 'commonjs-proxy:tape'; + +console.log(foo); + +var input = { + +}; + +export default input; +export { input as __moduleExports }; diff --git a/test/form/dynamic-template-literal/input.js b/test/form/dynamic-template-literal/input.js new file mode 100644 index 0000000..12767b6 --- /dev/null +++ b/test/form/dynamic-template-literal/input.js @@ -0,0 +1,3 @@ +var pe = 'pe'; +var foo = require(`ta${pe}`); +console.log(foo); diff --git a/test/form/dynamic-template-literal/output.js b/test/form/dynamic-template-literal/output.js new file mode 100644 index 0000000..514eb55 --- /dev/null +++ b/test/form/dynamic-template-literal/output.js @@ -0,0 +1,12 @@ +import * as commonjsHelpers from 'commonjsHelpers'; + +var pe = 'pe'; +var foo = commonjsHelpers.commonjsRequire(`ta${pe}`); +console.log(foo); + +var input = { + +}; + +export default input; +export { input as __moduleExports };