Skip to content

Commit

Permalink
Support Node.js ESM.
Browse files Browse the repository at this point in the history
Fixes vercel#640 .
  • Loading branch information
jaydenseric committed May 12, 2020
1 parent d111ffc commit b947efd
Show file tree
Hide file tree
Showing 27 changed files with 174 additions and 86 deletions.
24 changes: 3 additions & 21 deletions css.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
function notTranspiledError(name) {
throw new Error(
'styled-jsx/css: if you are getting this error it means that your `' +
name +
'` tagged template literals were not transpiled.'
)
}

function css() {
notTranspiledError('css')
}

css.global = function() {
notTranspiledError('global')
}

css.resolve = function() {
notTranspiledError('resolve')
}
const { css, global, resolve } = require('./dist/decoys')

module.exports = css
module.exports.global = css.global
module.exports.resolve = css.resolve
module.exports.global = global
module.exports.resolve = resolve
5 changes: 5 additions & 0 deletions css.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import decoys from './dist/decoys.js'

export default decoys.css
export const global = decoys.global
export const resolve = decoys.resolve
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Turn on/off automatic vendor prefixing (default: `true`)
The example above transpiles to the following:

```jsx
import _JSXStyle from 'styled-jsx/style'
import _JSXStyle from 'styled-jsx/style.js'

export default () => (
<div className="jsx-123">
Expand Down
2 changes: 1 addition & 1 deletion src/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ export const booleanOption = opts => {
export const createReactComponentImportDeclaration = () =>
t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(STYLE_COMPONENT))],
t.stringLiteral('styled-jsx/style')
t.stringLiteral('styled-jsx/style.js')
)

export const setStateOptions = state => {
Expand Down
9 changes: 6 additions & 3 deletions src/babel-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,11 @@ function addHash(exportIdentifier, hash) {

export const visitor = {
ImportDeclaration(path, state) {
// import css from 'styled-jsx/css'
if (path.node.source.value !== 'styled-jsx/css') {
// Bail if the import doesn’t have one of these specifiers:
// - styled-jsx/css
// - styled-jsx/css.mjs
// - 'styled-jsx/css.js
if (!/^styled-jsx\/css(?:\.m?js)?$/.test(path.node.source.value)) {
return
}

Expand Down Expand Up @@ -218,7 +221,7 @@ export const visitor = {
)

// When using the `resolve` helper we need to add an import
// for the _JSXStyle component `styled-jsx/style`
// for the _JSXStyle component `styled-jsx/style.js`
if (
hasJSXStyle &&
taggedTemplateExpressions.resolve.length > 0 &&
Expand Down
19 changes: 19 additions & 0 deletions src/decoys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function notTranspiledError(name) {
throw new Error(
'styled-jsx/css: if you are getting this error it means that your `' +
name +
'` tagged template literals were not transpiled.'
)
}

export function css() {
notTranspiledError('css')
}

export function global() {
notTranspiledError('global')
}

export function resolve() {
notTranspiledError('resolve')
}
4 changes: 2 additions & 2 deletions test/babel6/snapshots/attribute.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Generated by [AVA](https://ava.li).

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXStyle from 'styled-jsx/style.js';␊
import styles from './styles';␊
const styles2 = require('./styles2');␊
Expand Down Expand Up @@ -108,7 +108,7 @@ Generated by [AVA](https://ava.li).
`var _this = this;␊
import _JSXStyle from "styled-jsx/style";␊
import _JSXStyle from "styled-jsx/style.js";␊
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.
14 changes: 7 additions & 7 deletions test/babel6/snapshots/external.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Generated by [AVA](https://ava.li).

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

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

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

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

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

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

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXStyle from 'styled-jsx/style.js';␊
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.
28 changes: 14 additions & 14 deletions test/babel6/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Generated by [AVA](https://ava.li).

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

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

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

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

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

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

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

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

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

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

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXStyle from 'styled-jsx/style.js';␊
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 +226,7 @@ Generated by [AVA](https://ava.li).

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

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

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXStyle from 'styled-jsx/style.js';␊
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.
6 changes: 3 additions & 3 deletions test/babel6/snapshots/macro.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Generated by [AVA](https://ava.li).

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

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

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

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

> Snapshot 1
`import _JSXStyle from 'styled-jsx/style';␊
`import _JSXStyle from 'styled-jsx/style.js';␊
import styles from './styles';␊
const color = 'red';␊
Expand Down
Binary file modified test/babel6/snapshots/plugins.js.snap
Binary file not shown.
Loading

0 comments on commit b947efd

Please sign in to comment.