Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Support static template literals #271

Merged
merged 1 commit into from
Jan 26, 2018
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
4 changes: 2 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Choose a reason for hiding this comment

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

Since this template literal case is now handled at least to some degree, the comment seems a bit misleading... it can probably go, eh?

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 ) {
Expand Down
2 changes: 2 additions & 0 deletions test/form/constant-template-literal/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var foo = require(`tape`);
console.log(foo);
11 changes: 11 additions & 0 deletions test/form/constant-template-literal/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'tape';
import foo from 'commonjs-proxy:tape';

console.log(foo);

var input = {

};

export default input;
export { input as __moduleExports };
3 changes: 3 additions & 0 deletions test/form/dynamic-template-literal/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var pe = 'pe';
var foo = require(`ta${pe}`);
console.log(foo);
12 changes: 12 additions & 0 deletions test/form/dynamic-template-literal/output.js
Original file line number Diff line number Diff line change
@@ -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 };