Skip to content

Commit 2895e61

Browse files
Merge pull request #65 from Toilal/dts
feat(build): generate TypeScript definition file as a bundle
2 parents 7aece11 + 65fe3f4 commit 2895e61

File tree

5 files changed

+131
-10
lines changed

5 files changed

+131
-10
lines changed

build/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"DOM"
1010
],
1111
"moduleResolution": "node",
12-
"resolveJsonModule": true
12+
"resolveJsonModule": true,
13+
"declaration": true
1314
}
1415
}

build/webpack.config.js

+51-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,63 @@
11
const Path = require('path');
2-
const zlib = require("zlib");
2+
const zlib = require('zlib')
3+
const fs = require('fs');
34

45
const Webpack = require('webpack');
56
const TerserPlugin = require('terser-webpack-plugin');
67
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
78
const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
89
const CompressionPlugin = require('compression-webpack-plugin');
10+
const dts = require('dts-bundle');
911

12+
class DtsBundlePlugin {
13+
constructor (options) {
14+
if (!options) {
15+
options = {};
16+
}
17+
this.options = options;
18+
}
19+
20+
_bundle (compiler, options) {
21+
const logger = compiler.getInfrastructureLogger('DtsBundlePlugin');
22+
23+
return () => {
24+
const createSFCModuleNewPath = Path.resolve(this.options.baseDir, 'createSFCModule.d.ts')
25+
const createSFCModuleVue2Path = Path.resolve(this.options.baseDir, 'createVue2SFCModule.d.ts')
26+
const createSFCModuleVue3Path = Path.resolve(this.options.baseDir, 'createVue3SFCModule.d.ts')
27+
28+
if (fs.existsSync(createSFCModuleVue2Path)) {
29+
fs.renameSync(createSFCModuleVue2Path, createSFCModuleNewPath)
30+
} else if (fs.existsSync(createSFCModuleVue3Path)) {
31+
fs.renameSync(createSFCModuleVue3Path, createSFCModuleNewPath)
32+
}
33+
34+
logger.info("Creating dts bundle")
35+
dts.bundle(options);
36+
}
37+
}
38+
39+
apply (compiler) {
40+
const bundle = (compilation) => {
41+
return this._bundle(compiler, this.options);
42+
}
43+
44+
compiler.hooks.afterEmit.tap('DtsBundlePlugin', bundle());
45+
}
46+
}
1047

1148
// doc: https://github.com/Nyalab/caniuse-api#api
1249
const caniuse = require('caniuse-api')
1350

1451
const pkg = require('../package.json');
1552

16-
17-
const distPath = Path.resolve(__dirname, '..', 'dist');
18-
1953
const configure = ({name, vueTarget, libraryTargetModule}) => (env = {}, { mode = 'production', configName }) => {
2054
if (configName && !configName.includes(name)) {
2155
return {name}
2256
}
2357

58+
const distPath = Path.resolve(__dirname, '..', 'dist');
59+
const distTypesPath = Path.resolve(distPath, 'types', `vue${ vueTarget }${ libraryTargetModule ? '-esm' : ''}`)
60+
2461
const isProd = mode === 'production';
2562

2663
// doc: https://github.com/browserslist/browserslist#full-list
@@ -86,6 +123,14 @@ const configure = ({name, vueTarget, libraryTargetModule}) => (env = {}, { mode
86123
},
87124

88125
plugins: [
126+
...!libraryTargetModule ? [
127+
new DtsBundlePlugin({
128+
name: `vue${ vueTarget }-sfc-loader`,
129+
main:`${distTypesPath}/src/index.d.ts`,
130+
baseDir: `${distTypesPath}/src`,
131+
out: `${distPath}/vue${ vueTarget }-sfc-loader.d.ts`
132+
})] : [],
133+
89134
new Webpack.DefinePlugin({
90135

91136
'process.env.NODE_ENV': JSON.stringify(mode), // see also: https://webpack.js.org/configuration/optimization/#optimizationnodeenv
@@ -211,7 +256,7 @@ ${ pkg.name } v${ pkg.version } for vue${ vueTarget }
211256
'@babel/helper-module-transforms': require.resolve('@babel/helper-module-transforms'),
212257
'@babel/helper-replace-supers': require.resolve('@babel/helper-replace-supers'),
213258
'@babel/helper-simple-access': require.resolve('@babel/helper-simple-access'),
214-
259+
215260
'@vue/shared': require.resolve('@vue/shared'),
216261
'@vue/compiler-sfc': require.resolve('@vue/compiler-sfc'),
217262
'@vue/compiler-dom': require.resolve('@vue/compiler-dom'),
@@ -314,7 +359,7 @@ ${ pkg.name } v${ pkg.version } for vue${ vueTarget }
314359
sourceMap: !noSourceMap,
315360
outDir: distPath,
316361
declaration: true,
317-
declarationDir: Path.resolve(distPath, 'types'),
362+
declarationDir: distTypesPath,
318363
}
319364
}
320365
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"version": "0.7.3",
2323
"browserslist": "> 1%, last 2 versions, Firefox ESR, not dead, not ie 11",
2424
"main": "dist/vue3-sfc-loader.js",
25+
"types": "dist/vue3-sfc-loader.d.ts",
2526
"scripts": {
2627
"coverage": "node test/coverageTest.mjs",
2728
"test": "cd test && yarn run start",
@@ -69,6 +70,7 @@
6970
"compression-webpack-plugin": "^7.1.2",
7071
"core-js": "^3.11.0",
7172
"cross-env": "^7.0.3",
73+
"dts-bundle": "^0.7.3",
7274
"duplicate-package-checker-webpack-plugin": "^3.0.0",
7375
"jest": "^26.6.3",
7476
"lodash-es": "^4.17.21",

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
AbstractPath,
1818
} from './types'
1919

20+
export * from './types'
21+
2022

2123
/**
2224
* the version of the library (process.env.VERSION is set by webpack, at compile-time)

yarn.lock

+74-3
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,11 @@
12261226
dependencies:
12271227
"@babel/types" "^7.3.0"
12281228

1229+
1230+
version "0.1.30"
1231+
resolved "https://registry.yarnpkg.com/@types/detect-indent/-/detect-indent-0.1.30.tgz#dc682bb412b4e65ba098e70edad73b4833fb910d"
1232+
integrity sha1-3GgrtBK05lugmOcO2tc7SDP7kQ0=
1233+
12291234
"@types/eslint-scope@^3.7.0":
12301235
version "3.7.0"
12311236
resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.0.tgz#4792816e31119ebd506902a482caec4951fabd86"
@@ -1247,6 +1252,14 @@
12471252
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.47.tgz#d7a51db20f0650efec24cd04994f523d93172ed4"
12481253
integrity sha512-c5ciR06jK8u9BstrmJyO97m+klJrrhCf9u3rLu3DEAJBirxRqSCvDQoYKmxuYwQI5SZChAWu+tq9oVlGRuzPAg==
12491254

1255+
1256+
version "5.0.30"
1257+
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-5.0.30.tgz#1026409c5625a8689074602808d082b2867b8a51"
1258+
integrity sha1-ECZAnFYlqGiQdGAoCNCCsoZ7ilE=
1259+
dependencies:
1260+
"@types/minimatch" "*"
1261+
"@types/node" "*"
1262+
12501263
"@types/graceful-fs@^4.1.2":
12511264
version "4.1.5"
12521265
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
@@ -1278,16 +1291,31 @@
12781291
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
12791292
integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA==
12801293

1294+
"@types/minimatch@*":
1295+
version "3.0.4"
1296+
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.4.tgz#f0ec25dbf2f0e4b18647313ac031134ca5b24b21"
1297+
integrity sha512-1z8k4wzFnNjVK/tlxvrWuK5WMt6mydWWP7+zvH5eFep4oj+UkrfiJTRtjCeBXNpwaA/FYqqtb4/QS4ianFpIRA==
1298+
12811299
"@types/minimist@^1.2.0":
12821300
version "1.2.1"
12831301
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.1.tgz#283f669ff76d7b8260df8ab7a4262cc83d988256"
12841302
integrity sha512-fZQQafSREFyuZcdWFAExYjBiCL7AUCdgsk80iO0q4yihYYdcIiH28CcuPTGFgLOCC8RlW49GSQxdHwZP+I7CNg==
12851303

1304+
1305+
version "0.3.29"
1306+
resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-0.3.29.tgz#7f2ad7ec55f914482fc9b1ec4bb1ae6028d46066"
1307+
integrity sha1-fyrX7FX5FEgvybHsS7GuYCjUYGY=
1308+
12861309
"@types/node@*":
12871310
version "14.14.37"
12881311
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.37.tgz#a3dd8da4eb84a996c36e331df98d82abd76b516e"
12891312
integrity sha512-XYmBiy+ohOR4Lh5jE379fV2IU+6Jn4g5qASinhitfyO71b/sCo6MKsMLF5tc7Zf2CE8hViVQyYSobJNke8OvUw==
12901313

1314+
1315+
version "8.0.0"
1316+
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.0.tgz#acaa89247afddc7967e9902fd11761dadea1a555"
1317+
integrity sha512-j2tekvJCO7j22cs+LO6i0kRPhmQ9MXaPZ55TzOc1lzkN5b6BWqq4AFjl04s1oRRQ1v5rSe+KEvnLUSTonuls/A==
1318+
12911319
"@types/normalize-package-data@^2.4.0":
12921320
version "2.4.0"
12931321
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
@@ -2358,7 +2386,7 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
23582386
dependencies:
23592387
delayed-stream "~1.0.0"
23602388

2361-
commander@^2.20.0:
2389+
commander@^2.20.0, commander@^2.9.0:
23622390
version "2.20.3"
23632391
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
23642392
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
@@ -2805,6 +2833,14 @@ delayed-stream@~1.0.0:
28052833
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
28062834
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=
28072835

2836+
detect-indent@^0.2.0:
2837+
version "0.2.0"
2838+
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-0.2.0.tgz#042914498979ac2d9f3c73e4ff3e6877d3bc92b6"
2839+
integrity sha1-BCkUSYl5rC2fPHPk/z5od9O8krY=
2840+
dependencies:
2841+
get-stdin "^0.1.0"
2842+
minimist "^0.1.0"
2843+
28082844
detect-indent@^6.0.0:
28092845
version "6.0.0"
28102846
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-6.0.0.tgz#0abd0f549f69fc6659a254fe96786186b6f528fd"
@@ -2847,6 +2883,20 @@ dotgitignore@^2.1.0:
28472883
find-up "^3.0.0"
28482884
minimatch "^3.0.4"
28492885

2886+
dts-bundle@^0.7.3:
2887+
version "0.7.3"
2888+
resolved "https://registry.yarnpkg.com/dts-bundle/-/dts-bundle-0.7.3.tgz#372b7bb69c820782e6382f400739a69dced3d59a"
2889+
integrity sha1-Nyt7tpyCB4LmOC9ABzmmnc7T1Zo=
2890+
dependencies:
2891+
"@types/detect-indent" "0.1.30"
2892+
"@types/glob" "5.0.30"
2893+
"@types/mkdirp" "0.3.29"
2894+
"@types/node" "8.0.0"
2895+
commander "^2.9.0"
2896+
detect-indent "^0.2.0"
2897+
glob "^6.0.4"
2898+
mkdirp "^0.5.0"
2899+
28502900
duplexer@^0.1.2:
28512901
version "0.1.2"
28522902
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6"
@@ -3425,6 +3475,11 @@ get-pkg-repo@^1.0.0:
34253475
parse-github-repo-url "^1.3.0"
34263476
through2 "^2.0.0"
34273477

3478+
get-stdin@^0.1.0:
3479+
version "0.1.0"
3480+
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-0.1.0.tgz#5998af24aafc802d15c82c685657eeb8b10d4a91"
3481+
integrity sha1-WZivJKr8gC0VyCxoVlfuuLENSpE=
3482+
34283483
get-stdin@^4.0.1:
34293484
version "4.0.1"
34303485
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-4.0.1.tgz#b968c6b0a04384324902e8bf1a5df32579a450fe"
@@ -3500,6 +3555,17 @@ glob-to-regexp@^0.4.1:
35003555
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e"
35013556
integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==
35023557

3558+
glob@^6.0.4:
3559+
version "6.0.4"
3560+
resolved "https://registry.yarnpkg.com/glob/-/glob-6.0.4.tgz#0f08860f6a155127b2fadd4f9ce24b1aab6e4d22"
3561+
integrity sha1-DwiGD2oVUSey+t1PnOJLGqtuTSI=
3562+
dependencies:
3563+
inflight "^1.0.4"
3564+
inherits "2"
3565+
minimatch "2 || 3"
3566+
once "^1.3.0"
3567+
path-is-absolute "^1.0.0"
3568+
35033569
glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
35043570
version "7.1.6"
35053571
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
@@ -4966,7 +5032,7 @@ min-indent@^1.0.0:
49665032
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
49675033
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==
49685034

4969-
minimatch@^3.0.0, minimatch@^3.0.4:
5035+
"minimatch@2 || 3", minimatch@^3.0.0, minimatch@^3.0.4:
49705036
version "3.0.4"
49715037
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
49725038
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
@@ -4982,6 +5048,11 @@ [email protected]:
49825048
is-plain-obj "^1.1.0"
49835049
kind-of "^6.0.3"
49845050

5051+
minimist@^0.1.0:
5052+
version "0.1.0"
5053+
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.1.0.tgz#99df657a52574c21c9057497df742790b2b4c0de"
5054+
integrity sha1-md9lelJXTCHJBXSX33QnkLK0wN4=
5055+
49855056
minimist@^1.1.1, minimist@^1.1.3, minimist@^1.2.0, minimist@^1.2.5:
49865057
version "1.2.5"
49875058
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
@@ -5000,7 +5071,7 @@ mkdirp-classic@^0.5.2:
50005071
resolved "https://registry.yarnpkg.com/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz#fa10c9115cc6d8865be221ba47ee9bed78601113"
50015072
integrity sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==
50025073

5003-
mkdirp@^0.5.1:
5074+
mkdirp@^0.5.0, mkdirp@^0.5.1:
50045075
version "0.5.5"
50055076
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.5.tgz#d91cefd62d1436ca0f41620e251288d420099def"
50065077
integrity sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==

0 commit comments

Comments
 (0)