Skip to content

Commit 94af863

Browse files
author
xurudan01
committed
transformToDefaultImport can be a function
1 parent 39afe42 commit 94af863

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,40 @@ module.exports = function customName(name) {
253253

254254
#### transformToDefaultImport
255255

256-
Set this option to `false` if your module does not have a `default` export.
256+
Set this option to `false` if your module does not have a `default` export.
257+
258+
If `transformToDefaultImport` is a `Function`, your module will have a either `default` export or `named` export, depending on the function return value.
259+
260+
```js
261+
import { PageHeader } from "custom"
262+
ReactDOM.render(
263+
<PageHeader>xxxx</PageHeader>
264+
);
265+
266+
↓ ↓ ↓ ↓ ↓ ↓
267+
268+
var _page = require('custom/layout/page');
269+
ReactDOM.render(<_page.PageHeader>xxxx</_page.PageHeader>);
270+
```
271+
272+
```js
273+
[
274+
"import",
275+
{
276+
"libraryName": "custom",
277+
"customName": (name: string) => {
278+
if (name === 'PageHeader'){
279+
return 'custom/layout/page';
280+
}
281+
return `custom/${name}`;
282+
},
283+
"transformToDefaultImport": (name: string) => {
284+
return name !== 'PageHeader';
285+
},
286+
"camel2DashComponentName": false
287+
}
288+
]
289+
```
257290

258291
### Note
259292

src/Plugin.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ function normalizeCustomName(originCustomName) {
2121
return originCustomName;
2222
}
2323

24+
function resolveTransformToDefaultImport(transformToDefaultImport) {
25+
const type = typeof transformToDefaultImport;
26+
27+
return function (transformedMethodName) {
28+
if (type === 'undefined') {
29+
return true;
30+
}
31+
if (type === 'boolean') {
32+
return transformToDefaultImport;
33+
}
34+
return transformToDefaultImport(transformedMethodName);
35+
};
36+
}
37+
2438
export default class Plugin {
2539
constructor(
2640
libraryName,
@@ -49,9 +63,7 @@ export default class Plugin {
4963
this.customStyleName = normalizeCustomName(customStyleName);
5064
this.fileName = fileName || '';
5165
this.customName = normalizeCustomName(customName);
52-
this.transformToDefaultImport = typeof transformToDefaultImport === 'undefined'
53-
? true
54-
: transformToDefaultImport;
66+
this.transformToDefaultImport = resolveTransformToDefaultImport(transformToDefaultImport);
5567
this.types = types;
5668
this.pluginStateKey = `importPluginState${index}`;
5769
}
@@ -75,7 +87,7 @@ export default class Plugin {
7587
const path = winPath(
7688
this.customName ? this.customName(transformedMethodName, file) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
7789
);
78-
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
90+
pluginState.selectedMethods[methodName] = this.transformToDefaultImport(transformedMethodName) // eslint-disable-line
7991
? addDefault(file.path, path, { nameHint: methodName })
8092
: addNamed(file.path, methodName, path);
8193
if (this.customStyleName) {

0 commit comments

Comments
 (0)