Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support module #422 #424

Merged
merged 8 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-transform-jsx-stylesheet",
"version": "0.6.11",
"version": "1.0.0",
"description": "Transform stylesheet selector to style in JSX Elements.",
"license": "BSD-3-Clause",
"main": "lib/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import jSXStylePlugin from '../index';
import { mergeStylesFunctionString, getClassNameFunctionString, getStyleFunctionString } from '../constants';
import syntaxJSX from 'babel-plugin-syntax-jsx';
import { transform } from 'babel-core';
import { mergeStylesFunctionString, getClassNameFunctionString, getStyleFunctionString } from '../constants';
import jSXStylePlugin from '../index';

function getTransfromCode(code, opts) {
export default function getTransformCode(code, opts) {
return transform(code, {
plugins: [
[jSXStylePlugin, opts],
Expand All @@ -14,7 +14,7 @@ function getTransfromCode(code, opts) {

describe('jsx style plugin', () => {
it('transform only one className to style as member', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, Component } from 'rax';
import './app.css';

Expand All @@ -35,7 +35,7 @@ class App extends Component {
});

it('transform multiple classNames to style as array', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, Component } from 'rax';
import './app.css';

Expand All @@ -56,7 +56,7 @@ class App extends Component {
});

it('transform array, object and expressions', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, Component } from 'rax';
import './app.css';

Expand Down Expand Up @@ -92,7 +92,7 @@ class App extends Component {
});

it('combine one style and className', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, Component } from 'rax';
import './app.css';
import styles from './style.css';
Expand All @@ -115,7 +115,7 @@ class App extends Component {
});

it('combine inline style object and className', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, Component } from 'rax';
import './app.css';

Expand All @@ -140,7 +140,7 @@ class App extends Component {
});

it('combine multiple styles and className', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, Component } from 'rax';
import './app.css';
import styles from './style.css';
Expand Down Expand Up @@ -172,11 +172,11 @@ class App extends Component {
}
}`;

expect(getTransfromCode(code)).toBe(code);
expect(getTransformCode(code)).toBe(code);
});

it('transform scss file', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, Component } from 'rax';
import './app.scss';

Expand All @@ -197,7 +197,7 @@ class App extends Component {
});

it('transform constant elements in render', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, render } from 'rax';
import './app.css';

Expand All @@ -211,7 +211,7 @@ render(<div style={_styleSheet["header"]} />);`);
});

it('dont remove className', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, render } from 'rax';
import './app.css';

Expand All @@ -233,7 +233,7 @@ describe('test development env', () => {
});

it('transform constant element in development env', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, render } from 'rax';
import './app.css';

Expand All @@ -253,7 +253,7 @@ render(<div __class="header" style={_styleSheet["header"]} />);`);

describe('edge test', () => {
it('transform class and style import same file', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, render } from 'rax';
import './app.css';
import styles from './app.css';
Expand All @@ -269,7 +269,7 @@ render(<div style={Object.assign({}, _styleSheet["header1"], styles.header)} />)
});

it('should transform two class files', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, render } from 'rax';
import './app1.css';
import './app2.css';
Expand All @@ -287,7 +287,7 @@ render(<div style={Object.assign({}, _styleSheet["header1"], _styleSheet["header
});

it('should delete className attr when empty string', () => {
expect(getTransfromCode(`
expect(getTransformCode(`
import { createElement, render } from 'rax';
import 'app.css'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import syntaxJSX from 'babel-plugin-syntax-jsx';
import { transform } from 'babel-core';
import jSXStylePlugin from '../index';

export default function getTransformCode(code, opts) {
return transform(code, {
plugins: [
[jSXStylePlugin, opts],
syntaxJSX
]
}).code;
}

describe('css module', () => {
it('should transform css module', () => {
expect(getTransformCode(`
import { createElement, render } from 'rax';
import styles from './app.css';

render(<div className={styles.header} />);
`)).toBe(`
import { createElement, render } from 'rax';
import styles from './app.css';

render(<div style={styles.header} />);`);
});
});
28 changes: 22 additions & 6 deletions packages/babel-plugin-transform-jsx-stylesheet/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ export default function({ types: t, template }, opts = {}) {
// className
// className=""
return [];
} else if (value.type === 'JSXExpressionContainer' && value.expression && typeof value.expression.value !== 'string') {
} else if (value.type === 'JSXExpressionContainer' &&
value.expression &&
typeof value.expression.value !== 'string'
) {
// className={{ container: true }}
// className={['container wrapper', { scroll: false }]}
if (value.expression.type === 'MemberExpression') {
return [value.expression];
}
return [t.callExpression(t.identifier(GET_STYLE_FUNC_NAME), [value.expression])];
} else {
// className="container"
Expand Down Expand Up @@ -102,11 +108,6 @@ export default function({ types: t, template }, opts = {}) {
convertImport = true, // default to true
} = opts;

const cssFileCount = file.get('cssFileCount') || 0;
if (cssFileCount < 1 && convertImport !== false) {
return;
}

// Check if has "style"
let hasStyleAttribute = false;
let styleAttribute;
Expand All @@ -129,6 +130,16 @@ export default function({ types: t, template }, opts = {}) {
}
}

// like className={ x.xxx }
const isCssModule = hasClassName && classNameAttribute.value &&
classNameAttribute.value.type === 'JSXExpressionContainer' &&
classNameAttribute.value.expression.type === 'MemberExpression';

const cssFileCount = file.get('cssFileCount') || 0;
if (!isCssModule && cssFileCount < 1 && convertImport !== false) {
return;
}

if (hasClassName) {
// Dont remove className
if (!retainClassName) {
Expand All @@ -149,6 +160,11 @@ export default function({ types: t, template }, opts = {}) {
file.set('injectGetStyle', true);
}

if (isCssModule) {
classNameAttribute.name.name = 'style';
file.set('injectGetStyle', false);
}

const arrayExpression = getArrayExpression(classNameAttribute.value);

if (arrayExpression.length === 0) {
Expand Down