Skip to content

Commit 7c9865e

Browse files
committed
feat: add import on demand
1 parent cbcfcdc commit 7c9865e

File tree

4 files changed

+43
-13069
lines changed

4 files changed

+43
-13069
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ npm run build
8181
# https://cli.vuejs.org/guide/troubleshooting.html#symbolic-links-in-node-modules
8282
```
8383

84+
## 依赖方如何安装组件库?
85+
86+
### 全量导入
87+
88+
```js
89+
// components 为你的组件库名称
90+
// style 为你的 CSS 文件名
91+
import components from 'components';
92+
import 'components/cjs/style.css';
93+
```
94+
95+
### 按需加载
96+
97+
首先在依赖方的项目中安装以下依赖。
98+
99+
```bash
100+
npm i babel-plugin-component -D
101+
```
102+
103+
然后在 babel 配置文件中加入以下代码,`components` 为你的组件库名称。
104+
105+
```json
106+
{
107+
"plugins": [["component", { "libraryName": "components" }]]
108+
}
109+
```
110+
111+
然后在你的文件中这样导入即可。
112+
113+
```js
114+
// components 为你的组件库名称
115+
import { Button } from 'components';
116+
```
117+
84118
## 关于 ESLint 规范
85119

86120
本项目使用 [eslint-config-team-spec](https://github.com/Yingkaixiang/eslint-config-team-spec) 作为 `ESLint` 规范。此规范是笔者基于 [eslint-config-alloy](https://github.com/AlloyTeam/eslint-config-alloy) 的规范整理出的自己团队的编码规范。如果你不想使用我的规则,可以自行替换为你的规则。

build/webpack.component.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ const webpackBaseConfig = require('./webpack.base');
88
module.exports = merge(webpackBaseConfig, {
99
entry: getEntries(),
1010
output: {
11-
path: resolve("../lib"),
12-
filename: "[name]/index.js",
11+
path: resolve('../lib'),
12+
filename: '[name].js',
1313
libraryExport: 'default',
14-
libraryTarget: "commonjs2",
14+
libraryTarget: 'commonjs2',
1515
},
1616
plugins: [
1717
new MiniCssExtractPlugin({
18-
filename: "[name]/style.css",
18+
filename: '[name]/style.css',
1919
}),
2020
],
21-
});
21+
});

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Button from '../components/button/';
22

3+
// 组件库导入
34
const components = [Button];
45

56
const install = function(Vue) {
@@ -14,3 +15,6 @@ export default {
1415
install,
1516
Button,
1617
};
18+
19+
// 单个组件可以直接这样导出
20+
// export default Button;

0 commit comments

Comments
 (0)