Skip to content

Commit c7850e6

Browse files
Andaristemmatown
authored andcommitted
Optimize Babel output a little bit (#1656)
* Optimize Babel output a little bit * Fix label regression in dev nodes * Add `autoLabel` validation * Update more snapshots * Fix flow error * Rename auto to dev-only as autoLabel value * appendExpressionToArguments -> appendStringReturningExpressionToArguments * Add simple tests for new autoLabel values * Merge appendStringToArguments logic into appendStringReturningExpressionToArguments * Adjust docs about new autoLabel values * Add changeset
1 parent affed3d commit c7850e6

File tree

36 files changed

+858
-640
lines changed

36 files changed

+858
-640
lines changed

.changeset/polite-impalas-doubt.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'babel-plugin-emotion': major
3+
'@emotion/babel-preset-css-prop': major
4+
---
5+
6+
`autoLabel` option no longer is a simple boolean. Instead we accept now 3 values: `dev-only` (the default), `always` and `never`.
7+
8+
Each possible value for this option produces different output code:
9+
10+
- with `dev-only` we optimize the production code, so there are no labels added there, but at the same time we keep labels for development environments,
11+
- with `always` we always add labels when possible,
12+
- with `never` we disable this entirely and no labels are added.

packages/babel-plugin-emotion/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ _Defaults Shown_
132132
{
133133
// sourceMap is on by default but source maps are dead code eliminated in production
134134
"sourceMap": true,
135-
"autoLabel": process.env.NODE_ENV !== 'production',
135+
"autoLabel": "dev-only",
136136
"labelFormat": "[local]",
137137
"cssPropOptimization": true
138138
}
@@ -183,7 +183,7 @@ This option enables the following:
183183
184184
### `autoLabel`
185185

186-
`boolean`, defaults to `process.env.NODE_ENV !== 'production'`.
186+
`'dev-only' | 'always' | 'never'`, , defaults to `dev-only`.
187187

188188
This option enables the following:
189189

@@ -194,6 +194,12 @@ This option enables the following:
194194
(Eg. `iconStyles$1` will become `iconStyles1`) because `$` is not valid
195195
[CSS ClassName Selector](https://stackoverflow.com/questions/448981/which-characters-are-valid-in-css-class-names-selectors#449000)
196196

197+
Each possible value for this option produces different output code:
198+
199+
- with `dev-only` we optimize the production code, so there are no labels added there, but at the same time we keep labels for development environments,
200+
- with `always` we always add labels when possible,
201+
- with `never` we disable this entirely and no labels are added.
202+
197203
#### css
198204

199205
**In**
@@ -214,7 +220,7 @@ const brownStyles = /*#__PURE__*/ css({ color: 'brown' }, 'label:brownStyles;')
214220

215221
`string`, defaults to `"[local]"`.
216222

217-
This option only works when `autoLabel` is set to `true`. It allows you to
223+
This option only works when `autoLabel` is set to `'dev-only'` or `'always'`. It allows you to
218224
define the format of the resulting `label`. The format is defined via string where
219225
variable parts are enclosed in square brackets `[]`.
220226
For example `labelFormat: "my-classname--[local]"`, where `[local]` will be replaced
@@ -235,7 +241,7 @@ be prepended automatically.
235241

236242
```javascript
237243
// BrownView.js
238-
// autoLabel: true
244+
// autoLabel: 'dev-only'
239245
// labelFormat: '[filename]--[local]'
240246
const brownStyles = css({ color: 'brown' })
241247
```

packages/babel-plugin-emotion/__tests__/__snapshots__/css-requires-options.js.snap

Lines changed: 117 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,101 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`babel css inline autoLabel set to always - complex expression 1`] = `
4+
"
5+
import { css } from 'emotion'
6+
import fooStyles from './foo'
7+
let cls = css(fooStyles)
8+
9+
10+
↓ ↓ ↓ ↓ ↓ ↓
11+
12+
import { css } from 'emotion';
13+
import fooStyles from './foo';
14+
let cls =
15+
/*#__PURE__*/
16+
css(fooStyles, \\";label:cls\\" + (process.env.NODE_ENV === \\"production\\" ? \\"\\" : \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUdjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBpbXBvcnQgZm9vU3R5bGVzIGZyb20gJy4vZm9vJ1xuICAgIGxldCBjbHMgPSBjc3MoZm9vU3R5bGVzKVxuICAgICJdfQ== */\\"));"
17+
`;
18+
19+
exports[`babel css inline autoLabel set to always - complex expression, last arg string 1`] = `
20+
"
21+
import { css } from 'emotion'
22+
import fooStyles from './foo'
23+
let cls = css(fooStyles, 'color: hotpink;')
24+
25+
26+
↓ ↓ ↓ ↓ ↓ ↓
27+
28+
import { css } from 'emotion';
29+
import fooStyles from './foo';
30+
let cls =
31+
/*#__PURE__*/
32+
css(fooStyles, \\"color: hotpink;;label:cls\\" + (process.env.NODE_ENV === \\"production\\" ? \\"\\" : \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUdjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBpbXBvcnQgZm9vU3R5bGVzIGZyb20gJy4vZm9vJ1xuICAgIGxldCBjbHMgPSBjc3MoZm9vU3R5bGVzLCAnY29sb3I6IGhvdHBpbms7JylcbiAgICAiXX0= */\\"));"
33+
`;
34+
35+
exports[`babel css inline autoLabel set to always 1`] = `
36+
"
37+
import { css } from 'emotion'
38+
let cls = css({color:'hotpink'})
39+
40+
41+
↓ ↓ ↓ ↓ ↓ ↓
42+
43+
function _EMOTION_STRINGIFIED_CSS_ERROR__() { return \\"You have tried to stringify object returned from \`css\` function. It isn't supposed to be used directly (e.g. as value of the \`className\` prop), but rather handed to emotion so it can handle it (e.g. as value of \`css\` prop).\\"; }
44+
45+
import { css } from 'emotion';
46+
let cls =
47+
/*#__PURE__*/
48+
css(process.env.NODE_ENV === \\"production\\" ? {
49+
name: \\"6kh100-cls\\",
50+
styles: \\"color:hotpink;label:cls;\\"
51+
} : {
52+
name: \\"6kh100-cls\\",
53+
styles: \\"color:hotpink;label:cls;\\",
54+
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBsZXQgY2xzID0gY3NzKHtjb2xvcjonaG90cGluayd9KVxuICAgICJdfQ== */\\",
55+
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
56+
});"
57+
`;
58+
59+
exports[`babel css inline autoLabel set to never - complex expression 1`] = `
60+
"
61+
import { css } from 'emotion'
62+
import fooStyles from './foo'
63+
let cls = css(fooStyles)
64+
65+
66+
↓ ↓ ↓ ↓ ↓ ↓
67+
68+
import { css } from 'emotion';
69+
import fooStyles from './foo';
70+
let cls =
71+
/*#__PURE__*/
72+
css(fooStyles, process.env.NODE_ENV === \\"production\\" ? \\"\\" : \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUdjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBpbXBvcnQgZm9vU3R5bGVzIGZyb20gJy4vZm9vJ1xuICAgIGxldCBjbHMgPSBjc3MoZm9vU3R5bGVzKVxuICAgICJdfQ== */\\");"
73+
`;
74+
75+
exports[`babel css inline autoLabel set to never 1`] = `
76+
"
77+
import { css } from 'emotion'
78+
let cls = css({color:'hotpink'})
79+
80+
81+
↓ ↓ ↓ ↓ ↓ ↓
82+
83+
function _EMOTION_STRINGIFIED_CSS_ERROR__() { return \\"You have tried to stringify object returned from \`css\` function. It isn't supposed to be used directly (e.g. as value of the \`className\` prop), but rather handed to emotion so it can handle it (e.g. as value of \`css\` prop).\\"; }
84+
85+
import { css } from 'emotion';
86+
let cls =
87+
/*#__PURE__*/
88+
css(process.env.NODE_ENV === \\"production\\" ? {
89+
name: \\"3sn2xs\\",
90+
styles: \\"color:hotpink\\"
91+
} : {
92+
name: \\"3sn2xs\\",
93+
styles: \\"color:hotpink\\",
94+
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBsZXQgY2xzID0gY3NzKHtjb2xvcjonaG90cGluayd9KVxuICAgICJdfQ== */\\",
95+
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
96+
});"
97+
`;
98+
399
exports[`babel css inline code already transpiled by emotion plugin (avoid double transpilation) 1`] = `
4100
"
5101
import { keyframes as _keyframes } from \\"emotion\\";
@@ -80,11 +176,11 @@ import { css } from 'emotion';
80176
let cls =
81177
/*#__PURE__*/
82178
css(process.env.NODE_ENV === \\"production\\" ? {
83-
name: \\"1rm02zb-CLS_CSS-REQUIRES-OPTIONS\\",
84-
styles: \\"color:hotpink;;label:CLS_CSS-REQUIRES-OPTIONS;\\"
179+
name: \\"3sn2xs\\",
180+
styles: \\"color:hotpink\\"
85181
} : {
86-
name: \\"1rm02zb-CLS_CSS-REQUIRES-OPTIONS\\",
87-
styles: \\"color:hotpink;;label:CLS_CSS-REQUIRES-OPTIONS;\\",
182+
name: \\"wiafqk-CLS_CSS-REQUIRES-OPTIONS\\",
183+
styles: \\"color:hotpink;label:CLS_CSS-REQUIRES-OPTIONS;\\",
88184
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBsZXQgY2xzID0gY3NzKHtjb2xvcjonaG90cGluayd9KVxuICAgICJdfQ== */\\",
89185
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
90186
});"
@@ -104,11 +200,11 @@ import { css } from 'emotion';
104200
let cls =
105201
/*#__PURE__*/
106202
css(process.env.NODE_ENV === \\"production\\" ? {
107-
name: \\"mn0r3a-my-css-__tests__-css-requires-options-cls\\",
108-
styles: \\"color:hotpink;;label:my-css-__tests__-css-requires-options-cls;\\"
203+
name: \\"3sn2xs\\",
204+
styles: \\"color:hotpink\\"
109205
} : {
110-
name: \\"mn0r3a-my-css-__tests__-css-requires-options-cls\\",
111-
styles: \\"color:hotpink;;label:my-css-__tests__-css-requires-options-cls;\\",
206+
name: \\"1azqn5t-my-css-__tests__-css-requires-options-cls\\",
207+
styles: \\"color:hotpink;label:my-css-__tests__-css-requires-options-cls;\\",
112208
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBsZXQgY2xzID0gY3NzKHtjb2xvcjonaG90cGluayd9KVxuICAgICJdfQ== */\\",
113209
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
114210
});"
@@ -128,11 +224,11 @@ import { css } from 'emotion';
128224
let cls =
129225
/*#__PURE__*/
130226
css(process.env.NODE_ENV === \\"production\\" ? {
131-
name: \\"jnyc7-my-css-css-requires-options-cls\\",
132-
styles: \\"color:hotpink;;label:my-css-css-requires-options-cls;\\"
227+
name: \\"3sn2xs\\",
228+
styles: \\"color:hotpink\\"
133229
} : {
134-
name: \\"jnyc7-my-css-css-requires-options-cls\\",
135-
styles: \\"color:hotpink;;label:my-css-css-requires-options-cls;\\",
230+
name: \\"12j84ju-my-css-css-requires-options-cls\\",
231+
styles: \\"color:hotpink;label:my-css-css-requires-options-cls;\\",
136232
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBsZXQgY2xzID0gY3NzKHtjb2xvcjonaG90cGluayd9KVxuICAgICJdfQ== */\\",
137233
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
138234
});"
@@ -152,11 +248,11 @@ import { css } from 'emotion';
152248
let cls =
153249
/*#__PURE__*/
154250
css(process.env.NODE_ENV === \\"production\\" ? {
155-
name: \\"v8qj0c-my-css-some-directory-cls\\",
156-
styles: \\"color:hotpink;;label:my-css-some-directory-cls;\\"
251+
name: \\"3sn2xs\\",
252+
styles: \\"color:hotpink\\"
157253
} : {
158-
name: \\"v8qj0c-my-css-some-directory-cls\\",
159-
styles: \\"color:hotpink;;label:my-css-some-directory-cls;\\",
254+
name: \\"1uf5d32-my-css-some-directory-cls\\",
255+
styles: \\"color:hotpink;label:my-css-some-directory-cls;\\",
160256
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVjIiwiZmlsZSI6ImluZGV4LmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBsZXQgY2xzID0gY3NzKHtjb2xvcjonaG90cGluayd9KVxuICAgICJdfQ== */\\",
161257
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
162258
});"
@@ -176,11 +272,11 @@ import { css } from 'emotion';
176272
let cls =
177273
/*#__PURE__*/
178274
css(process.env.NODE_ENV === \\"production\\" ? {
179-
name: \\"ryhvpw-my-css-cls\\",
180-
styles: \\"color:hotpink;;label:my-css-cls;\\"
275+
name: \\"3sn2xs\\",
276+
styles: \\"color:hotpink\\"
181277
} : {
182-
name: \\"ryhvpw-my-css-cls\\",
183-
styles: \\"color:hotpink;;label:my-css-cls;\\",
278+
name: \\"1xiywpm-my-css-cls\\",
279+
styles: \\"color:hotpink;label:my-css-cls;\\",
184280
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBsZXQgY2xzID0gY3NzKHtjb2xvcjonaG90cGluayd9KVxuICAgICJdfQ== */\\",
185281
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
186282
});"
@@ -210,5 +306,5 @@ import { css } from '@emotion/core';
210306
var templateObject_1;
211307
const someVar =
212308
/*#__PURE__*/
213-
css(templateObject_1 || (templateObject_1 = __makeTemplateObject([\\"\\\\n color: hotpink;\\\\n;label:someVar;\\"], [\\"\\\\n color: hotpink;\\\\n;label:someVar;\\"])));"
309+
css(templateObject_1 || (templateObject_1 = __makeTemplateObject(['\\\\n color: hotpink;\\\\n' + (process.env.NODE_ENV === \\"production\\" ? \\"\\" : \\";label:someVar\\")], ['\\\\n color: hotpink;\\\\n' + (process.env.NODE_ENV === \\"production\\" ? \\"\\" : \\";label:someVar\\")])));"
214310
`;

0 commit comments

Comments
 (0)