From 94af863caec69d81816d0b261b92719ebb29995f Mon Sep 17 00:00:00 2001 From: xurudan01 Date: Thu, 23 Apr 2020 19:43:04 +0800 Subject: [PATCH] transformToDefaultImport can be a function --- README.md | 35 ++++++++++++++++++++++++++++++++++- src/Plugin.js | 20 ++++++++++++++++---- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 04584e1c..bf9b0ef7 100644 --- a/README.md +++ b/README.md @@ -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( + 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) {