Skip to content

Commit bdfbcfe

Browse files
committed
Make jsesc isScriptContext optional
+ Adds option isScriptContext to constant folding plugin + Fix #440, fix #413 + Related #384, #382
1 parent f2e57e1 commit bdfbcfe

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ jest.autoMockOff();
33
const babel = require("babel-core");
44
const unpad = require("../../../utils/unpad");
55

6-
function transform(code) {
6+
function transform(code, opts = {}) {
77
return babel.transform(code, {
8-
plugins: [require("../src/index")]
8+
plugins: [[require("../src/index"), opts]]
99
}).code;
1010
}
1111

@@ -83,7 +83,7 @@ describe("constant-folding-plugin", () => {
8383
"<\\\\/script";
8484
`
8585
);
86-
expect(transform(source)).toBe(expected);
86+
expect(transform(source, { isScriptContext: true })).toBe(expected);
8787
});
8888

8989
it("should handle style escape", () => {
@@ -98,7 +98,7 @@ describe("constant-folding-plugin", () => {
9898
"<\\\\/style";
9999
`
100100
);
101-
expect(transform(source)).toBe(expected);
101+
expect(transform(source, { isScriptContext: true })).toBe(expected);
102102
});
103103

104104
it("should handle html comment escape", () => {
@@ -113,6 +113,20 @@ describe("constant-folding-plugin", () => {
113113
"\\\\x3C!--";
114114
`
115115
);
116+
expect(transform(source, { isScriptContext: true })).toBe(expected);
117+
});
118+
119+
it("should fix #440", () => {
120+
const source = unpad(
121+
`
122+
var x = "'cool'" + "test";
123+
`
124+
);
125+
const expected = unpad(
126+
`
127+
var x = "'cool'test";
128+
`
129+
);
116130
expect(transform(source)).toBe(expected);
117131
});
118132
});

packages/babel-plugin-minify-constant-folding/src/index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = ({ types: t, traverse }) => {
5555
},
5656

5757
// TODO: look into evaluating binding too (could result in more code, but gzip?)
58-
Expression(path) {
58+
Expression(path, { opts: { isScriptContext = false } }) {
5959
const { node } = path;
6060

6161
if (node[seen]) {
@@ -122,10 +122,8 @@ module.exports = ({ types: t, traverse }) => {
122122
}
123123

124124
// https://github.com/babel/babili/issues/382
125-
if (typeof res.value === "string") {
126-
res.value = jsesc(res.value, {
127-
isScriptContext: true
128-
});
125+
if (typeof res.value === "string" && isScriptContext) {
126+
res.value = jsesc(res.value, { isScriptContext });
129127
}
130128

131129
const node = t.valueToNode(res.value);

0 commit comments

Comments
 (0)