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 d17f42975d..e7df6e2ab4 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 @@ -4,14 +4,6 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral")); -function _templateObject() { - const data = (0, _taggedTemplateLiteral2.default)(["foo"]); +var _templateObject; - _templateObject = function () { - return data; - }; - - return data; -} - -tag(_templateObject()); +tag(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["foo"]))); 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 d17f42975d..e7df6e2ab4 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 @@ -4,14 +4,6 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral")); -function _templateObject() { - const data = (0, _taggedTemplateLiteral2.default)(["foo"]); +var _templateObject; - _templateObject = function () { - return data; - }; - - return data; -} - -tag(_templateObject()); +tag(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["foo"]))); diff --git a/packages/babel-plugin-transform-template-literals/src/index.js b/packages/babel-plugin-transform-template-literals/src/index.js index abdf7087f8..d1f1689103 100644 --- a/packages/babel-plugin-transform-template-literals/src/index.js +++ b/packages/babel-plugin-transform-template-literals/src/index.js @@ -73,29 +73,22 @@ export default declare((api, options) => { } } - const scope = path.scope.getProgramParent(); - const templateObject = scope.generateUidIdentifier("templateObject"); - - const helperId = this.addHelper(helperName); - const callExpressionInput = [t.arrayExpression(strings)]; - + const helperArgs = [t.arrayExpression(strings)]; // only add raw arrayExpression if there is any difference between raws and strings if (!isStringsRawEqual) { - callExpressionInput.push(t.arrayExpression(raws)); + helperArgs.push(t.arrayExpression(raws)); } - const lazyLoad = template.ast` - function ${templateObject}() { - const data = ${t.callExpression(helperId, callExpressionInput)}; - ${t.cloneNode(templateObject)} = function() { return data }; - return data; - } - `; + const tmp = path.scope.generateUidIdentifier("templateObject"); + path.scope.getProgramParent().push({ id: t.cloneNode(tmp) }); - scope.path.unshiftContainer("body", lazyLoad); path.replaceWith( t.callExpression(node.tag, [ - t.callExpression(t.cloneNode(templateObject), []), + template.expression.ast` + ${t.cloneNode(tmp)} || ( + ${tmp} = ${this.addHelper(helperName)}(${helperArgs}) + ) + `, ...quasi.expressions, ]), ); diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js index 1c2af18033..391209700a 100644 --- a/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js @@ -1,33 +1,15 @@ -function _templateObject2() { - const data = _taggedTemplateLiteral(["some template"]); - - _templateObject2 = function () { - return data; - }; - - return data; -} - -function _templateObject() { - const data = _taggedTemplateLiteral(["some template"]); - - _templateObject = function () { - return data; - }; - - return data; -} +var _templateObject, _templateObject2; function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } var tag = v => v; function foo() { - return tag(_templateObject()); + return tag(_templateObject || (_templateObject = _taggedTemplateLiteral(["some template"]))); } function bar() { - return tag(_templateObject2()); + return tag(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["some template"]))); } expect(foo()).toBe(foo()); 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 index dd77aab5b4..cb92478b58 100644 --- 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 @@ -1,24 +1,6 @@ -function _templateObject2() { - const data = _taggedTemplateLiteral(["first", "second"]); - - _templateObject2 = function () { - return data; - }; - - return data; -} - -function _templateObject() { - const data = _taggedTemplateLiteral(["wow"]); - - _templateObject = function () { - return data; - }; - - return data; -} +var _templateObject, _templateObject2; 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); +var foo = tag(_templateObject || (_templateObject = _taggedTemplateLiteral(["wow"]))); +var bar = tag(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["first", "second"])), 1); diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/tag-with-unicode-escapes/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/tag-with-unicode-escapes/output.js index 81e26c19e1..84eb40577d 100644 --- a/packages/babel-plugin-transform-template-literals/test/fixtures/default/tag-with-unicode-escapes/output.js +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/tag-with-unicode-escapes/output.js @@ -1,13 +1,5 @@ -function _templateObject() { - const data = _taggedTemplateLiteral(["aa\uD835\uDC9C\uD835\uDC9C"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"]); - - _templateObject = function () { - return data; - }; - - return data; -} +var _templateObject; 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()); +var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["aa\uD835\uDC9C\uD835\uDC9C"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"]))); 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 4e3651b5f3..726c446db7 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 @@ -1,35 +1,7 @@ -function _templateObject3() { - const data = _taggedTemplateLiteral(["wow\naB", " ", ""], ["wow\\naB", " ", ""]); - - _templateObject3 = function () { - return data; - }; - - return data; -} - -function _templateObject2() { - const data = _taggedTemplateLiteral(["wow\nab", " ", ""], ["wow\\nab", " ", ""]); - - _templateObject2 = function () { - return data; - }; - - return data; -} - -function _templateObject() { - const data = _taggedTemplateLiteral(["wow\na", "b ", ""], ["wow\\na", "b ", ""]); - - _templateObject = function () { - return data; - }; - - return data; -} +var _templateObject, _templateObject2, _templateObject3; 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()); -var bar = bar(_templateObject3(), 42, _.baz()); +var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["wow\na", "b ", ""], ["wow\\na", "b ", ""])), 42, _.foobar()); +var bar = bar(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["wow\nab", " ", ""], ["wow\\nab", " ", ""])), 42, _.foobar()); +var bar = bar(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["wow\naB", " ", ""], ["wow\\naB", " ", ""])), 42, _.baz()); 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 372379c787..63b4c97c39 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 @@ -1,94 +1,16 @@ -function _templateObject8() { - const data = _taggedTemplateLiteral([void 0], ["\\01"]); - - _templateObject8 = function () { - return data; - }; - - return data; -} - -function _templateObject7() { - const data = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u{-0}", "right"]); - - _templateObject7 = function () { - return data; - }; - - return data; -} - -function _templateObject6() { - const data = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u000g", "right"]); - - _templateObject6 = function () { - return data; - }; - - return data; -} - -function _templateObject5() { - const data = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\xg", "right"]); - - _templateObject5 = function () { - return data; - }; - - return data; -} - -function _templateObject4() { - const data = _taggedTemplateLiteral(["left", void 0], ["left", "\\xg"]); - - _templateObject4 = function () { - return data; - }; - - return data; -} - -function _templateObject3() { - const data = _taggedTemplateLiteral([void 0, "right"], ["\\xg", "right"]); - - _templateObject3 = function () { - return data; - }; - - return data; -} - -function _templateObject2() { - const data = _taggedTemplateLiteral([void 0], ["\\01"]); - - _templateObject2 = function () { - return data; - }; - - return data; -} - -function _templateObject() { - const data = _taggedTemplateLiteral([void 0], ["\\unicode and \\u{55}"]); - - _templateObject = function () { - return data; - }; - - return data; -} +var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8; 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()); -tag(_templateObject3(), 0); -tag(_templateObject4(), 0); -tag(_templateObject5(), 0, 1); -tag(_templateObject6(), 0, 1); -tag(_templateObject7(), 0, 1); +tag(_templateObject || (_templateObject = _taggedTemplateLiteral([void 0], ["\\unicode and \\u{55}"]))); +tag(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral([void 0], ["\\01"]))); +tag(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral([void 0, "right"], ["\\xg", "right"])), 0); +tag(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["left", void 0], ["left", "\\xg"])), 0); +tag(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\xg", "right"])), 0, 1); +tag(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u000g", "right"])), 0, 1); +tag(_templateObject7 || (_templateObject7 = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u{-0}", "right"])), 0, 1); function a() { var undefined = 4; - tag(_templateObject8()); + tag(_templateObject8 || (_templateObject8 = _taggedTemplateLiteral([void 0], ["\\01"]))); } 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 f403521d97..c0c2e762df 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 @@ -1,35 +1,7 @@ -function _templateObject3() { - const data = _taggedTemplateLiteralLoose(["wow\naB", " ", ""], ["wow\\naB", " ", ""]); - - _templateObject3 = function () { - return data; - }; - - return data; -} - -function _templateObject2() { - const data = _taggedTemplateLiteralLoose(["wow\nab", " ", ""], ["wow\\nab", " ", ""]); - - _templateObject2 = function () { - return data; - }; - - return data; -} - -function _templateObject() { - const data = _taggedTemplateLiteralLoose(["wow\na", "b ", ""], ["wow\\na", "b ", ""]); - - _templateObject = function () { - return data; - }; - - return data; -} +var _templateObject, _templateObject2, _templateObject3; 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()); -var bar = bar(_templateObject3(), 42, _.baz()); +var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["wow\na", "b ", ""], ["wow\\na", "b ", ""])), 42, _.foobar()); +var bar = bar(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose(["wow\nab", " ", ""], ["wow\\nab", " ", ""])), 42, _.foobar()); +var bar = bar(_templateObject3 || (_templateObject3 = _taggedTemplateLiteralLoose(["wow\naB", " ", ""], ["wow\\naB", " ", ""])), 42, _.baz()); 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 7aa707ca75..cc2999ada6 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 @@ -1,94 +1,16 @@ -function _templateObject8() { - const data = _taggedTemplateLiteralLoose([void 0], ["\\01"]); - - _templateObject8 = function () { - return data; - }; - - return data; -} - -function _templateObject7() { - const data = _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u{-0}", "right"]); - - _templateObject7 = function () { - return data; - }; - - return data; -} - -function _templateObject6() { - const data = _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u000g", "right"]); - - _templateObject6 = function () { - return data; - }; - - return data; -} - -function _templateObject5() { - const data = _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\xg", "right"]); - - _templateObject5 = function () { - return data; - }; - - return data; -} - -function _templateObject4() { - const data = _taggedTemplateLiteralLoose(["left", void 0], ["left", "\\xg"]); - - _templateObject4 = function () { - return data; - }; - - return data; -} - -function _templateObject3() { - const data = _taggedTemplateLiteralLoose([void 0, "right"], ["\\xg", "right"]); - - _templateObject3 = function () { - return data; - }; - - return data; -} - -function _templateObject2() { - const data = _taggedTemplateLiteralLoose([void 0], ["\\01"]); - - _templateObject2 = function () { - return data; - }; - - return data; -} - -function _templateObject() { - const data = _taggedTemplateLiteralLoose([void 0], ["\\unicode and \\u{55}"]); - - _templateObject = function () { - return data; - }; - - return data; -} +var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8; function _taggedTemplateLiteralLoose(strings, raw) { if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; } -tag(_templateObject()); -tag(_templateObject2()); -tag(_templateObject3(), 0); -tag(_templateObject4(), 0); -tag(_templateObject5(), 0, 1); -tag(_templateObject6(), 0, 1); -tag(_templateObject7(), 0, 1); +tag(_templateObject || (_templateObject = _taggedTemplateLiteralLoose([void 0], ["\\unicode and \\u{55}"]))); +tag(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose([void 0], ["\\01"]))); +tag(_templateObject3 || (_templateObject3 = _taggedTemplateLiteralLoose([void 0, "right"], ["\\xg", "right"])), 0); +tag(_templateObject4 || (_templateObject4 = _taggedTemplateLiteralLoose(["left", void 0], ["left", "\\xg"])), 0); +tag(_templateObject5 || (_templateObject5 = _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\xg", "right"])), 0, 1); +tag(_templateObject6 || (_templateObject6 = _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u000g", "right"])), 0, 1); +tag(_templateObject7 || (_templateObject7 = _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u{-0}", "right"])), 0, 1); function a() { var undefined = 4; - tag(_templateObject8()); + tag(_templateObject8 || (_templateObject8 = _taggedTemplateLiteralLoose([void 0], ["\\01"]))); } diff --git a/packages/babel-plugin-transform-unicode-escapes/test/fixtures/unicode-escapes/tagged-template-transformed/output.js b/packages/babel-plugin-transform-unicode-escapes/test/fixtures/unicode-escapes/tagged-template-transformed/output.js index 4f8fa85f21..7dc1fc8000 100644 --- a/packages/babel-plugin-transform-unicode-escapes/test/fixtures/unicode-escapes/tagged-template-transformed/output.js +++ b/packages/babel-plugin-transform-unicode-escapes/test/fixtures/unicode-escapes/tagged-template-transformed/output.js @@ -1,57 +1,9 @@ -function _templateObject5() { - const data = _taggedTemplateLiteral(["\\\\u{1d49c}"], ["\\\\\\\\u{1d49c}"]); - - _templateObject5 = function () { - return data; - }; - - return data; -} - -function _templateObject4() { - const data = _taggedTemplateLiteral(["\\\uD835\uDC9C"], ["\\\\\\u{1d49c}"]); - - _templateObject4 = function () { - return data; - }; - - return data; -} - -function _templateObject3() { - const data = _taggedTemplateLiteral(["\\u{1d49c}"], ["\\\\u{1d49c}"]); - - _templateObject3 = function () { - return data; - }; - - return data; -} - -function _templateObject2() { - const data = _taggedTemplateLiteral(["\uD835\uDC9C"], ["\\u{1d49c}"]); - - _templateObject2 = function () { - return data; - }; - - return data; -} - -function _templateObject() { - const data = _taggedTemplateLiteral(["\uD835\uDC9C\uD835\uDC9C\uD835\uDC9C"], ["\uD835\uDC9C\\ud835\\udc9c\\u{1d49c}"]); - - _templateObject = function () { - return data; - }; - - return data; -} +var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5; function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } -test(_templateObject()); -test(_templateObject2()); -test(_templateObject3()); -test(_templateObject4()); -test(_templateObject5()); +test(_templateObject || (_templateObject = _taggedTemplateLiteral(["\uD835\uDC9C\uD835\uDC9C\uD835\uDC9C"], ["\uD835\uDC9C\\ud835\\udc9c\\u{1d49c}"]))); +test(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\uD835\uDC9C"], ["\\u{1d49c}"]))); +test(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\\u{1d49c}"], ["\\\\u{1d49c}"]))); +test(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\\\uD835\uDC9C"], ["\\\\\\u{1d49c}"]))); +test(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\\\\u{1d49c}"], ["\\\\\\\\u{1d49c}"]))); diff --git a/packages/babel-preset-env/test/fixtures/preset-options/safari-tagged-template-literals/output.js b/packages/babel-preset-env/test/fixtures/preset-options/safari-tagged-template-literals/output.js index bbeb47d3ed..c6a1ea9a46 100644 --- a/packages/babel-preset-env/test/fixtures/preset-options/safari-tagged-template-literals/output.js +++ b/packages/babel-preset-env/test/fixtures/preset-options/safari-tagged-template-literals/output.js @@ -1,13 +1,5 @@ -function _templateObject() { - var data = _taggedTemplateLiteral(["Safari 12 borked"]); - - _templateObject = function _templateObject() { - return data; - }; - - return data; -} +var _templateObject; function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } -tag(_templateObject()); +tag(_templateObject || (_templateObject = _taggedTemplateLiteral(["Safari 12 borked"])));