Remove tagged template literal global caching (#7722)
* Remove tagged template literal global caching Fixes #7350. * Review comments * assert output
This commit is contained in:
parent
7a106025ea
commit
29eafbbf44
@ -45,9 +45,6 @@ export default declare((api, options) => {
|
||||
}
|
||||
|
||||
return {
|
||||
pre() {
|
||||
this.templates = new Map();
|
||||
},
|
||||
visitor: {
|
||||
TaggedTemplateExpression(path) {
|
||||
const { node } = path;
|
||||
@ -75,41 +72,27 @@ export default declare((api, options) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a unique name based on the string literals so we dedupe
|
||||
// identical strings used in the program.
|
||||
const rawParts = raws.map(s => s.value).join(",");
|
||||
const name = `${helperName}_${raws.length}_${rawParts}`;
|
||||
const scope = path.scope.getProgramParent();
|
||||
const templateObject = scope.generateUidIdentifier("templateObject");
|
||||
|
||||
let templateObject = this.templates.get(name);
|
||||
if (templateObject) {
|
||||
templateObject = t.cloneNode(templateObject);
|
||||
} else {
|
||||
const programPath = path.find(p => p.isProgram());
|
||||
templateObject = programPath.scope.generateUidIdentifier(
|
||||
"templateObject",
|
||||
);
|
||||
this.templates.set(name, templateObject);
|
||||
const helperId = this.addHelper(helperName);
|
||||
const callExpressionInput = [t.arrayExpression(strings)];
|
||||
|
||||
const helperId = this.addHelper(helperName);
|
||||
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({
|
||||
id: templateObject,
|
||||
init,
|
||||
// This ensures that we don't fail if not using function expression helpers
|
||||
_blockHoist: 1.9,
|
||||
});
|
||||
// only add raw arrayExpression if there is any difference between raws and strings
|
||||
if (!isStringsRawEqual) {
|
||||
callExpressionInput.push(t.arrayExpression(raws));
|
||||
}
|
||||
|
||||
const init = t.callExpression(helperId, callExpressionInput);
|
||||
annotateAsPure(init);
|
||||
init._compact = true;
|
||||
scope.push({
|
||||
id: templateObject,
|
||||
init,
|
||||
// This ensures that we don't fail if not using function expression helpers
|
||||
_blockHoist: 1.9,
|
||||
});
|
||||
|
||||
path.replaceWith(
|
||||
t.callExpression(node.tag, [
|
||||
t.cloneNode(templateObject),
|
||||
|
||||
15
packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/exec.js
vendored
Normal file
15
packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/exec.js
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
var tag = v => v;
|
||||
|
||||
function foo() {
|
||||
return tag`some template`;
|
||||
}
|
||||
function bar() {
|
||||
return tag`some template`;
|
||||
}
|
||||
expect(foo()).toBe(foo());
|
||||
expect(foo()).toEqual(["some template"]);
|
||||
|
||||
expect(bar()).toBe(bar());
|
||||
expect(bar()).toEqual(["some template"]);
|
||||
|
||||
expect(bar()).not.toBe(foo());
|
||||
15
packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/input.js
vendored
Normal file
15
packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/input.js
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
var tag = v => v;
|
||||
|
||||
function foo() {
|
||||
return tag`some template`;
|
||||
}
|
||||
function bar() {
|
||||
return tag`some template`;
|
||||
}
|
||||
expect(foo()).toBe(foo());
|
||||
expect(foo()).toEqual(["some template"]);
|
||||
|
||||
expect(bar()).toBe(bar());
|
||||
expect(bar()).toEqual(["some template"]);
|
||||
|
||||
expect(bar()).not.toBe(foo());
|
||||
20
packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js
vendored
Normal file
20
packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["some template"]),
|
||||
_templateObject2 = /*#__PURE__*/ _taggedTemplateLiteral(["some template"]);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function bar() {
|
||||
return tag(_templateObject2);
|
||||
}
|
||||
|
||||
expect(foo()).toBe(foo());
|
||||
expect(foo()).toEqual(["some template"]);
|
||||
expect(bar()).toBe(bar());
|
||||
expect(bar()).toEqual(["some template"]);
|
||||
expect(bar()).not.toBe(foo());
|
||||
@ -4,7 +4,8 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral([void 0], ["\\unicode
|
||||
_templateObject4 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0], ["left", "\\xg"]),
|
||||
_templateObject5 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\xg", "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"]),
|
||||
_templateObject8 = /*#__PURE__*/ _taggedTemplateLiteral([void 0], ["\\01"]);
|
||||
|
||||
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
|
||||
|
||||
@ -18,5 +19,5 @@ tag(_templateObject7, 0, 1);
|
||||
|
||||
function a() {
|
||||
var undefined = 4;
|
||||
tag(_templateObject2);
|
||||
tag(_templateObject8);
|
||||
}
|
||||
|
||||
@ -4,7 +4,8 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteralLoose([void 0], ["\\un
|
||||
_templateObject4 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0], ["left", "\\xg"]),
|
||||
_templateObject5 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\xg", "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"]),
|
||||
_templateObject8 = /*#__PURE__*/ _taggedTemplateLiteralLoose([void 0], ["\\01"]);
|
||||
|
||||
function _taggedTemplateLiteralLoose(strings, raw) { if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; }
|
||||
|
||||
@ -18,5 +19,5 @@ tag(_templateObject7, 0, 1);
|
||||
|
||||
function a() {
|
||||
var undefined = 4;
|
||||
tag(_templateObject2);
|
||||
tag(_templateObject8);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user