Skip to content

Commit

Permalink
add option to keep named imports (umijs#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Pupke authored and sorrycc committed Oct 17, 2018
1 parent 3932968 commit 04ab143
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
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

0 comments on commit 04ab143

Please sign in to comment.