Skip to content

Commit

Permalink
fix: _JSXStyle import is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jul 31, 2021
1 parent 2741879 commit 56a1bbb
Show file tree
Hide file tree
Showing 21 changed files with 159 additions and 64 deletions.
38 changes: 35 additions & 3 deletions src/_utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { addDefault } from '@babel/helper-module-imports';
import { addDefault } from '@babel/helper-module-imports'
import * as t from '@babel/types'
import _hashString from 'string-hash'
import { SourceMapGenerator } from 'source-map'
Expand Down Expand Up @@ -622,13 +622,45 @@ export const booleanOption = opts => {
}

export const createReactComponentImportDeclaration = state => {
addDefault(
const uid = state.file.path.scope.generateUidIdentifierBasedOnNode(
state.file.path.node.id
)
const importIdentifierName = '_JSXImport' + uid.name

const importIdentifier = addDefault(
state.file.path,
typeof state.opts.styleModule === 'string'
? state.opts.styleModule
: 'styled-jsx/style',
{ nameHint: STYLE_COMPONENT}
{ nameHint: importIdentifierName }
)
// const _JSXStyle = _JSXImport<uid>;
const variableDeclaration = t.variableDeclaration('const', [
t.variableDeclarator(t.identifier(STYLE_COMPONENT), importIdentifier)
])

// Insert the variable declaration after the import declaration
state.file.path.traverse({
// Handle assignment declaration
VariableDeclarator(path) {
if (
t.isIdentifier(path.node.id) &&
path.node.id.name === importIdentifier.name
) {
path.parentPath.insertAfter(variableDeclaration)
}
},
// Handle ESM imports
ImportDeclaration(path) {
const firstSpecifier = path.node.specifiers[0]
if (
firstSpecifier &&
firstSpecifier.local.name === importIdentifier.name
) {
path.insertAfter(variableDeclaration)
}
}
})
}

export const setStateOptions = state => {
Expand Down
6 changes: 4 additions & 2 deletions test/babel6/snapshots/attribute.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
import styles from './styles';␊
const styles2 = require('./styles2');␊
Expand Down Expand Up @@ -106,10 +107,11 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
`import _JSXImport_ref from "styled-jsx/style";␊
var _this = this;␊
const _JSXStyle = _JSXImport_ref;␊
export default (() => {␊
const Element = 'div';␊
return <div className={"jsx-2886504620"}>␊
Expand Down
Binary file modified test/babel6/snapshots/attribute.js.snap
Binary file not shown.
21 changes: 14 additions & 7 deletions test/babel6/snapshots/external.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
import colors, { size } from './constants';␊
const color = 'red';␊
Expand Down Expand Up @@ -98,7 +99,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
function test() {␊
Expand All @@ -112,7 +114,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
import colors, { size } from './constants';␊
const color = 'red';␊
Expand Down Expand Up @@ -168,7 +171,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
`import _JSXImport_ref from "styled-jsx/style";␊
const _JSXStyle = _JSXImport_ref;␊
import styles from './styles2';␊
export default (({ level = 1 }) => {␊
Expand All @@ -184,7 +188,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
import styles from './styles';␊
const styles2 = require('./styles2');␊
import { foo as styles3 } from './styles';␊
Expand All @@ -211,7 +216,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
import styles, { foo as styles3 } from './styles';␊
const styles2 = require('./styles2');␊
Expand All @@ -229,7 +235,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
import styles from './styles';␊
export default (() => <div className={`jsx-${styles.__hash}`}>␊
Expand Down
Binary file modified test/babel6/snapshots/external.js.snap
Binary file not shown.
42 changes: 28 additions & 14 deletions test/babel6/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
export default (() => <div className={"jsx-2743241663"}>␊
<p className={"jsx-2743241663"}>test</p>␊
<p className={"jsx-2743241663"}>woot</p>␊
Expand All @@ -28,7 +29,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
export default (() => <div className={"jsx-2743241663"}>␊
<p className={"jsx-2743241663"}>test</p>␊
<p className={"jsx-2743241663"}>woot</p>␊
Expand All @@ -40,7 +42,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
const Test = () => <_JSXStyle id={"2743241663"}>{"p{color:red;}"}</_JSXStyle>;␊
export default (() => <div className={"jsx-2673076688"}>␊
Expand All @@ -53,7 +56,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
const color = 'red';␊
const otherColor = 'green';␊
Expand All @@ -76,7 +80,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
`import _JSXImport_ref from "styled-jsx/style";␊
const _JSXStyle = _JSXImport_ref;␊
const Test = () => <div className={"jsx-2529315885"}>␊
<span className={"jsx-2529315885"}>test</span>␊
<Component />␊
Expand All @@ -87,7 +92,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
`import _JSXImport_ref from "styled-jsx/style";␊
const _JSXStyle = _JSXImport_ref;␊
export default class {␊
render() {␊
Expand All @@ -103,7 +109,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
export default (({ children }) => <div className={`jsx-${styles.__hash}`}>␊
Expand All @@ -127,7 +134,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
`import _JSXImport_ref from "styled-jsx/style";␊
const _JSXStyle = _JSXImport_ref;␊
export default (({ level = 1 }) => {␊
const Element = `h${level}`;␊
Expand Down Expand Up @@ -158,7 +166,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
export default class {␊
render() {␊
const Element = 'div';␊
Expand All @@ -184,7 +193,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
const darken = c => c;␊
const color = 'red';␊
const otherColor = 'green';␊
Expand Down Expand Up @@ -215,7 +225,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
const Test = () => <div className={"jsx-2209073070"}>␊
<_JSXStyle id={"2209073070"}>{"body{color:red;}:hover{color:red;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-animation:foo 1s ease-out;animation:foo 1s ease-out;}div a{display:none;}[data-test]>div{color:red;}"}</_JSXStyle>␊
</div>;␊
Expand All @@ -226,7 +237,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
`import _JSXImport_ref from "styled-jsx/style";␊
const _JSXStyle = _JSXImport_ref;␊
const attrs = {␊
id: 'test'␊
};␊
Expand Down Expand Up @@ -257,7 +269,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
export default (() => <div className={"jsx-2743241663"}>␊
<p className={"jsx-2743241663"}>woot</p>␊
<style dangerouslySetInnerHTML={{ __html: `body { margin: 0; }` }}></style>␊
Expand All @@ -268,7 +281,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
export default (() => <div className={"jsx-2743241663"}>␊
<p className={"jsx-2743241663"}>test</p>␊
<p className={"jsx-2743241663"}>woot</p>␊
Expand Down
Binary file modified test/babel6/snapshots/index.js.snap
Binary file not shown.
9 changes: 6 additions & 3 deletions test/babel6/snapshots/macro.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
({␊
Expand All @@ -20,7 +21,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
function test() {␊
Expand All @@ -34,7 +36,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
const { className, styles } = {␊
Expand Down
Binary file modified test/babel6/snapshots/macro.js.snap
Binary file not shown.
6 changes: 4 additions & 2 deletions test/babel6/snapshots/plugins.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
import styles from './styles';␊
const color = 'red';␊
Expand Down Expand Up @@ -39,7 +40,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXImport_ref from 'styled-jsx/style';␊
const _JSXStyle = _JSXImport_ref;␊
import styles from './styles';␊
const color = 'red';␊
Expand Down
Binary file modified test/babel6/snapshots/plugins.js.snap
Binary file not shown.
6 changes: 4 additions & 2 deletions test/snapshots/attribute.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
`import _JSXImport_ref from "styled-jsx/style";␊
const _JSXStyle = _JSXImport_ref;␊
import styles from './styles';␊
const styles2 = require('./styles2'); // external only␊
Expand Down Expand Up @@ -112,7 +113,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
`import _JSXImport_ref from "styled-jsx/style";␊
const _JSXStyle = _JSXImport_ref;␊
export default (() => {␊
const Element = 'div';␊
return <div className={"jsx-2886504620"}>␊
Expand Down
Binary file modified test/snapshots/attribute.js.snap
Binary file not shown.
Loading

0 comments on commit 56a1bbb

Please sign in to comment.