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

File tree

9 files changed

+32
-3187
lines changed

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.

‎articles/chapter-2.md

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

‎articles/chapter-3.md

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

‎articles/chapter-4.md

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

‎articles/summary.md

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

‎package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "happy-ui",
33
"version": "1.0.0",
44
"description": "A React UI Library.",
5-
"typings": "types/index.d.ts",
5+
"typings": "lib/index.d.ts",
66
"main": "lib/index.js",
77
"module": "esm/index.js",
88
"sideEffects": [
@@ -16,8 +16,8 @@
1616
"start": "npm run dev",
1717
"build:doc": "rimraf doc-site && docz build",
1818
"preview:doc": "docz serve",
19-
"clean": "rimraf types lib esm dist",
20-
"build:types": "tsc -p tsconfig.build.json",
19+
"clean": "rimraf lib esm dist",
20+
"build:types": "tsc -p tsconfig.build.json && cpr lib esm",
2121
"build": "npm run clean && npm run build:types && gulp",
2222
"release": "ts-node ./scripts/release.ts",
2323
"commit": "git-cz",
@@ -62,6 +62,7 @@
6262
"@umijs/fabric": "^1.2.12",
6363
"chalk": "^3.0.0",
6464
"commitizen": "^4.0.3",
65+
"cpr": "^3.0.1",
6566
"cz-conventional-changelog": "^3.0.2",
6667
"doctoc": "^1.4.0",
6768
"docz": "^2.3.2-alpha.0",

‎tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": { "emitDeclarationOnly": true },
4-
"exclude": ["**/__tests__/**", "**/demo/**", "types", "node_modules", "lib", "esm"]
4+
"exclude": ["**/__tests__/**", "**/demo/**", "node_modules", "lib", "esm"]
55
}

‎yarn.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4736,6 +4736,16 @@ cosmiconfig@^6.0.0:
47364736
path-type "^4.0.0"
47374737
yaml "^1.7.2"
47384738

4739+
cpr@^3.0.1:
4740+
version "3.0.1"
4741+
resolved "https://registry.npm.taobao.org/cpr/download/cpr-3.0.1.tgz#b9a55038b7cd81a35c17b9761895bd8496aef1e5"
4742+
integrity sha1-uaVQOLfNgaNcF7l2GJW9hJau8eU=
4743+
dependencies:
4744+
graceful-fs "^4.1.5"
4745+
minimist "^1.2.0"
4746+
mkdirp "~0.5.1"
4747+
rimraf "^2.5.4"
4748+
47394749
create-ecdh@^4.0.0:
47404750
version "4.0.3"
47414751
resolved "https://registry.npm.taobao.org/create-ecdh/download/create-ecdh-4.0.3.tgz#c9111b6f33045c4697f144787f9254cdc77c45ff"
@@ -8110,6 +8120,11 @@ graceful-fs@^4.0.0, graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2
81108120
resolved "https://registry.npm.taobao.org/graceful-fs/download/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
81118121
integrity sha1-ShL/G2A3bvCYYsIJPt2Qgyi+hCM=
81128122

8123+
graceful-fs@^4.1.5:
8124+
version "4.2.4"
8125+
resolved "https://registry.npm.taobao.org/graceful-fs/download/graceful-fs-4.2.4.tgz?cache=0&sync_timestamp=1588086876757&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fgraceful-fs%2Fdownload%2Fgraceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb"
8126+
integrity sha1-Ila94U02MpWMRl68ltxGfKB6Kfs=
8127+
81138128
graphql-compose@^6.3.7:
81148129
version "6.3.7"
81158130
resolved "https://registry.npm.taobao.org/graphql-compose/download/graphql-compose-6.3.7.tgz#a252076818260d861bc01d5a8d1fcfc90cda3eff"

0 commit comments

Comments
 (0)
Please sign in to comment.