Skip to content

Commit c9ffb83

Browse files
authored
Merge pull request #14861 from webpack/fix-runtime-template
fix RuntimeTemplate
2 parents 4127edb + c106e43 commit c9ffb83

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

lib/RuntimeTemplate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class RuntimeTemplate {
200200

201201
// when the first two args are expression, we need to prepend "" + to force string
202202
// concatenation instead of number addition.
203-
return typeof args[0] !== "string" && args[1] !== "string"
203+
return typeof args[0] !== "string" && typeof args[1] !== "string"
204204
? `"" + ${str}`
205205
: str;
206206
}

test/RuntimeTemplate.unittest.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"use strict";
2+
3+
const RuntimeTemplate = require("../lib/RuntimeTemplate");
4+
const RequestShortener = require("../lib/RequestShortener");
5+
6+
describe("RuntimeTemplate.concatenation", () => {
7+
it("no args", () => {
8+
const runtimeTemplate = new RuntimeTemplate(
9+
undefined,
10+
{ environment: { templateLiteral: false } },
11+
new RequestShortener(__dirname)
12+
);
13+
expect(runtimeTemplate.concatenation()).toBe('""');
14+
});
15+
16+
it("1 arg", () => {
17+
const runtimeTemplate = new RuntimeTemplate(
18+
undefined,
19+
{ environment: { templateLiteral: false } },
20+
new RequestShortener(__dirname)
21+
);
22+
expect(runtimeTemplate.concatenation({ expr: 1 })).toBe('"" + 1');
23+
expect(runtimeTemplate.concatenation("str")).toBe('"str"');
24+
});
25+
26+
it("es5", () => {
27+
const runtimeTemplate = new RuntimeTemplate(
28+
undefined,
29+
{ environment: { templateLiteral: false } },
30+
new RequestShortener(__dirname)
31+
);
32+
33+
expect(
34+
runtimeTemplate.concatenation({ expr: "__webpack__.p" }, "str/a")
35+
).toBe('__webpack__.p + "str/a"');
36+
expect(
37+
runtimeTemplate.concatenation(
38+
{ expr: "__webpack__.p" },
39+
{ expr: "str.a" },
40+
"str"
41+
)
42+
).toBe('"" + __webpack__.p + str.a + "str"');
43+
expect(runtimeTemplate.concatenation("a", "b", { expr: 1 })).toBe(
44+
'"a" + "b" + 1'
45+
);
46+
expect(runtimeTemplate.concatenation("a", { expr: 1 }, "b")).toBe(
47+
'"a" + 1 + "b"'
48+
);
49+
});
50+
51+
describe("es6", () => {
52+
const runtimeTemplate = new RuntimeTemplate(
53+
undefined,
54+
{ environment: { templateLiteral: true } },
55+
new RequestShortener(__dirname)
56+
);
57+
58+
it("should prefer shorten variant #1", () => {
59+
expect(runtimeTemplate.concatenation({ expr: 1 }, "a", { expr: 2 })).toBe(
60+
'1 + "a" + 2'
61+
);
62+
});
63+
64+
it("should prefer shorten variant #2", () => {
65+
expect(
66+
runtimeTemplate.concatenation({ expr: 1 }, "a", { expr: 2 }, "b")
67+
).toBe('1 + "a" + 2 + "b"');
68+
});
69+
70+
it("should prefer shorten variant #3", () => {
71+
/* eslint-disable no-template-curly-in-string */
72+
expect(runtimeTemplate.concatenation("a", { expr: 1 }, "b")).toBe(
73+
"`a${1}b`"
74+
);
75+
/* eslint-enable */
76+
});
77+
});
78+
});

0 commit comments

Comments
 (0)