Skip to content

Commit

Permalink
fix: _JSXStyle import is not found
Browse files Browse the repository at this point in the history
create imports on program enter
  • Loading branch information
huozhi committed Aug 1, 2021
1 parent ffd11ca commit 0d57f9e
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 36 deletions.
20 changes: 13 additions & 7 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 @@ -258,7 +258,7 @@ export const getJSXStyleInfo = (expr, scope) => {
}
}

export const computeClassNames = (styles, externalJsxId) => {
export const computeClassNames = (styles, externalJsxId, StyleComponent) => {
if (styles.length === 0) {
return {
className: externalJsxId
Expand Down Expand Up @@ -297,7 +297,7 @@ export const computeClassNames = (styles, externalJsxId) => {
// _JSXStyle.dynamic([ ['1234', [props.foo, bar, fn(props)]], ... ])
const dynamic = t.callExpression(
// Callee: _JSXStyle.dynamic
t.memberExpression(t.identifier(STYLE_COMPONENT), t.identifier('dynamic')),
t.memberExpression(t.identifier(StyleComponent), t.identifier('dynamic')),
// Arguments
[
t.arrayExpression(
Expand Down Expand Up @@ -380,7 +380,12 @@ export const cssToBabelType = css => {
return t.cloneDeep(css)
}

export const makeStyledJsxTag = (id, transformedCss, expressions = []) => {
export const makeStyledJsxTag = (
id,
transformedCss,
expressions = [],
StyleComponent
) => {
const css = cssToBabelType(transformedCss)

const attributes = [
Expand All @@ -402,8 +407,8 @@ export const makeStyledJsxTag = (id, transformedCss, expressions = []) => {
}

return t.jSXElement(
t.jSXOpeningElement(t.jSXIdentifier(STYLE_COMPONENT), attributes),
t.jSXClosingElement(t.jSXIdentifier(STYLE_COMPONENT)),
t.jSXOpeningElement(t.jSXIdentifier(StyleComponent), attributes),
t.jSXClosingElement(t.jSXIdentifier(StyleComponent)),
[t.jSXExpressionContainer(css)]
)
}
Expand Down Expand Up @@ -627,7 +632,7 @@ export const createReactComponentImportDeclaration = state => {
typeof state.opts.styleModule === 'string'
? state.opts.styleModule
: 'styled-jsx/style',
{ nameHint: STYLE_COMPONENT}
{ nameHint: state.StyleComponent }
)
}

Expand All @@ -650,6 +655,7 @@ export const setStateOptions = state => {
vendorPrefixes: state.opts.vendorPrefixes
})
}
state.StyleComponent = STYLE_COMPONENT
}

export function log(message) {
Expand Down
18 changes: 11 additions & 7 deletions src/babel-external.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as t from '@babel/types'

import { STYLE_COMPONENT } from './_constants'

import {
getJSXStyleInfo,
processCss,
Expand All @@ -23,7 +21,8 @@ export function processTaggedTemplateExpression({
splitRules,
plugins,
vendorPrefixes,
sourceMaps
sourceMaps,
StyleComponent
}) {
const templateLiteral = path.get('quasi')
let scope
Expand All @@ -39,7 +38,11 @@ export function processTaggedTemplateExpression({

const stylesInfo = getJSXStyleInfo(templateLiteral, scope)

const { staticClassName, className } = computeClassNames([stylesInfo])
const { staticClassName, className } = computeClassNames(
[stylesInfo],
undefined,
StyleComponent
)

const styles = processCss(
{
Expand All @@ -64,7 +67,7 @@ export function processTaggedTemplateExpression({
t.objectExpression([
t.objectProperty(
t.identifier('styles'),
makeStyledJsxTag(hash, css, expressions)
makeStyledJsxTag(hash, css, expressions, StyleComponent)
),
t.objectProperty(t.identifier('className'), className)
])
Expand Down Expand Up @@ -212,7 +215,8 @@ export const visitor = {
: process.env.NODE_ENV === 'production',
plugins: state.plugins,
vendorPrefixes,
sourceMaps
sourceMaps,
StyleComponent: state.StyleComponent
})
})
)
Expand All @@ -223,7 +227,7 @@ export const visitor = {
hasJSXStyle &&
taggedTemplateExpressions.resolve.length > 0 &&
!state.hasInjectedJSXStyle &&
!path.scope.hasBinding(STYLE_COMPONENT)
!path.scope.hasBinding(state.StyleComponent)
) {
state.hasInjectedJSXStyle = true
createReactComponentImportDeclaration(state)
Expand Down
15 changes: 9 additions & 6 deletions src/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ export default function({ types: t }) {
if (state.styles.length > 0 || externalJsxId) {
const { staticClassName, className } = computeClassNames(
state.styles,
externalJsxId
externalJsxId,
state.StyleComponent
)
state.className = className
state.staticClassName = staticClassName
Expand Down Expand Up @@ -224,7 +225,7 @@ export default function({ types: t }) {
) {
const [id, css] = state.externalStyles.shift()

path.replaceWith(makeStyledJsxTag(id, css))
path.replaceWith(makeStyledJsxTag(id, css, [], state.StyleComponent))
return
}

Expand All @@ -247,7 +248,9 @@ export default function({ types: t }) {
splitRules
})

path.replaceWith(makeStyledJsxTag(hash, css, expressions))
path.replaceWith(
makeStyledJsxTag(hash, css, expressions, state.StyleComponent)
)
}
}
}
Expand Down Expand Up @@ -286,6 +289,8 @@ export default function({ types: t }) {

setStateOptions(state)

state.importDeclaration = createReactComponentImportDeclaration(state)

// we need to beat the arrow function transform and
// possibly others so we traverse from here or else
// dynamic values in classNames could be incorrect
Expand All @@ -299,14 +304,12 @@ export default function({ types: t }) {
!(
state.file.hasJSXStyle &&
!state.hasInjectedJSXStyle &&
!scope.hasBinding(STYLE_COMPONENT)
!scope.hasBinding(state.StyleComponent)
)
) {
return
}

state.hasInjectedJSXStyle = true
createReactComponentImportDeclaration(state)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ function styledJsxMacro({ references, state }) {
: process.env.NODE_ENV === 'production',
plugins: state.plugins,
vendorPrefixes: state.opts.vendorPrefixes,
sourceMaps: state.opts.sourceMaps
sourceMaps: state.opts.sourceMaps,
StyleComponent: state.StyleComponent
})

if (
!state.hasInjectedJSXStyle &&
!path.scope.hasBinding(STYLE_COMPONENT)
!path.scope.hasBinding(state.StyleComponent)
) {
state.hasInjectedJSXStyle = true
createReactComponentImportDeclaration(state)
Expand Down
10 changes: 7 additions & 3 deletions test/babel6/snapshots/external.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`const _defaultExport = ['div.jsx-2292456818{font-size:3em;}'];␊
`import _JSXStyle from 'styled-jsx/style';␊
const _defaultExport = ['div.jsx-2292456818{font-size:3em;}'];␊
_defaultExport.__hash = '2292456818';␊
Expand All @@ -63,7 +64,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import css from 'hell';␊
`import _JSXStyle from 'styled-jsx/style';␊
import css from 'hell';␊
const color = 'red';␊
Expand Down Expand Up @@ -157,7 +159,9 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`const _defaultExport = new String('div.jsx-2292456818{font-size:3em;}');␊
`import _JSXStyle from 'styled-jsx/style';␊
const _defaultExport = new String('div.jsx-2292456818{font-size:3em;}');␊
_defaultExport.__hash = '2292456818';␊
Expand Down
Binary file modified test/babel6/snapshots/external.js.snap
Binary file not shown.
8 changes: 4 additions & 4 deletions test/babel6/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`const a = () => <div>␊
`import _JSXStyle from 'styled-jsx/style';␊
const a = () => <div>␊
<p>hi</p>␊
<style>{'woot'}</style>␊
</div>;`
Expand Down Expand Up @@ -87,16 +88,15 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
`import _JSXStyle from 'styled-jsx/style';␊
export const _StyleImport = '123';␊
export default class {␊
render() {␊
return <div className={"jsx-2101845350"}>␊
<p className={"jsx-2101845350"}>test</p>␊
<_JSXStyle id={"2101845350"}>{"p.jsx-2101845350{color:red;}"}</_JSXStyle>␊
</div>;␊
}␊
}`

## works with css tagged template literals in the same file
Expand Down
Binary file modified test/babel6/snapshots/index.js.snap
Binary file not shown.
5 changes: 2 additions & 3 deletions test/fixtures/class.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const _StyleImport = '123'
export default class {

render () {
render() {
return (
<div>
<p>test</p>
Expand All @@ -12,5 +12,4 @@ export default class {
</div>
)
}

}
10 changes: 7 additions & 3 deletions test/snapshots/external.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`const _defaultExport = ["div.jsx-2292456818{font-size:3em;}"];␊
`import _JSXStyle from "styled-jsx/style";␊
const _defaultExport = ["div.jsx-2292456818{font-size:3em;}"];␊
_defaultExport.__hash = "2292456818";␊
module.exports = _defaultExport;`

Expand Down Expand Up @@ -88,7 +89,8 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import css from 'hell';␊
`import _JSXStyle from "styled-jsx/style";␊
import css from 'hell';␊
const color = 'red';␊
const bar = css`␊
div {␊
Expand Down Expand Up @@ -166,7 +168,9 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`const _defaultExport = new String("div.jsx-2292456818{font-size:3em;}");␊
`import _JSXStyle from "styled-jsx/style";␊
const _defaultExport = new String("div.jsx-2292456818{font-size:3em;}");␊
_defaultExport.__hash = "2292456818";␊
module.exports = _defaultExport;`
Expand Down
Binary file modified test/snapshots/external.js.snap
Binary file not shown.
5 changes: 4 additions & 1 deletion test/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`const a = () => <div>␊
`import _JSXStyle from "styled-jsx/style";␊
const a = () => <div>␊
<p>hi</p>␊
<style>{'woot'}</style>␊
</div>;`
Expand Down Expand Up @@ -148,6 +150,7 @@ Generated by [AVA](https://ava.li).
> Snapshot 1
`import _JSXStyle from "styled-jsx/style";␊
export const _StyleImport = '123';␊
export default class {␊
render() {␊
return <div className={"jsx-2101845350"}>␊
Expand Down
Binary file modified test/snapshots/index.js.snap
Binary file not shown.
Binary file modified test/snapshots/plugins.js.snap
Binary file not shown.

0 comments on commit 0d57f9e

Please sign in to comment.