Solves Tagged template literal size optimization (#7379)
* Tagged template literal size optimization solves #7352 * Incorporates review changes
This commit is contained in:
parent
7e90d56024
commit
dad05ed503
@ -690,6 +690,7 @@ helpers.slicedToArrayLoose = defineHelper(`
|
|||||||
|
|
||||||
helpers.taggedTemplateLiteral = defineHelper(`
|
helpers.taggedTemplateLiteral = defineHelper(`
|
||||||
export default function _taggedTemplateLiteral(strings, raw) {
|
export default function _taggedTemplateLiteral(strings, raw) {
|
||||||
|
if (!raw) { raw = strings.slice(0); }
|
||||||
return Object.freeze(Object.defineProperties(strings, {
|
return Object.freeze(Object.defineProperties(strings, {
|
||||||
raw: { value: Object.freeze(raw) }
|
raw: { value: Object.freeze(raw) }
|
||||||
}));
|
}));
|
||||||
@ -698,6 +699,7 @@ helpers.taggedTemplateLiteral = defineHelper(`
|
|||||||
|
|
||||||
helpers.taggedTemplateLiteralLoose = defineHelper(`
|
helpers.taggedTemplateLiteralLoose = defineHelper(`
|
||||||
export default function _taggedTemplateLiteralLoose(strings, raw) {
|
export default function _taggedTemplateLiteralLoose(strings, raw) {
|
||||||
|
if (!raw) { raw = strings.slice(0); }
|
||||||
strings.raw = raw;
|
strings.raw = raw;
|
||||||
return strings;
|
return strings;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral");
|
var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral");
|
||||||
|
|
||||||
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"], ["foo"]);
|
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"]);
|
||||||
|
|
||||||
tag(_templateObject);
|
tag(_templateObject);
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral");
|
var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral");
|
||||||
|
|
||||||
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"], ["foo"]);
|
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"]);
|
||||||
|
|
||||||
tag(_templateObject);
|
tag(_templateObject);
|
||||||
|
|||||||
@ -54,6 +54,9 @@ export default function(api, options) {
|
|||||||
const strings = [];
|
const strings = [];
|
||||||
const raws = [];
|
const raws = [];
|
||||||
|
|
||||||
|
// Flag variable to check if contents of strings and raw are equal
|
||||||
|
let isStringsRawEqual = true;
|
||||||
|
|
||||||
for (const elem of (quasi.quasis: Array)) {
|
for (const elem of (quasi.quasis: Array)) {
|
||||||
const { raw, cooked } = elem.value;
|
const { raw, cooked } = elem.value;
|
||||||
const value =
|
const value =
|
||||||
@ -63,6 +66,11 @@ export default function(api, options) {
|
|||||||
|
|
||||||
strings.push(value);
|
strings.push(value);
|
||||||
raws.push(t.stringLiteral(raw));
|
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
|
// 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);
|
this.templates.set(name, templateObject);
|
||||||
|
|
||||||
const helperId = this.addHelper(helperName);
|
const helperId = this.addHelper(helperName);
|
||||||
const init = t.callExpression(helperId, [
|
const callExpressionInput = [];
|
||||||
t.arrayExpression(strings),
|
callExpressionInput.push(t.arrayExpression(strings));
|
||||||
t.arrayExpression(raws),
|
|
||||||
]);
|
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);
|
annotateAsPure(init);
|
||||||
init._compact = true;
|
init._compact = true;
|
||||||
programPath.scope.push({
|
programPath.scope.push({
|
||||||
|
|||||||
2
packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/input.js
vendored
Normal file
2
packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
var foo = tag`wow`;
|
||||||
|
var bar = tag`first${1}second`;
|
||||||
7
packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/output.js
vendored
Normal file
7
packages/babel-plugin-transform-template-literals/test/fixtures/default/simple-tag/output.js
vendored
Normal 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);
|
||||||
@ -2,7 +2,7 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["wow\na", "b ", ""],
|
|||||||
_templateObject2 = /*#__PURE__*/ _taggedTemplateLiteral(["wow\nab", " ", ""], ["wow\\nab", " ", ""]),
|
_templateObject2 = /*#__PURE__*/ _taggedTemplateLiteral(["wow\nab", " ", ""], ["wow\\nab", " ", ""]),
|
||||||
_templateObject3 = /*#__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 foo = bar(_templateObject, 42, _.foobar());
|
||||||
var bar = bar(_templateObject2, 42, _.foobar());
|
var bar = bar(_templateObject2, 42, _.foobar());
|
||||||
|
|||||||
@ -6,7 +6,7 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral([void 0], ["\\unicode
|
|||||||
_templateObject6 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u000g", "right"]),
|
_templateObject6 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u000g", "right"]),
|
||||||
_templateObject7 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u{-0}", "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(_templateObject);
|
||||||
tag(_templateObject2);
|
tag(_templateObject2);
|
||||||
|
|||||||
@ -2,7 +2,7 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteralLoose(["wow\na", "b ",
|
|||||||
_templateObject2 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["wow\nab", " ", ""], ["wow\\nab", " ", ""]),
|
_templateObject2 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["wow\nab", " ", ""], ["wow\\nab", " ", ""]),
|
||||||
_templateObject3 = /*#__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 foo = bar(_templateObject, 42, _.foobar());
|
||||||
var bar = bar(_templateObject2, 42, _.foobar());
|
var bar = bar(_templateObject2, 42, _.foobar());
|
||||||
|
|||||||
@ -6,7 +6,7 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteralLoose([void 0], ["\\un
|
|||||||
_templateObject6 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u000g", "right"]),
|
_templateObject6 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u000g", "right"]),
|
||||||
_templateObject7 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u{-0}", "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(_templateObject);
|
||||||
tag(_templateObject2);
|
tag(_templateObject2);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user