[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:
@@ -20,7 +20,7 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"@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"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -203,8 +203,6 @@ export function StringLiteral(node: Object) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ensure the output is ASCII-safe
|
||||
|
||||
const val = jsesc(
|
||||
node.value,
|
||||
process.env.BABEL_8_BREAKING
|
||||
|
||||
@@ -55,6 +55,7 @@ function normalizeOptions(code, opts): Format {
|
||||
jsescOption: {
|
||||
quotes: "double",
|
||||
wrap: true,
|
||||
minimal: true,
|
||||
...opts.jsescOption,
|
||||
},
|
||||
recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType,
|
||||
@@ -62,6 +63,7 @@ function normalizeOptions(code, opts): Format {
|
||||
|
||||
if (!process.env.BABEL_8_BREAKING) {
|
||||
format.jsonCompatibleStrings = opts.jsonCompatibleStrings;
|
||||
delete format.jsescOption.minimal;
|
||||
}
|
||||
|
||||
if (format.minified) {
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
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';
|
||||
"\n\r";
|
||||
"😂";
|
||||
"\uD83D_\uDE02"
|
||||
`😂`;
|
||||
/foobar/g;
|
||||
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__*/
|
||||
_jsx("div", {
|
||||
id: "w\xF4w"
|
||||
id: "wôw"
|
||||
});
|
||||
|
||||
/*#__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__*/
|
||||
_jsx("div", {
|
||||
children: "w\xF4w"
|
||||
children: "wôw"
|
||||
});
|
||||
|
||||
/*#__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__*/
|
||||
React.createElement("div", {
|
||||
id: "w\xF4w"
|
||||
id: "wôw"
|
||||
});
|
||||
|
||||
/*#__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");
|
||||
|
||||
/*#__PURE__*/
|
||||
React.createElement("div", null, "w\xF4w");
|
||||
React.createElement("div", null, "wôw");
|
||||
|
||||
/*#__PURE__*/
|
||||
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) } })); }
|
||||
|
||||
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 {
|
||||
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: {
|
||||
Identifier(path) {
|
||||
const { node, key } = path;
|
||||
|
||||
Reference in New Issue
Block a user