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

Add Option to Keep Named Imports #277

Merged
merged 1 commit into from
Oct 17, 2018
Merged
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ For Example:
]
```

### style
#### style

- `["import", { "libraryName": "antd" }]`: import js modularly
- `["import", { "libraryName": "antd", "style": true }]`: import js and css modularly (LESS/Sass source files)
Expand Down Expand Up @@ -159,7 +159,7 @@ e.g.
]
```

### customName
#### customName

We can use `customName` to customize import file path.

Expand Down Expand Up @@ -204,6 +204,10 @@ import { TimePicker } from "antd"
var _button = require('antd/lib/custom-time-picker');
```

#### transformToDefaultImport

Set this option to `false` if your module does not have a `default` export.

### Note

babel-plugin-import will not work properly if you add the library to the webpack config [vendor](https://webpack.github.io/docs/code-splitting.html#split-app-and-vendor-code).
10 changes: 8 additions & 2 deletions src/Plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'path';
import { addSideEffect, addDefault } from '@babel/helper-module-imports';
import { addSideEffect, addDefault, addNamed } from '@babel/helper-module-imports';

function camel2Dash(_str) {
const str = _str[0].toLowerCase() + _str.substr(1);
Expand All @@ -24,6 +24,7 @@ export default class Plugin {
camel2UnderlineComponentName,
fileName,
customName,
transformToDefaultImport,
types
) {
this.specified = null;
Expand All @@ -40,6 +41,9 @@ export default class Plugin {
this.style = style || false;
this.fileName = fileName || '';
this.customName = customName;
this.transformToDefaultImport = typeof transformToDefaultImport === 'undefined'
? true
: transformToDefaultImport;
this.types = types;
}

Expand All @@ -61,7 +65,9 @@ export default class Plugin {
const path = winPath(
this.customName ? this.customName(transformedMethodName) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
);
this.selectedMethods[methodName] = addDefault(file.path, path, { nameHint: methodName });
this.selectedMethods[methodName] = this.transformToDefaultImport
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
if (style === true) {
addSideEffect(file.path, `${path}/style`);
} else if (style === 'css') {
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function ({ types }) {
camel2UnderlineComponentName,
fileName,
customName,
transformToDefaultImport,
}) => {
assert(libraryName, 'libraryName should be provided');
return new Plugin(
Expand All @@ -40,6 +41,7 @@ export default function ({ types }) {
camel2UnderlineComponentName,
fileName,
customName,
transformToDefaultImport,
types
);
});
Expand All @@ -54,6 +56,7 @@ export default function ({ types }) {
opts.camel2UnderlineComponentName,
opts.fileName,
opts.customName,
opts.transformToDefaultImport,
types
),
];
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/keep-named-import/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { start, end } from 'stream';

start();
end();
8 changes: 8 additions & 0 deletions test/fixtures/keep-named-import/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

var _end2 = require("stream/lib/end");

var _start2 = require("stream/lib/start");

(0, _start2.start)();
(0, _end2.end)();
4 changes: 4 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe('index', () => {
pluginWithOpts = [
plugin, { libraryName: 'material-ui', libraryDirectory: '', camel2DashComponentName: false },
];
} else if (caseName === 'keep-named-import') {
pluginWithOpts = [
plugin, { libraryName: 'stream', transformToDefaultImport: false },
];
} else if (caseName === 'react-toolbox') {
pluginWithOpts = [
plugin, { libraryName: 'react-toolbox', camel2UnderlineComponentName: true },
Expand Down