Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support mixed named export and default export #490

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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}`);
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default function ({ types }) {
plugins = null;
};

const a = [];
console.log(a);

function applyInstance(method, args, context) {
// eslint-disable-next-line no-restricted-syntax
for (const plugin of plugins) {
Expand Down Expand Up @@ -37,6 +40,7 @@ export default function ({ types }) {
fileName,
customName,
transformToDefaultImport,
mixedDefaultAndNamedExport,
},
index,
) => {
Expand All @@ -52,6 +56,7 @@ export default function ({ types }) {
fileName,
customName,
transformToDefaultImport,
mixedDefaultAndNamedExport,
types,
index,
);
Expand All @@ -71,6 +76,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