Skip to content

Commit eb65f35

Browse files
jaydensericTimer
andauthored
fix: make styled-jsx/babel plugin respect the source type (#684)
* Make the styled-jsx/babel Babel plugin respect the Babel source type. Fixes #680 . * chore: retrigger Co-authored-by: Joe Haddad <[email protected]> Co-authored-by: Joe Haddad <[email protected]>
1 parent 65b4f64 commit eb65f35

19 files changed

+65
-71
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
}
5353
},
5454
"dependencies": {
55+
"@babel/helper-module-imports": "7.12.5",
5556
"@babel/types": "7.8.3",
5657
"babel-plugin-syntax-jsx": "6.18.0",
5758
"convert-source-map": "1.7.0",

src/_utils.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path'
2+
import { addDefault } from '@babel/helper-module-imports';
23
import * as t from '@babel/types'
34
import _hashString from 'string-hash'
45
import { SourceMapGenerator } from 'source-map'
@@ -621,14 +622,12 @@ export const booleanOption = opts => {
621622
}
622623

623624
export const createReactComponentImportDeclaration = state => {
624-
const styleModule =
625+
addDefault(
626+
state.file.path,
625627
typeof state.opts.styleModule === 'string'
626628
? state.opts.styleModule
627-
: 'styled-jsx/style'
628-
629-
return t.importDeclaration(
630-
[t.importDefaultSpecifier(t.identifier(STYLE_COMPONENT))],
631-
t.stringLiteral(styleModule)
629+
: 'styled-jsx/style',
630+
{ nameHint: STYLE_COMPONENT}
632631
)
633632
}
634633

src/babel-external.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,7 @@ export const visitor = {
226226
!path.scope.hasBinding(STYLE_COMPONENT)
227227
) {
228228
state.hasInjectedJSXStyle = true
229-
const importDeclaration = createReactComponentImportDeclaration(state)
230-
path.scope.path.node.body.unshift(importDeclaration)
229+
createReactComponentImportDeclaration(state)
231230
}
232231
})
233232

src/babel.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export default function({ types: t }) {
294294
// Transpile external styles
295295
path.traverse(externalStylesVisitor, state)
296296
},
297-
exit({ node, scope }, state) {
297+
exit({ scope }, state) {
298298
if (
299299
!(
300300
state.file.hasJSXStyle &&
@@ -306,8 +306,7 @@ export default function({ types: t }) {
306306
}
307307

308308
state.hasInjectedJSXStyle = true
309-
const importDeclaration = createReactComponentImportDeclaration(state)
310-
node.body.unshift(importDeclaration)
309+
createReactComponentImportDeclaration(state)
311310
}
312311
}
313312
}

src/macro.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ function styledJsxMacro({ references, state }) {
102102
!path.scope.hasBinding(STYLE_COMPONENT)
103103
) {
104104
state.hasInjectedJSXStyle = true
105-
const importDeclaration = createReactComponentImportDeclaration(state)
106-
path.findParent(p => p.isProgram()).node.body.unshift(importDeclaration)
105+
createReactComponentImportDeclaration(state)
107106
}
108107
})
109108
})

test/babel6/snapshots/attribute.js.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ Generated by [AVA](https://ava.li).
106106

107107
> Snapshot 1
108108
109-
`var _this = this;␊
109+
`import _JSXStyle from "styled-jsx/style";␊
110+
111+
var _this = this;␊
110112
111-
import _JSXStyle from "styled-jsx/style";␊
112113
export default (() => {␊
113114
const Element = 'div';␊
114115
return <div className={"jsx-2886504620"}>␊
0 Bytes
Binary file not shown.

test/babel6/snapshots/external.js.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Generated by [AVA](https://ava.li).
1919
const baz = ['div{font-size:3em;}'];␊
2020
2121
baz.__hash = '2141779268';␊
22-
a.__hash = '262929833';␊
2322
const a = [`div{font-size:${size}em;}`];␊
2423
24+
a.__hash = '262929833';␊
2525
export const uh = bar;␊
2626
2727
export const foo = [`div.jsx-2299908427{color:${color};}`];␊
@@ -123,9 +123,9 @@ Generated by [AVA](https://ava.li).
123123
const baz = new String('div{font-size:3em;}');␊
124124
125125
baz.__hash = '2141779268';␊
126-
a.__hash = '262929833';␊
127126
const a = new String(`div{font-size:${size}em;}`);␊
128127
128+
a.__hash = '262929833';␊
129129
export const uh = bar;␊
130130
131131
export const foo = new String(`div.jsx-2299908427{color:${color};}`);␊
1 Byte
Binary file not shown.
7 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)