diff --git a/README.md b/README.md index 04584e1c..62c7a578 100644 --- a/README.md +++ b/README.md @@ -253,7 +253,47 @@ 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. + +```typescript +import { PageHeader } from "custom" +↓ ↓ ↓ ↓ ↓ ↓ +var _page = require('custom/layout/page'); +_page.PageHeader +``` + +```js +import { PageHeader } from "custom" +ReactDOM.render( + xxxx +); + + ↓ ↓ ↓ ↓ ↓ ↓ + +var _page = require('custom/layout/page'); +ReactDOM.render(<_page.PageHeader>xxxx); +``` + +```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 diff --git a/src/Plugin.js b/src/Plugin.js index 28bdc9c5..da677a1d 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -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, @@ -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}`; } @@ -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) {