Skip to content

Commit fb12afc

Browse files
Declare template objects inline (#12588)
1 parent ed90f17 commit fb12afc

12 files changed

Lines changed: 55 additions & 390 deletions

File tree

  • packages
    • babel-plugin-transform-modules-commonjs/test/fixtures
    • babel-plugin-transform-template-literals
    • babel-plugin-transform-unicode-escapes/test/fixtures/unicode-escapes/tagged-template-transformed
    • babel-preset-env/test/fixtures/preset-options/safari-tagged-template-literals

packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop-loose/imports-hoisting/output.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
44

55
var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral"));
66

7-
function _templateObject() {
8-
const data = (0, _taggedTemplateLiteral2.default)(["foo"]);
7+
var _templateObject;
98

10-
_templateObject = function () {
11-
return data;
12-
};
13-
14-
return data;
15-
}
16-
17-
tag(_templateObject());
9+
tag(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["foo"])));

packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/imports-hoisting/output.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
44

55
var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral"));
66

7-
function _templateObject() {
8-
const data = (0, _taggedTemplateLiteral2.default)(["foo"]);
7+
var _templateObject;
98

10-
_templateObject = function () {
11-
return data;
12-
};
13-
14-
return data;
15-
}
16-
17-
tag(_templateObject());
9+
tag(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["foo"])));

packages/babel-plugin-transform-template-literals/src/index.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,22 @@ export default declare((api, options) => {
7373
}
7474
}
7575

76-
const scope = path.scope.getProgramParent();
77-
const templateObject = scope.generateUidIdentifier("templateObject");
78-
79-
const helperId = this.addHelper(helperName);
80-
const callExpressionInput = [t.arrayExpression(strings)];
81-
76+
const helperArgs = [t.arrayExpression(strings)];
8277
// only add raw arrayExpression if there is any difference between raws and strings
8378
if (!isStringsRawEqual) {
84-
callExpressionInput.push(t.arrayExpression(raws));
79+
helperArgs.push(t.arrayExpression(raws));
8580
}
8681

87-
const lazyLoad = template.ast`
88-
function ${templateObject}() {
89-
const data = ${t.callExpression(helperId, callExpressionInput)};
90-
${t.cloneNode(templateObject)} = function() { return data };
91-
return data;
92-
}
93-
`;
82+
const tmp = path.scope.generateUidIdentifier("templateObject");
83+
path.scope.getProgramParent().push({ id: t.cloneNode(tmp) });
9484

95-
scope.path.unshiftContainer("body", lazyLoad);
9685
path.replaceWith(
9786
t.callExpression(node.tag, [
98-
t.callExpression(t.cloneNode(templateObject), []),
87+
template.expression.ast`
88+
${t.cloneNode(tmp)} || (
89+
${tmp} = ${this.addHelper(helperName)}(${helperArgs})
90+
)
91+
`,
9992
...quasi.expressions,
10093
]),
10194
);

packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
1-
function _templateObject2() {
2-
const data = _taggedTemplateLiteral(["some template"]);
3-
4-
_templateObject2 = function () {
5-
return data;
6-
};
7-
8-
return data;
9-
}
10-
11-
function _templateObject() {
12-
const data = _taggedTemplateLiteral(["some template"]);
13-
14-
_templateObject = function () {
15-
return data;
16-
};
17-
18-
return data;
19-
}
1+
var _templateObject, _templateObject2;
202

213
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
224

235
var tag = v => v;
246

257
function foo() {
26-
return tag(_templateObject());
8+
return tag(_templateObject || (_templateObject = _taggedTemplateLiteral(["some template"])));
279
}
2810

2911
function bar() {
30-
return tag(_templateObject2());
12+
return tag(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["some template"])));
3113
}
3214

3315
expect(foo()).toBe(foo());
Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
1-
function _templateObject2() {
2-
const data = _taggedTemplateLiteral(["first", "second"]);
3-
4-
_templateObject2 = function () {
5-
return data;
6-
};
7-
8-
return data;
9-
}
10-
11-
function _templateObject() {
12-
const data = _taggedTemplateLiteral(["wow"]);
13-
14-
_templateObject = function () {
15-
return data;
16-
};
17-
18-
return data;
19-
}
1+
var _templateObject, _templateObject2;
202

213
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
224

23-
var foo = tag(_templateObject());
24-
var bar = tag(_templateObject2(), 1);
5+
var foo = tag(_templateObject || (_templateObject = _taggedTemplateLiteral(["wow"])));
6+
var bar = tag(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["first", "second"])), 1);
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
function _templateObject() {
2-
const data = _taggedTemplateLiteral(["aa\uD835\uDC9C\uD835\uDC9C"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"]);
3-
4-
_templateObject = function () {
5-
return data;
6-
};
7-
8-
return data;
9-
}
1+
var _templateObject;
102

113
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
124

13-
var foo = bar(_templateObject());
5+
var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["aa\uD835\uDC9C\uD835\uDC9C"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"])));
Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
1-
function _templateObject3() {
2-
const data = _taggedTemplateLiteral(["wow\naB", " ", ""], ["wow\\naB", " ", ""]);
3-
4-
_templateObject3 = function () {
5-
return data;
6-
};
7-
8-
return data;
9-
}
10-
11-
function _templateObject2() {
12-
const data = _taggedTemplateLiteral(["wow\nab", " ", ""], ["wow\\nab", " ", ""]);
13-
14-
_templateObject2 = function () {
15-
return data;
16-
};
17-
18-
return data;
19-
}
20-
21-
function _templateObject() {
22-
const data = _taggedTemplateLiteral(["wow\na", "b ", ""], ["wow\\na", "b ", ""]);
23-
24-
_templateObject = function () {
25-
return data;
26-
};
27-
28-
return data;
29-
}
1+
var _templateObject, _templateObject2, _templateObject3;
302

313
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
324

33-
var foo = bar(_templateObject(), 42, _.foobar());
34-
var bar = bar(_templateObject2(), 42, _.foobar());
35-
var bar = bar(_templateObject3(), 42, _.baz());
5+
var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["wow\na", "b ", ""], ["wow\\na", "b ", ""])), 42, _.foobar());
6+
var bar = bar(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["wow\nab", " ", ""], ["wow\\nab", " ", ""])), 42, _.foobar());
7+
var bar = bar(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["wow\naB", " ", ""], ["wow\\naB", " ", ""])), 42, _.baz());
Lines changed: 9 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,16 @@
1-
function _templateObject8() {
2-
const data = _taggedTemplateLiteral([void 0], ["\\01"]);
3-
4-
_templateObject8 = function () {
5-
return data;
6-
};
7-
8-
return data;
9-
}
10-
11-
function _templateObject7() {
12-
const data = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u{-0}", "right"]);
13-
14-
_templateObject7 = function () {
15-
return data;
16-
};
17-
18-
return data;
19-
}
20-
21-
function _templateObject6() {
22-
const data = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u000g", "right"]);
23-
24-
_templateObject6 = function () {
25-
return data;
26-
};
27-
28-
return data;
29-
}
30-
31-
function _templateObject5() {
32-
const data = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\xg", "right"]);
33-
34-
_templateObject5 = function () {
35-
return data;
36-
};
37-
38-
return data;
39-
}
40-
41-
function _templateObject4() {
42-
const data = _taggedTemplateLiteral(["left", void 0], ["left", "\\xg"]);
43-
44-
_templateObject4 = function () {
45-
return data;
46-
};
47-
48-
return data;
49-
}
50-
51-
function _templateObject3() {
52-
const data = _taggedTemplateLiteral([void 0, "right"], ["\\xg", "right"]);
53-
54-
_templateObject3 = function () {
55-
return data;
56-
};
57-
58-
return data;
59-
}
60-
61-
function _templateObject2() {
62-
const data = _taggedTemplateLiteral([void 0], ["\\01"]);
63-
64-
_templateObject2 = function () {
65-
return data;
66-
};
67-
68-
return data;
69-
}
70-
71-
function _templateObject() {
72-
const data = _taggedTemplateLiteral([void 0], ["\\unicode and \\u{55}"]);
73-
74-
_templateObject = function () {
75-
return data;
76-
};
77-
78-
return data;
79-
}
1+
var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8;
802

813
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
824

83-
tag(_templateObject());
84-
tag(_templateObject2());
85-
tag(_templateObject3(), 0);
86-
tag(_templateObject4(), 0);
87-
tag(_templateObject5(), 0, 1);
88-
tag(_templateObject6(), 0, 1);
89-
tag(_templateObject7(), 0, 1);
5+
tag(_templateObject || (_templateObject = _taggedTemplateLiteral([void 0], ["\\unicode and \\u{55}"])));
6+
tag(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral([void 0], ["\\01"])));
7+
tag(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral([void 0, "right"], ["\\xg", "right"])), 0);
8+
tag(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["left", void 0], ["left", "\\xg"])), 0);
9+
tag(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\xg", "right"])), 0, 1);
10+
tag(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u000g", "right"])), 0, 1);
11+
tag(_templateObject7 || (_templateObject7 = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u{-0}", "right"])), 0, 1);
9012

9113
function a() {
9214
var undefined = 4;
93-
tag(_templateObject8());
15+
tag(_templateObject8 || (_templateObject8 = _taggedTemplateLiteral([void 0], ["\\01"])));
9416
}
Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
1-
function _templateObject3() {
2-
const data = _taggedTemplateLiteralLoose(["wow\naB", " ", ""], ["wow\\naB", " ", ""]);
3-
4-
_templateObject3 = function () {
5-
return data;
6-
};
7-
8-
return data;
9-
}
10-
11-
function _templateObject2() {
12-
const data = _taggedTemplateLiteralLoose(["wow\nab", " ", ""], ["wow\\nab", " ", ""]);
13-
14-
_templateObject2 = function () {
15-
return data;
16-
};
17-
18-
return data;
19-
}
20-
21-
function _templateObject() {
22-
const data = _taggedTemplateLiteralLoose(["wow\na", "b ", ""], ["wow\\na", "b ", ""]);
23-
24-
_templateObject = function () {
25-
return data;
26-
};
27-
28-
return data;
29-
}
1+
var _templateObject, _templateObject2, _templateObject3;
302

313
function _taggedTemplateLiteralLoose(strings, raw) { if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; }
324

33-
var foo = bar(_templateObject(), 42, _.foobar());
34-
var bar = bar(_templateObject2(), 42, _.foobar());
35-
var bar = bar(_templateObject3(), 42, _.baz());
5+
var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["wow\na", "b ", ""], ["wow\\na", "b ", ""])), 42, _.foobar());
6+
var bar = bar(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose(["wow\nab", " ", ""], ["wow\\nab", " ", ""])), 42, _.foobar());
7+
var bar = bar(_templateObject3 || (_templateObject3 = _taggedTemplateLiteralLoose(["wow\naB", " ", ""], ["wow\\naB", " ", ""])), 42, _.baz());

0 commit comments

Comments
 (0)