Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 20b0f6a

Browse files
author
zhaozhiwen
committedMay 15, 2020
fix(types): 生成两份声明文件[lib/esm]
1 parent 407bdce commit 20b0f6a

9 files changed

+32
-3187
lines changed
 

‎README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
22
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3-
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
3+
4+
**Table of Contents** _generated with [DocToc](https://github.com/thlorenz/doctoc)_
45

56
- [概览](#%E6%A6%82%E8%A7%88)
67
- [准备工作](#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C)
@@ -727,24 +728,28 @@ exports.onCreateWebpackConfig = args => {
727728

728729
```json
729730
{
730-
"typings": "types/index.d.ts", // 定义类型入口文件
731+
"typings": "lib/index.d.ts", // 定义类型入口文件
731732
"scripts": {
732-
"build:types": "tsc -p tsconfig.build.json" // 执行tsc命令生成类型声明文件
733+
"build:types": "tsc -p tsconfig.build.json && cpr lib esm" // 执行tsc命令生成类型声明文件
733734
}
734735
}
735736
```
736737

738+
> 值得注意的是:此处使用`cpr`(需要手动安装)将`lib`的声明文件拷贝了一份,并将文件夹重命名为`esm`,用于后面存放 ES module 形式的组件。这样做的原因是保证用户手动按需引入组件时依旧可以获取自动提示。
739+
740+
> 最开始的方式是将声明文件单独存放在`types`文件夹,但这样只有通过'happy-ui'引入才可以获取提示,而'happy-ui/esm/xxx'和'happy-ui/lib/xxx'就无法获取提示。
741+
737742
**tsconfig.build.json**
738743

739744
```json
740745
{
741746
"extends": "./tsconfig.json",
742747
"compilerOptions": { "emitDeclarationOnly": true }, // 只生成声明文件
743-
"exclude": ["**/__tests__/**", "**/demo/**", "node_modules", "lib", "esm", "types"] // 排除示例、测试以及打包好的文件夹
748+
"exclude": ["**/__tests__/**", "**/demo/**", "node_modules", "lib", "esm"] // 排除示例、测试以及打包好的文件夹
744749
}
745750
```
746751

747-
执行`yarn build:types`,可以发现根目录下已经生成了`lib`文件夹(`tsconfig.json`中定义的`outDir`字段),目录结构与`components`文件夹保持一致,如下:
752+
执行`yarn build:types`,可以发现根目录下已经生成了`lib`文件夹(`tsconfig.json`中定义的`declarationDir`字段),目录结构与`components`文件夹保持一致,如下:
748753

749754
**types**
750755

@@ -900,7 +905,7 @@ exports.default = build;
900905
+ "main": "lib/index.js",
901906
"scripts": {
902907
...
903-
+ "clean": "rimraf types lib esm dist",
908+
+ "clean": "rimraf lib esm dist",
904909
+ "build": "npm run clean && npm run build:types && gulp",
905910
...
906911
},
@@ -1038,7 +1043,7 @@ const build = gulp.parallel(buildScripts);
10381043
// ...
10391044
```
10401045

1041-
执行`yarn build`,可以发现生成了`types`/`lib`/`esm`三个文件夹,观察`esm`目录,结构同`lib`/`types`一致,js 文件都是以`ES module`模块形式导入导出。
1046+
执行`yarn build`,可以发现生成了`lib`/`esm`三个文件夹,观察`esm`目录,结构同`lib`一致,js 文件都是以`ES module`模块形式导入导出。
10421047

10431048
**esm/alert/alert.js**
10441049

‎articles/chapter-1.md

Lines changed: 0 additions & 295 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.