Solves Tagged template literal size optimization (#7379)

* Tagged template literal size optimization
solves #7352

* Incorporates review changes
This commit is contained in:
Deepak Pai 2018-02-20 21:39:23 +01:00 committed by Justin Ridgewell
parent 7e90d56024
commit dad05ed503
10 changed files with 34 additions and 10 deletions

View File

@ -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;
}

View File

@ -1,5 +1,5 @@
var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral");
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"], ["foo"]);
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"]);
tag(_templateObject);

View File

@ -1,5 +1,5 @@
var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral");
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"], ["foo"]);
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"]);
tag(_templateObject);

View File

@ -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({

View File

@ -0,0 +1,2 @@
var foo = tag`wow`;
var bar = tag`first${1}second`;

View File

@ -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);

View File

@ -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());

View File

@ -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);

View File

@ -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());

View File

@ -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);