[babel 8] Output minimal strings by default (#12675)
Co-authored-by: Justin Ridgewell <justin@ridgewell.name> Co-authored-by: Justin Ridgewell <jridgewell@google.com>
This commit is contained in:
parent
464a02ffe1
commit
cd090e9842
@ -20,7 +20,7 @@
|
|||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/types": "workspace:^7.12.11",
|
"@babel/types": "workspace:^7.12.11",
|
||||||
"jsesc": "^2.5.1",
|
"jsesc": "condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1",
|
||||||
"source-map": "^0.5.0"
|
"source-map": "^0.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@ -203,8 +203,6 @@ export function StringLiteral(node: Object) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure the output is ASCII-safe
|
|
||||||
|
|
||||||
const val = jsesc(
|
const val = jsesc(
|
||||||
node.value,
|
node.value,
|
||||||
process.env.BABEL_8_BREAKING
|
process.env.BABEL_8_BREAKING
|
||||||
|
|||||||
@ -55,6 +55,7 @@ function normalizeOptions(code, opts): Format {
|
|||||||
jsescOption: {
|
jsescOption: {
|
||||||
quotes: "double",
|
quotes: "double",
|
||||||
wrap: true,
|
wrap: true,
|
||||||
|
minimal: true,
|
||||||
...opts.jsescOption,
|
...opts.jsescOption,
|
||||||
},
|
},
|
||||||
recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType,
|
recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType,
|
||||||
@ -62,6 +63,7 @@ function normalizeOptions(code, opts): Format {
|
|||||||
|
|
||||||
if (!process.env.BABEL_8_BREAKING) {
|
if (!process.env.BABEL_8_BREAKING) {
|
||||||
format.jsonCompatibleStrings = opts.jsonCompatibleStrings;
|
format.jsonCompatibleStrings = opts.jsonCompatibleStrings;
|
||||||
|
delete format.jsescOption.minimal;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format.minified) {
|
if (format.minified) {
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
0;// Not a directive
|
0;// Not a directive
|
||||||
"\u00A9";
|
"©";
|
||||||
|
|||||||
18
packages/babel-generator/test/fixtures/minified/literals-babel-7/input.js
vendored
Normal file
18
packages/babel-generator/test/fixtures/minified/literals-babel-7/input.js
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
5;
|
||||||
|
5.0;
|
||||||
|
5e1;
|
||||||
|
5e4;
|
||||||
|
"foobar";
|
||||||
|
'\x20';
|
||||||
|
"\n\r";
|
||||||
|
"😂";
|
||||||
|
"\uD83D_\uDE02"
|
||||||
|
`😂`;
|
||||||
|
/foobar/g;
|
||||||
|
null;
|
||||||
|
true;
|
||||||
|
false;
|
||||||
|
5.;
|
||||||
|
0b10;
|
||||||
|
0o70;
|
||||||
|
0X1F;
|
||||||
3
packages/babel-generator/test/fixtures/minified/literals-babel-7/options.json
vendored
Normal file
3
packages/babel-generator/test/fixtures/minified/literals-babel-7/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": false
|
||||||
|
}
|
||||||
1
packages/babel-generator/test/fixtures/minified/literals-babel-7/output.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/minified/literals-babel-7/output.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
5;5;50;5e4;"foobar";" ";"\n\r";"\uD83D\uDE02";"\uD83D_\uDE02"`😂`;/foobar/g;null;true;false;5;2;56;31;
|
||||||
@ -6,6 +6,7 @@
|
|||||||
'\x20';
|
'\x20';
|
||||||
"\n\r";
|
"\n\r";
|
||||||
"😂";
|
"😂";
|
||||||
|
"\uD83D_\uDE02"
|
||||||
`😂`;
|
`😂`;
|
||||||
/foobar/g;
|
/foobar/g;
|
||||||
null;
|
null;
|
||||||
|
|||||||
3
packages/babel-generator/test/fixtures/minified/literals/options.json
vendored
Normal file
3
packages/babel-generator/test/fixtures/minified/literals/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": true
|
||||||
|
}
|
||||||
@ -1 +1 @@
|
|||||||
5;5;50;5e4;"foobar";" ";"\n\r";"\uD83D\uDE02";`😂`;/foobar/g;null;true;false;5;2;56;31;
|
5;5;50;5e4;"foobar";" ";"\n\r";"😂";"\uD83D_\uDE02"`😂`;/foobar/g;null;true;false;5;2;56;31;
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
<div id="wôw" />;
|
||||||
|
<div id="\w" />;
|
||||||
|
<div id="w < w" />;
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": false
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
import { jsx as _jsx } from "react/jsx-runtime";
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
id: "w\xF4w"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
id: "\\w"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
id: "w < w"
|
||||||
|
});
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": true
|
||||||
|
}
|
||||||
@ -2,7 +2,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|||||||
|
|
||||||
/*#__PURE__*/
|
/*#__PURE__*/
|
||||||
_jsx("div", {
|
_jsx("div", {
|
||||||
id: "w\xF4w"
|
id: "wôw"
|
||||||
});
|
});
|
||||||
|
|
||||||
/*#__PURE__*/
|
/*#__PURE__*/
|
||||||
|
|||||||
@ -0,0 +1,12 @@
|
|||||||
|
<div>wow</div>;
|
||||||
|
<div>wôw</div>;
|
||||||
|
|
||||||
|
<div>w & w</div>;
|
||||||
|
<div>w & w</div>;
|
||||||
|
|
||||||
|
<div>w w</div>;
|
||||||
|
<div>this should not parse as unicode: \u00a0</div>;
|
||||||
|
<div>this should parse as nbsp: </div>;
|
||||||
|
<div>this should parse as unicode: {'\u00a0 '}</div>;
|
||||||
|
|
||||||
|
<div>w < w</div>;
|
||||||
@ -0,0 +1 @@
|
|||||||
|
{ "BABEL_8_BREAKING": false }
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
import { jsx as _jsx } from "react/jsx-runtime";
|
||||||
|
import { jsxs as _jsxs } from "react/jsx-runtime";
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
children: "wow"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
children: "w\xF4w"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
children: "w & w"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
children: "w & w"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
children: "w \xA0 w"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
children: "this should not parse as unicode: \\u00a0"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
children: "this should parse as nbsp: \xA0 "
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsxs("div", {
|
||||||
|
children: ["this should parse as unicode: ", '\u00a0 ']
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
_jsx("div", {
|
||||||
|
children: "w < w"
|
||||||
|
});
|
||||||
@ -0,0 +1 @@
|
|||||||
|
{ "BABEL_8_BREAKING": true }
|
||||||
@ -8,7 +8,7 @@ _jsx("div", {
|
|||||||
|
|
||||||
/*#__PURE__*/
|
/*#__PURE__*/
|
||||||
_jsx("div", {
|
_jsx("div", {
|
||||||
children: "w\xF4w"
|
children: "wôw"
|
||||||
});
|
});
|
||||||
|
|
||||||
/*#__PURE__*/
|
/*#__PURE__*/
|
||||||
|
|||||||
@ -0,0 +1,3 @@
|
|||||||
|
<div id="wôw" />;
|
||||||
|
<div id="\w" />;
|
||||||
|
<div id="w < w" />;
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": false
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", {
|
||||||
|
id: "w\xF4w"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", {
|
||||||
|
id: "\\w"
|
||||||
|
});
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", {
|
||||||
|
id: "w < w"
|
||||||
|
});
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": true
|
||||||
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
/*#__PURE__*/
|
/*#__PURE__*/
|
||||||
React.createElement("div", {
|
React.createElement("div", {
|
||||||
id: "w\xF4w"
|
id: "wôw"
|
||||||
});
|
});
|
||||||
|
|
||||||
/*#__PURE__*/
|
/*#__PURE__*/
|
||||||
|
|||||||
@ -0,0 +1,12 @@
|
|||||||
|
<div>wow</div>;
|
||||||
|
<div>wôw</div>;
|
||||||
|
|
||||||
|
<div>w & w</div>;
|
||||||
|
<div>w & w</div>;
|
||||||
|
|
||||||
|
<div>w w</div>;
|
||||||
|
<div>this should not parse as unicode: \u00a0</div>;
|
||||||
|
<div>this should parse as nbsp: </div>;
|
||||||
|
<div>this should parse as unicode: {'\u00a0 '}</div>;
|
||||||
|
|
||||||
|
<div>w < w</div>;
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": false
|
||||||
|
}
|
||||||
@ -0,0 +1,26 @@
|
|||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", null, "wow");
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", null, "w\xF4w");
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", null, "w & w");
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", null, "w & w");
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", null, "w \xA0 w");
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", null, "this should not parse as unicode: \\u00a0");
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", null, "this should parse as nbsp: \xA0 ");
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", null, "this should parse as unicode: ", '\u00a0 ');
|
||||||
|
|
||||||
|
/*#__PURE__*/
|
||||||
|
React.createElement("div", null, "w < w");
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": true
|
||||||
|
}
|
||||||
@ -2,7 +2,7 @@
|
|||||||
React.createElement("div", null, "wow");
|
React.createElement("div", null, "wow");
|
||||||
|
|
||||||
/*#__PURE__*/
|
/*#__PURE__*/
|
||||||
React.createElement("div", null, "w\xF4w");
|
React.createElement("div", null, "wôw");
|
||||||
|
|
||||||
/*#__PURE__*/
|
/*#__PURE__*/
|
||||||
React.createElement("div", null, "w & w");
|
React.createElement("div", null, "w & w");
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
var foo = bar`\u0061\u{0061}\ud835\udc9c\u{1d49c}`;
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": false
|
||||||
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
var _templateObject;
|
||||||
|
|
||||||
|
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
|
||||||
|
|
||||||
|
var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["aa\uD835\uDC9C\uD835\uDC9C"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"])));
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"BABEL_8_BREAKING": true
|
||||||
|
}
|
||||||
@ -2,4 +2,4 @@ var _templateObject;
|
|||||||
|
|
||||||
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
|
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
|
||||||
|
|
||||||
var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["aa\uD835\uDC9C\uD835\uDC9C"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"])));
|
var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["aa𝒜𝒜"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"])));
|
||||||
|
|||||||
@ -42,6 +42,15 @@ export default declare(api => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
name: "transform-unicode-escapes",
|
name: "transform-unicode-escapes",
|
||||||
|
manipulateOptions({ generatorOpts }) {
|
||||||
|
// Babel 8 will enable jsesc minimal mode by default, which outputs
|
||||||
|
// unescaped unicode string
|
||||||
|
if (!generatorOpts.jsescOption) {
|
||||||
|
generatorOpts.jsescOption = {};
|
||||||
|
}
|
||||||
|
// $FlowIgnore: Flow does not support logical assignment
|
||||||
|
generatorOpts.jsescOption.minimal ??= false;
|
||||||
|
},
|
||||||
visitor: {
|
visitor: {
|
||||||
Identifier(path) {
|
Identifier(path) {
|
||||||
const { node, key } = path;
|
const { node, key } = path;
|
||||||
|
|||||||
23
yarn.lock
23
yarn.lock
@ -251,7 +251,7 @@ __metadata:
|
|||||||
"@babel/helper-fixtures": "workspace:*"
|
"@babel/helper-fixtures": "workspace:*"
|
||||||
"@babel/parser": "workspace:*"
|
"@babel/parser": "workspace:*"
|
||||||
"@babel/types": "workspace:^7.12.11"
|
"@babel/types": "workspace:^7.12.11"
|
||||||
jsesc: ^2.5.1
|
jsesc: "condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1"
|
||||||
source-map: ^0.5.0
|
source-map: ^0.5.0
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
linkType: soft
|
linkType: soft
|
||||||
@ -9210,7 +9210,7 @@ fsevents@^1.2.7:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"jsesc@npm:^2.5.1":
|
"jsesc-BABEL_8_BREAKING-false@npm:jsesc@^2.5.1, jsesc@npm:^2.5.1":
|
||||||
version: 2.5.2
|
version: 2.5.2
|
||||||
resolution: "jsesc@npm:2.5.2"
|
resolution: "jsesc@npm:2.5.2"
|
||||||
bin:
|
bin:
|
||||||
@ -9219,6 +9219,25 @@ fsevents@^1.2.7:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"jsesc-BABEL_8_BREAKING-true@npm:jsesc@^3.0.2":
|
||||||
|
version: 3.0.2
|
||||||
|
resolution: "jsesc@npm:3.0.2"
|
||||||
|
bin:
|
||||||
|
jsesc: bin/jsesc
|
||||||
|
checksum: 809c7663241f76c42528e2706f011d43c0eaa19b50a5d6f4015b4b9563292c0ffad1f5b12a9a41b71e2206efc5ae3632e8b27c35cb3ef508c0a58a3bb011da90
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
|
"jsesc@condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1":
|
||||||
|
version: 0.0.0-condition-31d9b4
|
||||||
|
resolution: "jsesc@condition:BABEL_8_BREAKING?^3.0.2:^2.5.1#31d9b4"
|
||||||
|
dependencies:
|
||||||
|
jsesc-BABEL_8_BREAKING-false: "npm:jsesc@^2.5.1"
|
||||||
|
jsesc-BABEL_8_BREAKING-true: "npm:jsesc@^3.0.2"
|
||||||
|
checksum: d6751e397e85958087fffcb6fbe5892902d2455e98ff0d69b69d0b15103892465d60aa0d750199a5d45cca75bbd89c4854b80ef4cbb98b229e3e9fa4e567d968
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"jsesc@npm:~0.5.0":
|
"jsesc@npm:~0.5.0":
|
||||||
version: 0.5.0
|
version: 0.5.0
|
||||||
resolution: "jsesc@npm:0.5.0"
|
resolution: "jsesc@npm:0.5.0"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user