Skip to content

Commit 539bc0c

Browse files
Andaristemmatown
authored andcommitted
Insert import to @emotion/core after existing imports to avoid ordering issues with polyfills (#1576)
1 parent b3a0f14 commit 539bc0c

File tree

7 files changed

+65
-13
lines changed

7 files changed

+65
-13
lines changed

.changeset/five-ligers-chew.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@emotion/babel-plugin-jsx-pragmatic': patch
3+
'@emotion/babel-preset-css-prop': patch
4+
---
5+
6+
Insert import to @emotion/core after existing imports to avoid ordering issues with polyfills
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// inserted import has to go AFTER polyfills
2+
import 'react-app-polyfill/ie11'
3+
import 'react-app-polyfill/stable'
4+
5+
import React from 'react'
6+
import ReactDOM from 'react-dom'
7+
import App from './App'
8+
9+
ReactDOM.render(<App />, document.getElementById('root'))

packages/babel-plugin-jsx-pragmatic/__tests__/__snapshots__/index.js.snap

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

3+
exports[`@emotion/babel-plugin-jsx-pragmatic existing-imports 1`] = `
4+
"// inserted import has to go AFTER polyfills
5+
import 'react-app-polyfill/ie11'
6+
import 'react-app-polyfill/stable'
7+
8+
import React from 'react'
9+
import ReactDOM from 'react-dom'
10+
import App from './App'
11+
12+
ReactDOM.render(<App />, document.getElementById('root'))
13+
14+
15+
↓ ↓ ↓ ↓ ↓ ↓
16+
17+
// inserted import has to go AFTER polyfills
18+
import 'react-app-polyfill/ie11';
19+
import 'react-app-polyfill/stable';
20+
import React from 'react';
21+
import ReactDOM from 'react-dom';
22+
import App from './App';
23+
import { jsx as ___EmotionJSX } from \\"@emotion/core\\";
24+
ReactDOM.render(<App />, document.getElementById('root'));"
25+
`;
26+
327
exports[`@emotion/babel-plugin-jsx-pragmatic fragment-only 1`] = `
428
"import * as React from 'react'
529
@@ -8,8 +32,8 @@ const F = () => <></>
832
933
↓ ↓ ↓ ↓ ↓ ↓
1034
11-
import { jsx as ___EmotionJSX } from \\"@emotion/core\\";
1235
import * as React from 'react';
36+
import { jsx as ___EmotionJSX } from \\"@emotion/core\\";
1337
1438
const F = () => <></>;"
1539
`;
@@ -22,8 +46,8 @@ const P = () => <p />
2246
2347
↓ ↓ ↓ ↓ ↓ ↓
2448
25-
import { jsx as ___EmotionJSX } from \\"@emotion/core\\";
2649
import * as React from 'react';
50+
import { jsx as ___EmotionJSX } from \\"@emotion/core\\";
2751
2852
const P = () => <p />;"
2953
`;

packages/babel-plugin-jsx-pragmatic/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
"devDependencies": {
2323
"@babel/core": "^7.5.5"
2424
}
25-
}
25+
}

packages/babel-plugin-jsx-pragmatic/src/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import syntaxJsx from '@babel/plugin-syntax-jsx'
22

3+
const findLast = (arr, predicate) => {
4+
for (let i = arr.length - 1; i >= 0; i--) {
5+
if (predicate(arr[i])) {
6+
return arr[i]
7+
}
8+
}
9+
}
10+
311
export default function jsxPragmatic(babel) {
412
const t = babel.types
5-
function getPragmaImport(state) {
6-
return t.importDeclaration(
13+
14+
function addPragmaImport(path, state) {
15+
const importDeclar = t.importDeclaration(
716
[
817
t.importSpecifier(
918
t.identifier(state.opts.import),
@@ -12,6 +21,15 @@ export default function jsxPragmatic(babel) {
1221
],
1322
t.stringLiteral(state.opts.module)
1423
)
24+
25+
const targetPath = findLast(path.get('body'), p => p.isImportDeclaration())
26+
27+
if (targetPath) {
28+
targetPath.insertAfter([importDeclar])
29+
} else {
30+
// Apparently it's now safe to do this even if Program begins with directives.
31+
path.unshiftContainer('body', importDeclar)
32+
}
1533
}
1634

1735
return {
@@ -27,10 +45,7 @@ export default function jsxPragmatic(babel) {
2745
Program: {
2846
exit: function(path, state) {
2947
if (!state.get('jsxDetected')) return
30-
31-
// Apparently it's now safe to do this even if Program begins with
32-
// directives.
33-
path.unshiftContainer('body', getPragmaImport(state))
48+
addPragmaImport(path, state)
3449
}
3550
},
3651

packages/babel-preset-css-prop/__tests__/__snapshots__/index.js.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ export let Button = props => {
1919
2020
↓ ↓ ↓ ↓ ↓ ↓
2121
22-
import { jsx as ___EmotionJSX } from \\"@emotion/core\\";
23-
2422
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2523
2624
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).\\"; }
2725
2826
import * as React from 'react';
27+
import { jsx as ___EmotionJSX } from \\"@emotion/core\\";
2928
3029
var _ref = process.env.NODE_ENV === \\"production\\" ? {
3130
name: \\"1q24rv0-Button\\",

packages/babel-preset-css-prop/__tests__/__snapshots__/options-are-used.js.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ export let Button = props => {
1919
2020
↓ ↓ ↓ ↓ ↓ ↓
2121
22-
import { jsx as ___EmotionJSX } from \\"@emotion/core\\";
23-
2422
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2523
2624
import * as React from 'react';
25+
import { jsx as ___EmotionJSX } from \\"@emotion/core\\";
2726
var _ref = {
2827
name: \\"1a7y9g-__fixtures__--__fixtures__--Button\\",
2928
styles: \\"color:hotpink;;label:__fixtures__--__fixtures__--Button;\\"

0 commit comments

Comments
 (0)