Skip to content

Commit

Permalink
feat(build): generate TypeScript definition file as a bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed May 10, 2021
1 parent 7aece11 commit 65fe3f4
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 10 deletions.
3 changes: 2 additions & 1 deletion build/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"DOM"
],
"moduleResolution": "node",
"resolveJsonModule": true
"resolveJsonModule": true,
"declaration": true
}
}
57 changes: 51 additions & 6 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,63 @@
const Path = require('path');
const zlib = require("zlib");
const zlib = require('zlib')
const fs = require('fs');

const Webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const dts = require('dts-bundle');

class DtsBundlePlugin {
constructor (options) {
if (!options) {
options = {};
}
this.options = options;
}

_bundle (compiler, options) {
const logger = compiler.getInfrastructureLogger('DtsBundlePlugin');

return () => {
const createSFCModuleNewPath = Path.resolve(this.options.baseDir, 'createSFCModule.d.ts')
const createSFCModuleVue2Path = Path.resolve(this.options.baseDir, 'createVue2SFCModule.d.ts')
const createSFCModuleVue3Path = Path.resolve(this.options.baseDir, 'createVue3SFCModule.d.ts')

if (fs.existsSync(createSFCModuleVue2Path)) {
fs.renameSync(createSFCModuleVue2Path, createSFCModuleNewPath)
} else if (fs.existsSync(createSFCModuleVue3Path)) {
fs.renameSync(createSFCModuleVue3Path, createSFCModuleNewPath)
}

logger.info("Creating dts bundle")
dts.bundle(options);
}
}

apply (compiler) {
const bundle = (compilation) => {
return this._bundle(compiler, this.options);
}

compiler.hooks.afterEmit.tap('DtsBundlePlugin', bundle());
}
}

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

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


const distPath = Path.resolve(__dirname, '..', 'dist');

const configure = ({name, vueTarget, libraryTargetModule}) => (env = {}, { mode = 'production', configName }) => {
if (configName && !configName.includes(name)) {
return {name}
}

const distPath = Path.resolve(__dirname, '..', 'dist');
const distTypesPath = Path.resolve(distPath, 'types', `vue${ vueTarget }${ libraryTargetModule ? '-esm' : ''}`)

const isProd = mode === 'production';

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

plugins: [
...!libraryTargetModule ? [
new DtsBundlePlugin({
name: `vue${ vueTarget }-sfc-loader`,
main:`${distTypesPath}/src/index.d.ts`,
baseDir: `${distTypesPath}/src`,
out: `${distPath}/vue${ vueTarget }-sfc-loader.d.ts`
})] : [],

new Webpack.DefinePlugin({

'process.env.NODE_ENV': JSON.stringify(mode), // see also: https://webpack.js.org/configuration/optimization/#optimizationnodeenv
Expand Down Expand Up @@ -211,7 +256,7 @@ ${ pkg.name } v${ pkg.version } for vue${ vueTarget }
'@babel/helper-module-transforms': require.resolve('@babel/helper-module-transforms'),
'@babel/helper-replace-supers': require.resolve('@babel/helper-replace-supers'),
'@babel/helper-simple-access': require.resolve('@babel/helper-simple-access'),

'@vue/shared': require.resolve('@vue/shared'),
'@vue/compiler-sfc': require.resolve('@vue/compiler-sfc'),
'@vue/compiler-dom': require.resolve('@vue/compiler-dom'),
Expand Down Expand Up @@ -314,7 +359,7 @@ ${ pkg.name } v${ pkg.version } for vue${ vueTarget }
sourceMap: !noSourceMap,
outDir: distPath,
declaration: true,
declarationDir: Path.resolve(distPath, 'types'),
declarationDir: distTypesPath,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"version": "0.7.3",
"browserslist": "> 1%, last 2 versions, Firefox ESR, not dead, not ie 11",
"main": "dist/vue3-sfc-loader.js",
"types": "dist/vue3-sfc-loader.d.ts",
"scripts": {
"coverage": "node test/coverageTest.mjs",
"test": "cd test && yarn run start",
Expand Down Expand Up @@ -69,6 +70,7 @@
"compression-webpack-plugin": "^7.1.2",
"core-js": "^3.11.0",
"cross-env": "^7.0.3",
"dts-bundle": "^0.7.3",
"duplicate-package-checker-webpack-plugin": "^3.0.0",
"jest": "^26.6.3",
"lodash-es": "^4.17.21",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
AbstractPath,
} from './types'

export * from './types'


/**
* the version of the library (process.env.VERSION is set by webpack, at compile-time)
Expand Down
77 changes: 74 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,11 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/detect-indent@0.1.30":
version "0.1.30"
resolved "https://registry.yarnpkg.com/@types/detect-indent/-/detect-indent-0.1.30.tgz#dc682bb412b4e65ba098e70edad73b4833fb910d"
integrity sha1-3GgrtBK05lugmOcO2tc7SDP7kQ0=

"@types/eslint-scope@^3.7.0":
version "3.7.0"
resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.0.tgz#4792816e31119ebd506902a482caec4951fabd86"
Expand All @@ -1247,6 +1252,14 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.47.tgz#d7a51db20f0650efec24cd04994f523d93172ed4"
integrity sha512-c5ciR06jK8u9BstrmJyO97m+klJrrhCf9u3rLu3DEAJBirxRqSCvDQoYKmxuYwQI5SZChAWu+tq9oVlGRuzPAg==

"@types/glob@5.0.30":
version "5.0.30"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-5.0.30.tgz#1026409c5625a8689074602808d082b2867b8a51"
integrity sha1-ECZAnFYlqGiQdGAoCNCCsoZ7ilE=
dependencies:
"@types/minimatch" "*"
"@types/node" "*"

"@types/graceful-fs@^4.1.2":
version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
Expand Down Expand Up @@ -1278,16 +1291,31 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA==

"@types/minimatch@*":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.4.tgz#f0ec25dbf2f0e4b18647313ac031134ca5b24b21"
integrity sha512-1z8k4wzFnNjVK/tlxvrWuK5WMt6mydWWP7+zvH5eFep4oj+UkrfiJTRtjCeBXNpwaA/FYqqtb4/QS4ianFpIRA==

"@types/minimist@^1.2.0":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.1.tgz#283f669ff76d7b8260df8ab7a4262cc83d988256"
integrity sha512-fZQQafSREFyuZcdWFAExYjBiCL7AUCdgsk80iO0q4yihYYdcIiH28CcuPTGFgLOCC8RlW49GSQxdHwZP+I7CNg==

"@types/mkdirp@0.3.29":
version "0.3.29"
resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-0.3.29.tgz#7f2ad7ec55f914482fc9b1ec4bb1ae6028d46066"
integrity sha1-fyrX7FX5FEgvybHsS7GuYCjUYGY=

"@types/node@*":
version "14.14.37"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.37.tgz#a3dd8da4eb84a996c36e331df98d82abd76b516e"
integrity sha512-XYmBiy+ohOR4Lh5jE379fV2IU+6Jn4g5qASinhitfyO71b/sCo6MKsMLF5tc7Zf2CE8hViVQyYSobJNke8OvUw==

"@types/node@8.0.0":
version "8.0.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.0.tgz#acaa89247afddc7967e9902fd11761dadea1a555"
integrity sha512-j2tekvJCO7j22cs+LO6i0kRPhmQ9MXaPZ55TzOc1lzkN5b6BWqq4AFjl04s1oRRQ1v5rSe+KEvnLUSTonuls/A==

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
Expand Down Expand Up @@ -2358,7 +2386,7 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
dependencies:
delayed-stream "~1.0.0"

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

detect-indent@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-0.2.0.tgz#042914498979ac2d9f3c73e4ff3e6877d3bc92b6"
integrity sha1-BCkUSYl5rC2fPHPk/z5od9O8krY=
dependencies:
get-stdin "^0.1.0"
minimist "^0.1.0"

detect-indent@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-6.0.0.tgz#0abd0f549f69fc6659a254fe96786186b6f528fd"
Expand Down Expand Up @@ -2847,6 +2883,20 @@ dotgitignore@^2.1.0:
find-up "^3.0.0"
minimatch "^3.0.4"

dts-bundle@^0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/dts-bundle/-/dts-bundle-0.7.3.tgz#372b7bb69c820782e6382f400739a69dced3d59a"
integrity sha1-Nyt7tpyCB4LmOC9ABzmmnc7T1Zo=
dependencies:
"@types/detect-indent" "0.1.30"
"@types/glob" "5.0.30"
"@types/mkdirp" "0.3.29"
"@types/node" "8.0.0"
commander "^2.9.0"
detect-indent "^0.2.0"
glob "^6.0.4"
mkdirp "^0.5.0"

duplexer@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6"
Expand Down Expand Up @@ -3425,6 +3475,11 @@ get-pkg-repo@^1.0.0:
parse-github-repo-url "^1.3.0"
through2 "^2.0.0"

get-stdin@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-0.1.0.tgz#5998af24aafc802d15c82c685657eeb8b10d4a91"
integrity sha1-WZivJKr8gC0VyCxoVlfuuLENSpE=

get-stdin@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-4.0.1.tgz#b968c6b0a04384324902e8bf1a5df32579a450fe"
Expand Down Expand Up @@ -3500,6 +3555,17 @@ glob-to-regexp@^0.4.1:
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e"
integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==

glob@^6.0.4:
version "6.0.4"
resolved "https://registry.yarnpkg.com/glob/-/glob-6.0.4.tgz#0f08860f6a155127b2fadd4f9ce24b1aab6e4d22"
integrity sha1-DwiGD2oVUSey+t1PnOJLGqtuTSI=
dependencies:
inflight "^1.0.4"
inherits "2"
minimatch "2 || 3"
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
version "7.1.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
Expand Down Expand Up @@ -4966,7 +5032,7 @@ min-indent@^1.0.0:
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==

minimatch@^3.0.0, minimatch@^3.0.4:
"minimatch@2 || 3", minimatch@^3.0.0, minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
Expand All @@ -4982,6 +5048,11 @@ minimist-options@4.1.0:
is-plain-obj "^1.1.0"
kind-of "^6.0.3"

minimist@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.1.0.tgz#99df657a52574c21c9057497df742790b2b4c0de"
integrity sha1-md9lelJXTCHJBXSX33QnkLK0wN4=

minimist@^1.1.1, minimist@^1.1.3, minimist@^1.2.0, minimist@^1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
Expand All @@ -5000,7 +5071,7 @@ mkdirp-classic@^0.5.2:
resolved "https://registry.yarnpkg.com/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz#fa10c9115cc6d8865be221ba47ee9bed78601113"
integrity sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==

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

0 comments on commit 65fe3f4

Please sign in to comment.