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: add dont remove className option #1171

Merged
merged 2 commits into from
Jul 30, 2019
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
Expand Up @@ -66,9 +66,12 @@ const getStyleFunctionTemplete = `function _getStyle(classNameExpression) {
return style;
}`;

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

Expand Down Expand Up @@ -273,6 +276,20 @@ import appStyleSheet from './app.css';
var _styleSheet = appStyleSheet;
render(<div style={_styleSheet["header"]} />);`);
});

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

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

var _styleSheet = appStyleSheet;
render(<div className="header" style={_styleSheet["header"]} />);`);
});
});

describe('test development env', () => {
Expand All @@ -299,4 +316,4 @@ render(<div __class="header" style={_styleSheet["header"]} />);`);
afterEach(() => {
process.env.NODE_ENV = lastEnv;
});
});
});
19 changes: 12 additions & 7 deletions packages/babel-plugin-transform-jsx-stylesheet/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ function ${GET_STYLE_FUNC_NAME}(classNameExpression) {
}
}
},
JSXOpeningElement({ container }, { file }) {
JSXOpeningElement({ container }, { file, opts }) {
const { retainClassName = false } = opts;

const cssFileCount = file.get('cssFileCount') || 0;
if (cssFileCount < 1) {
return;
Expand Down Expand Up @@ -177,12 +179,15 @@ function ${GET_STYLE_FUNC_NAME}(classNameExpression) {


if (hasClassName) {
// development env: change className to __class
if (process.env.NODE_ENV === 'development' && classNameAttribute.name) {
classNameAttribute.name.name = '__class';
} else {
// Remove origin className
attributes.splice(attributes.indexOf(classNameAttribute), 1);
// Dont remove className
if (!retainClassName) {
// development env: change className to __class
if (process.env.NODE_ENV === 'development' && classNameAttribute.name) {
classNameAttribute.name.name = '__class';
} else {
// Remove origin className
attributes.splice(attributes.indexOf(classNameAttribute), 1);
}
}

if (
Expand Down