Skip to content

Commit

Permalink
transformToDefaultImport can be a function
Browse files Browse the repository at this point in the history
  • Loading branch information
xurudan01 committed Apr 23, 2020
1 parent 39afe42 commit 94af863
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,40 @@ module.exports = function customName(name) {

#### transformToDefaultImport

Set this option to `false` if your module does not have a `default` export.
Set this option to `false` if your module does not have a `default` export.

If `transformToDefaultImport` is a `Function`, your module will have a either `default` export or `named` export, depending on the function return value.

```js
import { PageHeader } from "custom"
ReactDOM.render(
<PageHeader>xxxx</PageHeader>
);

↓ ↓ ↓ ↓ ↓ ↓

var _page = require('custom/layout/page');
ReactDOM.render(<_page.PageHeader>xxxx</_page.PageHeader>);
```

```js
[
"import",
{
"libraryName": "custom",
"customName": (name: string) => {
if (name === 'PageHeader'){
return 'custom/layout/page';
}
return `custom/${name}`;
},
"transformToDefaultImport": (name: string) => {
return name !== 'PageHeader';
},
"camel2DashComponentName": false
}
]
```

### Note

Expand Down
20 changes: 16 additions & 4 deletions src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ function normalizeCustomName(originCustomName) {
return originCustomName;
}

function resolveTransformToDefaultImport(transformToDefaultImport) {
const type = typeof transformToDefaultImport;

return function (transformedMethodName) {
if (type === 'undefined') {
return true;
}
if (type === 'boolean') {
return transformToDefaultImport;
}
return transformToDefaultImport(transformedMethodName);
};
}

export default class Plugin {
constructor(
libraryName,
Expand Down Expand Up @@ -49,9 +63,7 @@ export default class Plugin {
this.customStyleName = normalizeCustomName(customStyleName);
this.fileName = fileName || '';
this.customName = normalizeCustomName(customName);
this.transformToDefaultImport = typeof transformToDefaultImport === 'undefined'
? true
: transformToDefaultImport;
this.transformToDefaultImport = resolveTransformToDefaultImport(transformToDefaultImport);
this.types = types;
this.pluginStateKey = `importPluginState${index}`;
}
Expand All @@ -75,7 +87,7 @@ export default class Plugin {
const path = winPath(
this.customName ? this.customName(transformedMethodName, file) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
);
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
pluginState.selectedMethods[methodName] = this.transformToDefaultImport(transformedMethodName) // eslint-disable-line
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
if (this.customStyleName) {
Expand Down

0 comments on commit 94af863

Please sign in to comment.