Skip to content

Commit

Permalink
feat: support mixed named export and default export
Browse files Browse the repository at this point in the history
  • Loading branch information
decadef20 committed Jul 16, 2020
1 parent f39e711 commit adad7e5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
19 changes: 16 additions & 3 deletions src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default class Plugin {
fileName,
customName,
transformToDefaultImport,
mixedDefaultAndNamedExport,
types,
index = 0,
) {
Expand All @@ -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;
Expand Down Expand Up @@ -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.transformToDefaultImport) {
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
} else if (this.mixedDefaultAndNamedExport) {
pluginState.selectedMethods[methodName] = this.mixedDefaultAndNamedExport(
// eslint-disable-line
transformedMethodName,
file,
) // eslint-disable-line
? addNamed(file.path, methodName, path)
: addDefault(file.path, path, { nameHint: methodName });
}

if (this.customStyleName) {
const stylePath = winPath(this.customStyleName(transformedMethodName));
addSideEffect(file.path, `${stylePath}`);
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default function ({ types }) {
fileName,
customName,
transformToDefaultImport,
mixedDefaultAndNamedExport,
},
index,
) => {
Expand All @@ -52,6 +53,7 @@ export default function ({ types }) {
fileName,
customName,
transformToDefaultImport,
mixedDefaultAndNamedExport,
types,
index,
);
Expand All @@ -71,6 +73,7 @@ export default function ({ types }) {
opts.fileName,
opts.customName,
opts.transformToDefaultImport,
opts.mixedDefaultAndNamedExport,
types,
),
];
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/mixed-default-and-named-export/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { start, end } from 'stream';

start();
end()
10 changes: 10 additions & 0 deletions test/fixtures/mixed-default-and-named-export/expected.js
Original file line number Diff line number Diff line change
@@ -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)();
5 changes: 5 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit adad7e5

Please sign in to comment.