Skip to content

Commit 1549a8e

Browse files
author
wuhaolin
committed
fix: only import file when exits
1 parent a8ad0f6 commit 1549a8e

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ module.exports = {
5353
- **lib**: library want to replace,value is name in npm.
5454
- **libDir**: library directory path which store will use code, default it `lib`
5555
- **style**: should append style file to a component? value is relative path to style file.
56-
- **camel2**: should translate MyComponent to my-component, value is the join string.
56+
- **camel2**: should translate MyComponent to my-component, value is the join string.
57+
- **existCheck**: should check if import file exist, only import file when exits, default will check. To close this check set `existCheck=null`.
5758

5859
## Diff with babel-plugin-import
5960
1. babel-plugin-import is a babel plugin, which means must be used in project with babel.

lib/util.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path');
2+
const {existsSync} = require('fs');
23
// 用于匹配 import { Button } from 'antd';
34
const importReg = /import\s+{\s*(.+)\s*}\s+from\s+['"](.+)['"]/g;
45

@@ -12,12 +13,21 @@ function tranCamel2(componentName, joinString) {
1213
return componentName.replace(/([a-z])([A-Z])/g, `$1${joinString}$2`).toLowerCase();
1314
}
1415

16+
/**
17+
* file exists in node_modules?
18+
* @param filePath
19+
*/
20+
function fileExistInNpm(filePath) {
21+
return existsSync(path.resolve(__dirname, '../../', filePath));
22+
}
23+
1524
function replaceImport(source, options = {}) {
1625
const {
1726
lib,// lib name
1827
libDir = 'lib', //lib dir in npm
1928
style, // style file path
2029
camel2, // translate ComponentName to component-name | component_name
30+
existCheck = fileExistInNpm,// only import file when exits
2131
} = options;
2232
if (!lib) {
2333
return source;
@@ -34,10 +44,18 @@ function replaceImport(source, options = {}) {
3444
}
3545

3646
if (componentName.length > 0) {
37-
ret += `import ${componentName} from '${path.join(importFrom, libDir, componentPath)}'\n`;
47+
// import js file
48+
const jsFilePath = path.join(importFrom, libDir, componentPath);
49+
if (typeof existCheck !== 'function' || existCheck(jsFilePath)) {
50+
ret += `import ${componentName} from '${jsFilePath}'\n`;
51+
}
3852

3953
if (style) {
40-
ret += `import '${path.join(importFrom, libDir, componentPath, style)}'\n`;
54+
// import style file
55+
const styleFilePath = path.join(importFrom, libDir, componentPath, style);
56+
if (typeof existCheck !== 'function' || existCheck(styleFilePath)) {
57+
ret += `import '${styleFilePath}'\n`;
58+
}
4159
}
4260
}
4361
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ui-component-loader",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"main": "./lib/index.js",
55
"engines": {
66
"node": ">= 6.0.0"
@@ -12,6 +12,7 @@
1212
"webpack": "*"
1313
},
1414
"devDependencies": {
15+
"antd": "^2.13.4",
1516
"mocha": "^3.5.3"
1617
}
1718
}

test/index.test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,22 @@ import 'antd/lib/Icon/index.css'
100100
camel2: '-'
101101
},
102102
},
103+
{
104+
des: '使用 camel2 转化大小写',
105+
source: `import {Button} from 'antd'`,
106+
output: `import Button from 'antd/lib/button'\n`,
107+
options: {
108+
lib: 'antd',
109+
camel2: '-'
110+
},
111+
},
103112
];
104113

105-
testData.forEach(({des, source, output, options}) => {
114+
testData.forEach(({des, source, output, options = {}}) => {
106115
it(des, function () {
107-
let realOutput = replaceImport(source, options);
116+
let realOutput = replaceImport(source, Object.assign(options, {
117+
existCheck: null
118+
}));
108119
assert.equal(realOutput, output, `
109120
source=${source}
110121
options=${JSON.stringify(options)}

0 commit comments

Comments
 (0)