Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solves Tagged template literal size optimization #7379

Merged
merged 2 commits into from
Feb 20, 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
2 changes: 2 additions & 0 deletions packages/babel-helpers/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,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) }
}));
Expand All @@ -678,6 +679,7 @@ helpers.taggedTemplateLiteral = defineHelper(`

helpers.taggedTemplateLiteralLoose = defineHelper(`
export default function _taggedTemplateLiteralLoose(strings, raw) {
if (!raw) { raw = strings.slice(0); }
strings.raw = raw;
return strings;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral");

var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"], ["foo"]);
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"]);

tag(_templateObject);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _taggedTemplateLiteral = require("@babel/runtime/helpers/taggedTemplateLiteral");

var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"], ["foo"]);
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["foo"]);

tag(_templateObject);
21 changes: 17 additions & 4 deletions packages/babel-plugin-transform-template-literals/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var foo = tag`wow`;
Copy link
Member

Choose a reason for hiding this comment

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

Let’s add a multi quasi case:

var bar = tag`first${1}second`;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made this change

var bar = tag`first${1}second`;
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down