Skip to content

Commit

Permalink
Merge pull request #221 from GeoffZhu/master
Browse files Browse the repository at this point in the history
Options style support customize & update README.md
  • Loading branch information
yesmeck authored Apr 4, 2018
2 parents d197ee0 + c5c6214 commit 2929996
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ For Example:
- `["import", { "libraryName": "antd", "style": true }]`: import js and css modularly (LESS/Sass source files)
- `["import", { "libraryName": "antd", "style": "css" }]`: import js and css modularly (css built files)

If option style is a `Function`, `babel-plugin-import` will auto import the file which filepath equal to the function return value. This is useful for the components library developers.

e.g.
- `["import", { "libraryName": "antd", "style": (name) => `${name}/style/2x` }]`: import js and css modularly & css file path is `ComponentName/style/2x`

### Note

babel-plugin-import will not work properly if you add the library to the webpack config [vendor](https://webpack.github.io/docs/code-splitting.html#split-app-and-vendor-code).
2 changes: 2 additions & 0 deletions src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export default class Plugin {
addSideEffect(file.path, `${path}/style`);
} else if (style === 'css') {
addSideEffect(file.path, `${path}/style/css`);
} else if (typeof style === 'function') {
addSideEffect(file.path, style(path));
}
}
return Object.assign({}, this.selectedMethods[methodName]);
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/custom-style-path/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';
import ReactDom from 'react-dom';
import { Button } from 'antd';

ReactDOM.render(<div>
<Button>xxxx</Button>
</div>, document.getElementById('react-container'));
13 changes: 13 additions & 0 deletions test/fixtures/custom-style-path/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

require("antd/lib/button/style/2x");

var _button = _interopRequireDefault(require("antd/lib/button"));

var _react = _interopRequireDefault(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

ReactDOM.render(_react.default.createElement("div", null, _react.default.createElement(_button.default, null, "xxxx")), document.getElementById('react-container'));
8 changes: 8 additions & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ describe('index', () => {
customName: (name) => `antd/lib/${name}`,
},
];
} else if (caseName === 'custom-style-path') {
pluginWithOpts = [
plugin,
{
libraryName: 'antd',
style: (name) => `${name}/style/2x`,
},
];
} else {
pluginWithOpts = [
plugin, { libraryName: 'antd' }
Expand Down

0 comments on commit 2929996

Please sign in to comment.