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 2 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": "0.7.11",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个版本变化是啥操作。。。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

"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,16 +1,5 @@
import jSXStylePlugin from '../index';
import syntaxJSX from 'babel-plugin-syntax-jsx';
import { transform } from 'babel-core';
import { mergeStylesFunctionString, getClassNameFunctionString, getStyleFunctionString } from '../constants';

function getTransfromCode(code, opts) {
return transform(code, {
plugins: [
[jSXStylePlugin, opts],
syntaxJSX
]
}).code;
}
import getTransfromCode from '../getTransfromCode';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transfrom -> transform

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


describe('jsx style plugin', () => {
it('transform only one className to style as member', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import getTransfromCode from '../getTransfromCode';

describe('css module', () => {
it('should transform css module', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ci 看下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

expect(getTransfromCode(`
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} />);`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import syntaxJSX from 'babel-plugin-syntax-jsx';
import { transform } from 'babel-core';
import jSXStylePlugin from './index';

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

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