Skip to content

Commit b7d2137

Browse files
authored
Make cssPropOptimization's default always true (regardless of the @emotion/react import presence) (#2080)
1 parent 688586d commit b7d2137

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

.changeset/slow-tables-shave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/babel-plugin': major
3+
---
4+
5+
`cssPropOptimization` defaults now to `true` regardless of the `@emotion/react` import presence.

docs/css-prop.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ If you are using the compatible React version (`>=16.14.0`) then you can opt int
4646
{ "runtime": "automatic", "importSource": "@emotion/react" }
4747
]
4848
],
49-
"plugins": [["@emotion/babel-plugin", { "cssPropOptimization": true }]]
49+
"plugins": ["@emotion/babel-plugin"]
5050
}
5151
```
5252

@@ -67,7 +67,7 @@ In case you want to use the new JSX runtimes with [Next.js](https://nextjs.org/)
6767
}
6868
]
6969
],
70-
"plugins": [["@emotion/babel-plugin", { "cssPropOptimization": true }]]
70+
"plugins": ["@emotion/babel-plugin"]
7171
}
7272
```
7373

packages/babel-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ const H1 = /*#__PURE__*/ styled('h1', {
293293

294294
### `cssPropOptimization`
295295

296-
`boolean`, defaults to `true` if an import from `@emotion/react` is found in a file.
296+
`boolean`, defaults to `true`.
297297

298298
This option assumes that you are using something to make `@emotion/react`'s `jsx` function work for all jsx. If you are not doing so and you do not want such optimizations to occur, disable this option.
299299

packages/babel-plugin/src/index.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,22 @@ export default function(babel: *, options: *) {
148148
},
149149
Program(path: *, state: *) {
150150
let macros = {}
151-
let jsxCoreImports: Array<{
151+
let jsxReactImports: Array<{
152152
importSource: string,
153153
export: string,
154154
cssExport: string
155155
}> = [
156156
{ importSource: '@emotion/react', export: 'jsx', cssExport: 'css' }
157157
]
158-
state.jsxCoreImport = jsxCoreImports[0]
158+
state.jsxReactImport = jsxReactImports[0]
159159
Object.keys(state.opts.importMap || {}).forEach(importSource => {
160160
let value = state.opts.importMap[importSource]
161161
let transformers = {}
162162
Object.keys(value).forEach(localExportName => {
163163
let { canonicalImport, ...options } = value[localExportName]
164164
let [packageName, exportName] = canonicalImport
165165
if (packageName === '@emotion/react' && exportName === 'jsx') {
166-
jsxCoreImports.push({
166+
jsxReactImports.push({
167167
importSource,
168168
export: localExportName,
169169
cssExport: getCssExport('jsx', importSource, value)
@@ -191,7 +191,9 @@ export default function(babel: *, options: *) {
191191
) {
192192
// this is supposed to override defaultOptions value
193193
// and let correct value to be set if coming in options
194-
extraOptions = { styledBaseImport: undefined }
194+
extraOptions = {
195+
styledBaseImport: undefined
196+
}
195197
}
196198

197199
let [exportTransformer, defaultOptions] =
@@ -202,7 +204,11 @@ export default function(babel: *, options: *) {
202204

203205
transformers[localExportName] = [
204206
exportTransformer,
205-
{ ...defaultOptions, ...extraOptions, ...options }
207+
{
208+
...defaultOptions,
209+
...extraOptions,
210+
...options
211+
}
206212
]
207213
})
208214
macros[importSource] = createTransformerMacro(transformers, {
@@ -217,26 +223,28 @@ export default function(babel: *, options: *) {
217223
'@emotion/css': vanillaEmotionMacro,
218224
...macros
219225
}
220-
if (state.opts.cssPropOptimization === undefined) {
221-
for (const node of path.node.body) {
222-
if (t.isImportDeclaration(node)) {
223-
let jsxCoreImport = jsxCoreImports.find(
224-
thing =>
225-
node.source.value === thing.importSource &&
226-
node.specifiers.some(
227-
x =>
228-
t.isImportSpecifier(x) && x.imported.name === thing.export
229-
)
230-
)
231-
if (jsxCoreImport) {
232-
state.transformCssProp = true
233-
state.jsxCoreImport = jsxCoreImport
234-
break
235-
}
226+
227+
for (const node of path.node.body) {
228+
if (t.isImportDeclaration(node)) {
229+
let jsxReactImport = jsxReactImports.find(
230+
thing =>
231+
node.source.value === thing.importSource &&
232+
node.specifiers.some(
233+
x =>
234+
t.isImportSpecifier(x) && x.imported.name === thing.export
235+
)
236+
)
237+
if (jsxReactImport) {
238+
state.jsxReactImport = jsxReactImport
239+
break
236240
}
237241
}
242+
}
243+
244+
if (state.opts.cssPropOptimization === false) {
245+
state.transformCssProp = false
238246
} else {
239-
state.transformCssProp = state.opts.cssPropOptimization
247+
state.transformCssProp = true
240248
}
241249

242250
if (state.opts.sourceMap === false) {
@@ -262,7 +270,7 @@ export default function(babel: *, options: *) {
262270
state,
263271
babel,
264272
path,
265-
cssImport: state.jsxCoreImport
273+
cssImport: state.jsxReactImport
266274
})
267275
}
268276
}

scripts/babel-preset-emotion-dev/src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ module.exports = (api, options = {}) => {
3131
options.useEmotionPlugin && [
3232
require.resolve('@emotion/babel-plugin'),
3333
{
34-
...('sourceMap' in options && { sourceMap: options.sourceMap }),
35-
...(options.runtime === 'automatic' && { cssPropOptimization: true })
34+
...('sourceMap' in options && { sourceMap: options.sourceMap })
3635
}
3736
]
3837
].filter(Boolean)

0 commit comments

Comments
 (0)