From 91be2c773bcaeed1b60bd0bb95020b1ea978155c Mon Sep 17 00:00:00 2001 From: decadef20 Date: Thu, 16 Jul 2020 13:37:00 +0800 Subject: [PATCH] feat: support mixed named export and default export --- README.md | 16 ++++++++++++++++ src/Plugin.js | 19 ++++++++++++++++--- src/index.js | 3 +++ .../mixed-default-and-named-export/actual.js | 4 ++++ .../expected.js | 10 ++++++++++ test/index.test.js | 5 +++++ 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/mixed-default-and-named-export/actual.js create mode 100644 test/fixtures/mixed-default-and-named-export/expected.js diff --git a/README.md b/README.md index 04584e1c..f4049e33 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,22 @@ module.exports = function customName(name) { Set this option to `false` if your module does not have a `default` export. +#### mixedDefaultAndNamedExport +we can decide which files should be used as named export +```js +[ + "import", + { + "libraryName": "stream", + "mixedDefaultAndNamedExport": name => { + if (name === 'start'){ + return true + } + } + } +] +``` + ### Note babel-plugin-import will not work properly if you add the library to the webpack config [vendor](https://webpack.js.org/concepts/entry-points/#separate-app-and-vendor-entries). diff --git a/src/Plugin.js b/src/Plugin.js index 95259b13..1ad43e39 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -33,6 +33,7 @@ export default class Plugin { fileName, customName, transformToDefaultImport, + mixedDefaultAndNamedExport, types, index = 0, ) { @@ -45,6 +46,7 @@ export default class Plugin { this.styleLibraryDirectory = styleLibraryDirectory; this.customStyleName = normalizeCustomName(customStyleName); this.fileName = fileName || ''; + this.mixedDefaultAndNamedExport = mixedDefaultAndNamedExport || false; this.customName = normalizeCustomName(customName); this.transformToDefaultImport = typeof transformToDefaultImport === 'undefined' ? true : transformToDefaultImport; @@ -72,9 +74,20 @@ export default class Plugin { ? this.customName(transformedMethodName, file) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName), // eslint-disable-line ); - pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line - ? addDefault(file.path, path, { nameHint: methodName }) - : addNamed(file.path, methodName, path); + if (this.mixedDefaultAndNamedExport) { + pluginState.selectedMethods[methodName] = this.mixedDefaultAndNamedExport( + // eslint-disable-line + transformedMethodName, + file, + ) + ? addNamed(file.path, methodName, path) + : addDefault(file.path, path, { nameHint: methodName }); + } else { + pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line + ? addDefault(file.path, path, { nameHint: methodName }) + : addNamed(file.path, methodName, path); + } + if (this.customStyleName) { const stylePath = winPath(this.customStyleName(transformedMethodName)); addSideEffect(file.path, `${stylePath}`); diff --git a/src/index.js b/src/index.js index 8d5c7c02..18cf799f 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,7 @@ export default function ({ types }) { fileName, customName, transformToDefaultImport, + mixedDefaultAndNamedExport, }, index, ) => { @@ -52,6 +53,7 @@ export default function ({ types }) { fileName, customName, transformToDefaultImport, + mixedDefaultAndNamedExport, types, index, ); @@ -71,6 +73,7 @@ export default function ({ types }) { opts.fileName, opts.customName, opts.transformToDefaultImport, + opts.mixedDefaultAndNamedExport, types, ), ]; diff --git a/test/fixtures/mixed-default-and-named-export/actual.js b/test/fixtures/mixed-default-and-named-export/actual.js new file mode 100644 index 00000000..3e9bed51 --- /dev/null +++ b/test/fixtures/mixed-default-and-named-export/actual.js @@ -0,0 +1,4 @@ +import { start, end } from 'stream'; + +start(); +end() diff --git a/test/fixtures/mixed-default-and-named-export/expected.js b/test/fixtures/mixed-default-and-named-export/expected.js new file mode 100644 index 00000000..3d71c3d3 --- /dev/null +++ b/test/fixtures/mixed-default-and-named-export/expected.js @@ -0,0 +1,10 @@ +"use strict"; + +var _end2 = _interopRequireDefault(require("stream/lib/end")); + +var _start2 = require("stream/lib/start"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +(0, _start2.start)(); +(0, _end2.default)(); \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 7f8e54dc..e488f342 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -33,6 +33,11 @@ describe('index', () => { ]; } else if (caseName === 'keep-named-import') { pluginWithOpts = [plugin, { libraryName: 'stream', transformToDefaultImport: false }]; + } else if (caseName === 'mixed-default-and-named-export') { + pluginWithOpts = [ + plugin, + { libraryName: 'stream', mixedDefaultAndNamedExport: name => name === 'start' }, + ]; } else if (caseName === 'react-toolbox') { pluginWithOpts = [ plugin,