Skip to content

Commit 86a5c16

Browse files
authored
chore: bundle typing into single d.ts file (#368)
1 parent ccf0905 commit 86a5c16

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ lib
88
.idea
99
.rpt2_cache
1010
TODO.md
11+
typings

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"scripts": {
2828
"start": "cross-env TARGET=es rollup -c -w",
29-
"build": "rollup -c",
29+
"build": "rimraf dist typings && tsc --emitDeclarationOnly && rollup -c",
3030
"lint": "prettier --write --parser typescript \"{src,test,test-dts}/*.ts?(x)\" && prettier --write \"{src,test}/*.js\"",
3131
"test": "cross-env NODE_ENV=test jest",
3232
"test-dts": "tsc -p ./test-dts/tsconfig.json && npm run build && tsc -p ./test-dts/tsconfig.build.json",
@@ -46,7 +46,9 @@
4646
"jest": "^26.0.1",
4747
"lint-staged": "^10.2.7",
4848
"prettier": "^2.0.5",
49+
"rimraf": "^3.0.2",
4950
"rollup": "^2.12.0",
51+
"rollup-plugin-dts": "^1.4.7",
5052
"rollup-plugin-terser": "^6.1.0",
5153
"rollup-plugin-typescript2": "^0.27.1",
5254
"ts-jest": "^26.1.0",

Diff for: rollup.config.js

+41-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as path from 'path';
2-
// import filesize from 'rollup-plugin-filesize';
3-
import typescript from 'rollup-plugin-typescript2';
4-
import resolve from '@rollup/plugin-node-resolve';
5-
import { terser } from 'rollup-plugin-terser';
6-
import replace from '@rollup/plugin-replace';
1+
import * as path from 'path'
2+
import typescript from 'rollup-plugin-typescript2'
3+
import resolve from '@rollup/plugin-node-resolve'
4+
import { terser } from 'rollup-plugin-terser'
5+
import replace from '@rollup/plugin-replace'
6+
import dts from 'rollup-plugin-dts'
77

88
const builds = {
99
'cjs-dev': {
@@ -31,14 +31,20 @@ const builds = {
3131
format: 'es',
3232
mode: 'development',
3333
},
34-
};
34+
}
35+
36+
function onwarn(msg, warn) {
37+
if (!/Circular/.test(msg)) {
38+
warn(msg)
39+
}
40+
}
3541

3642
function getAllBuilds() {
37-
return Object.keys(builds).map((key) => genConfig(builds[key]));
43+
return Object.keys(builds).map((key) => genConfig(builds[key]))
3844
}
3945

4046
function genConfig({ outFile, format, mode }) {
41-
const isProd = mode === 'production';
47+
const isProd = mode === 'production'
4248
return {
4349
input: './src/index.ts',
4450
output: {
@@ -51,18 +57,21 @@ function genConfig({ outFile, format, mode }) {
5157
name: format === 'umd' ? 'vueCompositionApi' : undefined,
5258
},
5359
external: ['vue'],
54-
onwarn: (msg, warn) => {
55-
if (!/Circular/.test(msg)) {
56-
warn(msg);
57-
}
58-
},
60+
onwarn,
5961
plugins: [
6062
typescript({
61-
typescript: require('typescript'),
63+
tsconfigOverride: {
64+
declaration: false,
65+
declarationDir: null,
66+
emitDeclarationOnly: false,
67+
},
68+
useTsconfigDeclarationDir: true,
6269
}),
6370
resolve(),
6471
replace({
65-
'process.env.NODE_ENV': JSON.stringify(isProd ? 'production' : 'development'),
72+
'process.env.NODE_ENV': JSON.stringify(
73+
isProd ? 'production' : 'development'
74+
),
6675
__DEV__:
6776
format === 'es'
6877
? // preserve to be handled by bundlers
@@ -72,15 +81,26 @@ function genConfig({ outFile, format, mode }) {
7281
}),
7382
isProd && terser(),
7483
].filter(Boolean),
75-
};
84+
}
7685
}
7786

78-
let buildConfig;
87+
let buildConfig
7988

8089
if (process.env.TARGET) {
81-
buildConfig = genConfig(builds[process.env.TARGET]);
90+
buildConfig = genConfig(builds[process.env.TARGET])
8291
} else {
83-
buildConfig = getAllBuilds();
92+
buildConfig = getAllBuilds()
8493
}
8594

86-
export default buildConfig;
95+
// bundle typings
96+
buildConfig.push({
97+
input: 'typings/index.d.ts',
98+
output: {
99+
file: 'dist/index.d.ts',
100+
format: 'es',
101+
},
102+
onwarn,
103+
plugins: [dts()],
104+
})
105+
106+
export default buildConfig

Diff for: tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"target": "es5",
44
"lib": ["dom", "esnext"],
55
"declaration": true,
6+
"declarationDir": "./typings",
67
"module": "esnext",
78
"moduleResolution": "node",
89
"skipLibCheck": true,
@@ -19,5 +20,6 @@
1920
"downlevelIteration": true,
2021
"noUnusedLocals": true
2122
},
22-
"include": ["src"]
23+
"include": ["src"],
24+
"exclude": ["typings"]
2325
}

Diff for: yarn.lock

+8-1
Original file line numberDiff line numberDiff line change
@@ -3406,13 +3406,20 @@ ret@~0.1.10:
34063406
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
34073407
integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==
34083408

3409-
rimraf@^3.0.0:
3409+
rimraf@^3.0.0, rimraf@^3.0.2:
34103410
version "3.0.2"
34113411
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"
34123412
integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==
34133413
dependencies:
34143414
glob "^7.1.3"
34153415

3416+
rollup-plugin-dts@^1.4.7:
3417+
version "1.4.7"
3418+
resolved "https://registry.yarnpkg.com/rollup-plugin-dts/-/rollup-plugin-dts-1.4.7.tgz#6255147ac777314c0725a1efcb42df10fe282243"
3419+
integrity sha512-QkunbJ96yUNkW95k/Vd6SdTjCbWSG0rMVUtpHSCwfg078Z7vbDaBnfz/gkSqR5h8WFMxoccBT4aodHm6387Jvg==
3420+
optionalDependencies:
3421+
"@babel/code-frame" "^7.8.3"
3422+
34163423
rollup-plugin-terser@^6.1.0:
34173424
version "6.1.0"
34183425
resolved "https://registry.yarnpkg.com/rollup-plugin-terser/-/rollup-plugin-terser-6.1.0.tgz#071866585aea104bfbb9dd1019ac523e63c81e45"

0 commit comments

Comments
 (0)