Move template object creation from core into the template transform. (#6307)
* Move template object creation into the template transform. * use shorthand [skip ci]
This commit is contained in:
parent
0379060f8a
commit
5a2a5fb411
@ -263,34 +263,10 @@ export default class File extends Store {
|
|||||||
return uid;
|
return uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
addTemplateObject(
|
addTemplateObject() {
|
||||||
helperName: string,
|
throw new Error(
|
||||||
strings: Array<Object>,
|
"This function has been moved into the template literal transform itself.",
|
||||||
raw: Object,
|
);
|
||||||
): Object {
|
|
||||||
// Generate a unique name based on the string literals so we dedupe
|
|
||||||
// identical strings used in the program.
|
|
||||||
const stringIds = raw.elements.map(function(string) {
|
|
||||||
return string.value;
|
|
||||||
});
|
|
||||||
const name = `${helperName}_${raw.elements.length}_${stringIds.join(",")}`;
|
|
||||||
|
|
||||||
const declar = this.declarations[name];
|
|
||||||
if (declar) return declar;
|
|
||||||
|
|
||||||
const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
|
|
||||||
"templateObject",
|
|
||||||
));
|
|
||||||
|
|
||||||
const helperId = this.addHelper(helperName);
|
|
||||||
const init = t.callExpression(helperId, [strings, raw]);
|
|
||||||
init._compact = true;
|
|
||||||
this.scope.push({
|
|
||||||
id: uid,
|
|
||||||
init: init,
|
|
||||||
_blockHoist: 1.9, // This ensures that we don't fail if not using function expression helpers
|
|
||||||
});
|
|
||||||
return uid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buildCodeFrameError(
|
buildCodeFrameError(
|
||||||
|
|||||||
@ -35,6 +35,9 @@ export default function({ types: t }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
pre() {
|
||||||
|
this.templates = new Map();
|
||||||
|
},
|
||||||
visitor: {
|
visitor: {
|
||||||
TaggedTemplateExpression(path, state) {
|
TaggedTemplateExpression(path, state) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
@ -54,18 +57,41 @@ export default function({ types: t }) {
|
|||||||
raws.push(t.stringLiteral(raw));
|
raws.push(t.stringLiteral(raw));
|
||||||
}
|
}
|
||||||
|
|
||||||
let templateName = "taggedTemplateLiteral";
|
let helperName = "taggedTemplateLiteral";
|
||||||
if (state.opts.loose) templateName += "Loose";
|
if (state.opts.loose) helperName += "Loose";
|
||||||
|
|
||||||
const templateObject = state.file.addTemplateObject(
|
// Generate a unique name based on the string literals so we dedupe
|
||||||
templateName,
|
// identical strings used in the program.
|
||||||
t.arrayExpression(strings),
|
const rawParts = raws.map(s => s.value).join(",");
|
||||||
t.arrayExpression(raws),
|
const name = `${helperName}_${raws.length}_${rawParts}`;
|
||||||
|
|
||||||
|
let templateObject = this.templates.get(name);
|
||||||
|
if (templateObject) {
|
||||||
|
templateObject = t.clone(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 init = t.callExpression(helperId, [
|
||||||
|
t.arrayExpression(strings),
|
||||||
|
t.arrayExpression(raws),
|
||||||
|
]);
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
path.replaceWith(
|
||||||
|
t.callExpression(node.tag, [templateObject, ...quasi.expressions]),
|
||||||
);
|
);
|
||||||
|
|
||||||
const args = [templateObject].concat(quasi.expressions);
|
|
||||||
|
|
||||||
path.replaceWith(t.callExpression(node.tag, args));
|
|
||||||
},
|
},
|
||||||
|
|
||||||
TemplateLiteral(path, state) {
|
TemplateLiteral(path, state) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user