Skip to content

Commit

Permalink
add styleLibraryDirectory option, fixes #381
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenPCG authored and Zhang Cheng committed Nov 23, 2019
1 parent 8efa514 commit aeb9193
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ e.g.
]
```

#### styleLibraryDirectory

- `["import", { "libraryName": "element-ui", "styleLibraryDirectory": "lib/theme-chalk" }]`: import js and css modularly

If `styleLibraryDirectory` is provided (default `null`), it will be used to form style file path,
`style` will be ignored then. e.g.

```javascript
{
"libraryName": "element-ui",
"styleLibraryDirectory": "lib/theme-chalk",
}

import { Button } from 'element-ui';

↓ ↓ ↓ ↓ ↓ ↓

var _button = require('element-ui/lib/button');
require('element-ui/lib/theme-chalk/button');
```

#### customName

We can use `customName` to customize import file path.
Expand Down Expand Up @@ -226,6 +247,10 @@ module.exports = function customName(name) {
};
```

#### customStyleName

`customStyleName` works exactly the same as customName, except that it deals with style file path.

#### transformToDefaultImport

Set this option to `false` if your module does not have a `default` export.
Expand Down
14 changes: 13 additions & 1 deletion src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export default class Plugin {
libraryName,
libraryDirectory,
style,
styleLibraryDirectory,
customStyleName,
camel2DashComponentName,
camel2UnderlineComponentName,
fileName,
Expand All @@ -43,6 +45,8 @@ export default class Plugin {
: camel2DashComponentName;
this.camel2UnderlineComponentName = camel2UnderlineComponentName;
this.style = style || false;
this.styleLibraryDirectory = styleLibraryDirectory;
this.customStyleName = normalizeCustomName(customStyleName);
this.fileName = fileName || '';
this.customName = normalizeCustomName(customName);
this.transformToDefaultImport = typeof transformToDefaultImport === 'undefined'
Expand Down Expand Up @@ -74,7 +78,15 @@ export default class Plugin {
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
if (style === true) {
if (this.customStyleName) {
const stylePath = winPath(this.customStyleName(transformedMethodName));
addSideEffect(file.path, `${stylePath}`);
} else if (this.styleLibraryDirectory) {
const stylePath = winPath(
join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName)
);
addSideEffect(file.path, `${stylePath}`);
} else if (style === true) {
addSideEffect(file.path, `${path}/style`);
} else if (style === 'css') {
addSideEffect(file.path, `${path}/style/css`);
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export default function ({ types }) {
libraryName,
libraryDirectory,
style,
styleLibraryDirectory,
customStyleName,
camel2DashComponentName,
camel2UnderlineComponentName,
fileName,
Expand All @@ -37,6 +39,8 @@ export default function ({ types }) {
libraryName,
libraryDirectory,
style,
styleLibraryDirectory,
customStyleName,
camel2DashComponentName,
camel2UnderlineComponentName,
fileName,
Expand All @@ -53,6 +57,8 @@ export default function ({ types }) {
opts.libraryName,
opts.libraryDirectory,
opts.style,
opts.styleLibraryDirectory,
opts.customStyleName,
opts.camel2DashComponentName,
opts.camel2UnderlineComponentName,
opts.fileName,
Expand Down

0 comments on commit aeb9193

Please sign in to comment.