Skip to content

Commit

Permalink
feat(customStyleName): 完善customStyleName传参 (#603)
Browse files Browse the repository at this point in the history
* feat(customStyleName): 完善customStyleName传参

* test(customStyleName): 提供customStyleName传入file参数的测例

* test(customStyleName): 补充customStyleName 的分支测例
  • Loading branch information
DawnLck authored Apr 11, 2022
1 parent d21264e commit 469c018
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class Plugin {
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
if (this.customStyleName) {
const stylePath = winPath(this.customStyleName(transformedMethodName));
const stylePath = winPath(this.customStyleName(transformedMethodName, file));
addSideEffect(file.path, `${stylePath}`);
} else if (this.styleLibraryDirectory) {
const stylePath = winPath(
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/custom-style-name-file/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Button } from 'element-ui';

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

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

require("../../../../element-ui/lib/theme-light/button");

var _button2 = _interopRequireDefault(require("element-ui/lib/button"));

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

ReactDOM.render( /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_button2.default, null, "xxxx")));
14 changes: 14 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ describe('index', () => {
customStyleName: name => `element-ui/lib/theme-light/${name}`,
},
];
} else if (caseName === 'custom-style-name-file') {
pluginWithOpts = [
plugin,
{
libraryName: 'element-ui',
customStyleName: (name, file) => {
const { root, filename } = file?.opts;
const diff = filename?.slice(root?.length);
const count = diff.match(/\//g).length;
const prefix = String.prototype.repeat.call('../', count);
return `${prefix}element-ui/lib/theme-light/${name}`;
},
},
];
} else {
pluginWithOpts = [plugin, { libraryName: 'antd' }];
}
Expand Down

0 comments on commit 469c018

Please sign in to comment.