diff --git a/css.js b/css.js index 6521536a..b49a8b1f 100644 --- a/css.js +++ b/css.js @@ -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 diff --git a/css.mjs b/css.mjs new file mode 100644 index 00000000..855e1152 --- /dev/null +++ b/css.mjs @@ -0,0 +1,5 @@ +import decoys from './dist/decoys.js' + +export default decoys.css +export const global = decoys.global +export const resolve = decoys.resolve diff --git a/readme.md b/readme.md index a1af73af..19516fec 100644 --- a/readme.md +++ b/readme.md @@ -26,7 +26,7 @@ Code and docs are for v3 which we highly recommend you to try. Looking for style * [Via inline `style`](#via-inline-style) - [Constants](#constants) - [Server-Side Rendering](#server-side-rendering) - * [`styled-jsx/server`](#styled-jsxserver) + * [`styled-jsx/server.js`](#styled-jsxserver) - [External CSS and styles outside of the component](#external-css-and-styles-outside-of-the-component) * [External styles](#external-styles) * [Styles outside of components](#styles-outside-of-components) @@ -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 () => (
@@ -316,8 +316,8 @@ In this example, the padding defaults to the one set in `␊ @@ -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 (() =>

test

woot

␊ diff --git a/test/babel6/snapshots/index.js.snap b/test/babel6/snapshots/index.js.snap index 985b671e..98c7a84d 100644 Binary files a/test/babel6/snapshots/index.js.snap and b/test/babel6/snapshots/index.js.snap differ diff --git a/test/babel6/snapshots/macro.js.md b/test/babel6/snapshots/macro.js.md index eecb7c37..c6193f40 100644 --- a/test/babel6/snapshots/macro.js.md +++ b/test/babel6/snapshots/macro.js.md @@ -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';␊ ␊ ␊ ({␊ @@ -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() {␊ @@ -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 } = {␊ diff --git a/test/babel6/snapshots/macro.js.snap b/test/babel6/snapshots/macro.js.snap index 532e5ab0..2c178ddf 100644 Binary files a/test/babel6/snapshots/macro.js.snap and b/test/babel6/snapshots/macro.js.snap differ diff --git a/test/babel6/snapshots/plugins.js.md b/test/babel6/snapshots/plugins.js.md index 546b315c..d4c9f73c 100644 --- a/test/babel6/snapshots/plugins.js.md +++ b/test/babel6/snapshots/plugins.js.md @@ -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';␊ ␊ @@ -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';␊ ␊ diff --git a/test/babel6/snapshots/plugins.js.snap b/test/babel6/snapshots/plugins.js.snap index 1a809ca1..21d71768 100644 Binary files a/test/babel6/snapshots/plugins.js.snap and b/test/babel6/snapshots/plugins.js.snap differ diff --git a/test/index.js b/test/index.js index 39fb1006..9b4cddf5 100644 --- a/test/index.js +++ b/test/index.js @@ -5,7 +5,7 @@ import ReactDOM from 'react-dom/server' // Ours import plugin from '../src/babel' -import _transform from './_transform' +import _transform, { transformSource as _transformSource } from './_transform' const transform = (file, opts = {}) => _transform(file, { @@ -13,6 +13,12 @@ const transform = (file, opts = {}) => ...opts }) +const transformSource = (src, opts = {}) => + _transformSource(src.trim(), { + plugins: [[plugin, opts]], + ...opts + }) + test('handles dynamic `this` value inside of arrow function', async t => { const { code } = await transform( './fixtures/dynamic-this-value-in-arrow.js', @@ -267,3 +273,39 @@ test('optimized styles do not contain new lines', t => { t.is(html, `${expected}`) }) + +test('Babel plugin matches import specifier styled-jsx/css', async t => { + const { code } = await transformSource(` + import css from 'styled-jsx/css' + + css.resolve\`div { color: red }\` + `) + t.snapshot(code) +}) + +test('Babel plugin matches import specifier styled-jsx/css.mjs', async t => { + const { code } = await transformSource(` + import css from 'styled-jsx/css.mjs' + + css.resolve\`div { color: red }\` + `) + t.snapshot(code) +}) + +test('Babel plugin matches import specifier styled-jsx/css.js', async t => { + const { code } = await transformSource(` + import css from 'styled-jsx/css.js' + + css.resolve\`div { color: red }\` + `) + t.snapshot(code) +}) + +test('Babel plugin doesn’t match an unrelated import specifier', async t => { + const { code } = await transformSource(` + import css from 'x' + + css.resolve\`div { color: red }\` + `) + t.snapshot(code) +}) diff --git a/test/snapshots/attribute.js.md b/test/snapshots/attribute.js.md index 594ec1a8..c226a2c6 100644 --- a/test/snapshots/attribute.js.md +++ b/test/snapshots/attribute.js.md @@ -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'); // external only␊ @@ -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";␊ export default (() => {␊ const Element = 'div';␊ return
␊ diff --git a/test/snapshots/attribute.js.snap b/test/snapshots/attribute.js.snap index 61300916..5e980061 100644 Binary files a/test/snapshots/attribute.js.snap and b/test/snapshots/attribute.js.snap differ diff --git a/test/snapshots/external.js.md b/test/snapshots/external.js.md index e1f84dcb..2951c2e0 100644 --- a/test/snapshots/external.js.md +++ b/test/snapshots/external.js.md @@ -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';␊ const bar = ["div.jsx-2141779268{font-size:3em;}"];␊ @@ -55,7 +55,7 @@ Generated by [AVA](https://ava.li). `"use strict";␊ ␊ - var _style = _interopRequireDefault(require("styled-jsx/style"));␊ + var _style = _interopRequireDefault(require("styled-jsx/style.js"));␊ ␊ var _App = _interopRequireDefault(require("./App.styles"));␊ ␊ @@ -99,7 +99,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 - `import _JSXStyle from "styled-jsx/style";␊ + `import _JSXStyle from "styled-jsx/style.js";␊ ␊ function test() {␊ ({␊ @@ -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';␊ const bar = new String("div.jsx-2141779268{font-size:3em;}");␊ @@ -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";␊ import styles from './styles2';␊ export default (({␊ level = 1␊ @@ -174,7 +174,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');␊ @@ -201,7 +201,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');␊ @@ -219,7 +219,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 (() =>

test

␊ diff --git a/test/snapshots/external.js.snap b/test/snapshots/external.js.snap index b7551ed6..60d66372 100644 Binary files a/test/snapshots/external.js.snap and b/test/snapshots/external.js.snap differ diff --git a/test/snapshots/index.js.md b/test/snapshots/index.js.md index 46d5ce7b..36fcc445 100644 --- a/test/snapshots/index.js.md +++ b/test/snapshots/index.js.md @@ -4,11 +4,41 @@ The actual snapshot is saved in `index.js.snap`. Generated by [AVA](https://ava.li). +## Babel plugin matches import specifier styled-jsx/css + +> Snapshot 1 + + `import _JSXStyle from "styled-jsx/style.js";␊ + ({␊ + styles: <_JSXStyle id={"2886504620"}>{"div.jsx-2886504620{color:red;}"},␊ + className: "jsx-2886504620"␊ + });` + +## Babel plugin matches import specifier styled-jsx/css.js + +> Snapshot 1 + + `import _JSXStyle from "styled-jsx/style.js";␊ + ({␊ + styles: <_JSXStyle id={"2886504620"}>{"div.jsx-2886504620{color:red;}"},␊ + className: "jsx-2886504620"␊ + });` + +## Babel plugin matches import specifier styled-jsx/css.mjs + +> Snapshot 1 + + `import _JSXStyle from "styled-jsx/style.js";␊ + ({␊ + styles: <_JSXStyle id={"2886504620"}>{"div.jsx-2886504620{color:red;}"},␊ + className: "jsx-2886504620"␊ + });` + ## generates source maps > Snapshot 1 - `import _JSXStyle from "styled-jsx/style";␊ + `import _JSXStyle from "styled-jsx/style.js";␊ export default (() =>

test

woot

␊ @@ -19,7 +49,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 - `import _JSXStyle from "styled-jsx/style";␊ + `import _JSXStyle from "styled-jsx/style.js";␊ import React, { Component } from 'react';␊ export default class Index extends Component {␊ static getInitialProps() {␊ @@ -72,7 +102,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 - `import _JSXStyle from "styled-jsx/style";␊ + `import _JSXStyle from "styled-jsx/style.js";␊ export default (() =>

test

woot

␊ @@ -84,7 +114,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;}"};␊ ␊ @@ -98,7 +128,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';␊ ␊ @@ -121,7 +151,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 - `import _JSXStyle from "styled-jsx/style";␊ + `import _JSXStyle from "styled-jsx/style.js";␊ ␊ const Test = () =>
test␊ @@ -133,7 +163,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() {␊ return
␊ @@ -148,7 +178,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␊ }) =>
␊ @@ -172,7 +202,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␊ }) => {␊ @@ -203,7 +233,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';␊ @@ -229,7 +259,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;␊ ␊ @@ -265,7 +295,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 - `import _JSXStyle from "styled-jsx/style";␊ + `import _JSXStyle from "styled-jsx/style.js";␊ import React from 'react';␊ export default (() => <>␊

Testing!!!

␊ @@ -300,7 +330,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={"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;}"}␊ @@ -312,7 +342,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'␊ };␊ @@ -344,7 +374,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 - `import _JSXStyle from "styled-jsx/style";␊ + `import _JSXStyle from "styled-jsx/style.js";␊ export default (() =>

woot