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 14fe5e0
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 35 deletions.
20 changes: 12 additions & 8 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 All @@ -10,7 +10,6 @@ import {
STYLE_ATTRIBUTE,
GLOBAL_ATTRIBUTE,
STYLE_COMPONENT_ID,
STYLE_COMPONENT,
STYLE_COMPONENT_DYNAMIC
} from './_constants'

Expand Down Expand Up @@ -258,7 +257,7 @@ export const getJSXStyleInfo = (expr, scope) => {
}
}

export const computeClassNames = (styles, externalJsxId) => {
export const computeClassNames = (styles, externalJsxId, componentName) => {
if (styles.length === 0) {
return {
className: externalJsxId
Expand Down Expand Up @@ -297,7 +296,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(componentName), t.identifier('dynamic')),
// Arguments
[
t.arrayExpression(
Expand Down Expand Up @@ -380,7 +379,12 @@ export const cssToBabelType = css => {
return t.cloneDeep(css)
}

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

const attributes = [
Expand All @@ -402,8 +406,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(componentName), attributes),
t.jSXClosingElement(t.jSXIdentifier(componentName)),
[t.jSXExpressionContainer(css)]
)
}
Expand Down Expand Up @@ -627,7 +631,7 @@ export const createReactComponentImportDeclaration = state => {
typeof state.opts.styleModule === 'string'
? state.opts.styleModule
: 'styled-jsx/style',
{ nameHint: STYLE_COMPONENT}
{ nameHint: state.componentName }
)
}

Expand Down
16 changes: 11 additions & 5 deletions src/babel-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function processTaggedTemplateExpression({
splitRules,
plugins,
vendorPrefixes,
sourceMaps
sourceMaps,
componentName
}) {
const templateLiteral = path.get('quasi')
let scope
Expand All @@ -39,7 +40,11 @@ export function processTaggedTemplateExpression({

const stylesInfo = getJSXStyleInfo(templateLiteral, scope)

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

const styles = processCss(
{
Expand All @@ -64,7 +69,7 @@ export function processTaggedTemplateExpression({
t.objectExpression([
t.objectProperty(
t.identifier('styles'),
makeStyledJsxTag(hash, css, expressions)
makeStyledJsxTag(hash, css, expressions, componentName)
),
t.objectProperty(t.identifier('className'), className)
])
Expand Down Expand Up @@ -212,7 +217,8 @@ export const visitor = {
: process.env.NODE_ENV === 'production',
plugins: state.plugins,
vendorPrefixes,
sourceMaps
sourceMaps,
componentName: state.componentName
})
})
)
Expand All @@ -223,7 +229,7 @@ export const visitor = {
hasJSXStyle &&
taggedTemplateExpressions.resolve.length > 0 &&
!state.hasInjectedJSXStyle &&
!path.scope.hasBinding(STYLE_COMPONENT)
!path.scope.hasBinding(state.componentName)
) {
state.hasInjectedJSXStyle = true
createReactComponentImportDeclaration(state)
Expand Down
16 changes: 10 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.componentName
)
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.componentName))
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.componentName)
)
}
}
}
Expand Down Expand Up @@ -286,6 +289,9 @@ export default function({ types: t }) {

setStateOptions(state)

state.componentName = STYLE_COMPONENT
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 +305,12 @@ export default function({ types: t }) {
!(
state.file.hasJSXStyle &&
!state.hasInjectedJSXStyle &&
!scope.hasBinding(STYLE_COMPONENT)
!scope.hasBinding(state.componentName)
)
) {
return
}

state.hasInjectedJSXStyle = true
createReactComponentImportDeclaration(state)
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function styledJsxMacro({ references, state }) {
// People who want to migrate from this macro to pure styled-jsx might have name conflicts issues.
const cssReferences = []

state.componentName = STYLE_COMPONENT

// references looks like this
// {
// default: [path, path],
Expand Down Expand Up @@ -94,12 +96,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,
componentName: state.componentName
})

if (
!state.hasInjectedJSXStyle &&
!path.scope.hasBinding(STYLE_COMPONENT)
!path.scope.hasBinding(state.componentName)
) {
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 14fe5e0

Please sign in to comment.