diff --git a/packages/babel-helpers/src/helpers.js b/packages/babel-helpers/src/helpers.js index 6d4d84850d..8cfabc8f64 100644 --- a/packages/babel-helpers/src/helpers.js +++ b/packages/babel-helpers/src/helpers.js @@ -690,6 +690,7 @@ helpers.slicedToArrayLoose = defineHelper(` helpers.taggedTemplateLiteral = defineHelper(` export default function _taggedTemplateLiteral(strings, raw) { + if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); @@ -698,6 +699,7 @@ helpers.taggedTemplateLiteral = defineHelper(` helpers.taggedTemplateLiteralLoose = defineHelper(` export default function _taggedTemplateLiteralLoose(strings, raw) { + if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; } diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop-loose/imports-hoisting/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop-loose/imports-hoisting/output.js index f88741ee45..b1133b75bd 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop-loose/imports-hoisting/output.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop-loose/imports-hoisting/output.js @@ -1,5 +1,5 @@ var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral"); -var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"], ["foo"]); +var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"]); tag(_templateObject); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/imports-hoisting/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/imports-hoisting/output.js index f88741ee45..b1133b75bd 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/imports-hoisting/output.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/imports-hoisting/output.js @@ -1,5 +1,5 @@ var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral"); -var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"], ["foo"]); +var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"]); tag(_templateObject); diff --git a/packages/babel-plugin-transform-template-literals/src/index.js b/packages/babel-plugin-transform-template-literals/src/index.js index 6acc478537..4d28f9d581 100644 --- a/packages/babel-plugin-transform-template-literals/src/index.js +++ b/packages/babel-plugin-transform-template-literals/src/index.js @@ -54,6 +54,9 @@ export default function(api, options) { const strings = []; const raws = []; + // Flag variable to check if contents of strings and raw are equal + let isStringsRawEqual = true; + for (const elem of (quasi.quasis: Array)) { const { raw, cooked } = elem.value; const value = @@ -63,6 +66,11 @@ export default function(api, options) { strings.push(value); raws.push(t.stringLiteral(raw)); + + if (raw !== cooked) { + // false even if one of raw and cooked are not equal + isStringsRawEqual = false; + } } // Generate a unique name based on the string literals so we dedupe @@ -81,10 +89,15 @@ export default function(api, options) { this.templates.set(name, templateObject); const helperId = this.addHelper(helperName); - const init = t.callExpression(helperId, [ - t.arrayExpression(strings), - t.arrayExpression(raws), - ]); + const callExpressionInput = []; + callExpressionInput.push(t.arrayExpression(strings)); + + if (!isStringsRawEqual) { + callExpressionInput.push(t.arrayExpression(raws)); + } + + // only add raw arrayExpression if there is any difference between raws and strings + const init = t.callExpression(helperId, callExpressionInput); annotateAsPure(init); init._compact = true; programPath.scope.push({ diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/input.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/input.js new file mode 100644 index 0000000000..5b13aa793a --- /dev/null +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/input.js @@ -0,0 +1,2 @@ +var foo = tag`wow`; +var bar = tag`first${1}second`; diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/output.js new file mode 100644 index 0000000000..7b916abd6b --- /dev/null +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/output.js @@ -0,0 +1,7 @@ +var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["wow"]), + _templateObject2 = /*#__PURE__*/ _taggedTemplateLiteral(["first", "second"]); + +function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } + +var foo = tag(_templateObject); +var bar = tag(_templateObject2, 1); diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/tag/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/tag/output.js index 6a3d89a3c4..b47badfa7f 100644 --- a/packages/babel-plugin-transform-template-literals/test/fixtures/default/tag/output.js +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/tag/output.js @@ -2,7 +2,7 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["wow\na", "b ", ""], _templateObject2 = /*#__PURE__*/ _taggedTemplateLiteral(["wow\nab", " ", ""], ["wow\\nab", " ", ""]), _templateObject3 = /*#__PURE__*/ _taggedTemplateLiteral(["wow\naB", " ", ""], ["wow\\naB", " ", ""]); -function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } +function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } var foo = bar(_templateObject, 42, _.foobar()); var bar = bar(_templateObject2, 42, _.foobar()); diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/template-revision/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/template-revision/output.js index c7f4371d6e..2f8b82de8f 100644 --- a/packages/babel-plugin-transform-template-literals/test/fixtures/default/template-revision/output.js +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/template-revision/output.js @@ -6,7 +6,7 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral([void 0], ["\\unicode _templateObject6 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u000g", "right"]), _templateObject7 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u{-0}", "right"]); -function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } +function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } tag(_templateObject); tag(_templateObject2); diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/loose/tag/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/loose/tag/output.js index 09b108b3b1..7e82908b36 100644 --- a/packages/babel-plugin-transform-template-literals/test/fixtures/loose/tag/output.js +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/loose/tag/output.js @@ -2,7 +2,7 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteralLoose(["wow\na", "b ", _templateObject2 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["wow\nab", " ", ""], ["wow\\nab", " ", ""]), _templateObject3 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["wow\naB", " ", ""], ["wow\\naB", " ", ""]); -function _taggedTemplateLiteralLoose(strings, raw) { strings.raw = raw; return strings; } +function _taggedTemplateLiteralLoose(strings, raw) { if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; } var foo = bar(_templateObject, 42, _.foobar()); var bar = bar(_templateObject2, 42, _.foobar()); diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/loose/template-revision/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/loose/template-revision/output.js index 3077a8c82f..9eee99e306 100644 --- a/packages/babel-plugin-transform-template-literals/test/fixtures/loose/template-revision/output.js +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/loose/template-revision/output.js @@ -6,7 +6,7 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteralLoose([void 0], ["\\un _templateObject6 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u000g", "right"]), _templateObject7 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u{-0}", "right"]); -function _taggedTemplateLiteralLoose(strings, raw) { strings.raw = raw; return strings; } +function _taggedTemplateLiteralLoose(strings, raw) { if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; } tag(_templateObject); tag(_templateObject2);