From ad36d7b89da0bcfd67c68772c19a5e4e5b501cd7 Mon Sep 17 00:00:00 2001 From: zhong666 Date: Thu, 25 Apr 2024 19:31:13 +0800 Subject: [PATCH] feat: support vue-tsc v2 (#201) Co-authored-by: Daniel Roe --- package.json | 14 ++- pnpm-lock.yaml | 231 ++++++++++++++++++++++++++++--------------- src/utils/vue-dts.ts | 121 +++++++++++++++++++---- test/index.test.ts | 130 +++++++++++++++++++++++- 4 files changed, 389 insertions(+), 107 deletions(-) diff --git a/package.json b/package.json index d71f137..5832bf7 100644 --- a/package.json +++ b/package.json @@ -40,14 +40,19 @@ "mlly": "^1.6.1", "mri": "^1.2.0", "pathe": "^1.1.2", + "pkg-types": "^1.1.0", "postcss": "^8.4.38", - "postcss-nested": "^6.0.1" + "postcss-nested": "^6.0.1", + "semver": "^7.6.0" }, "devDependencies": { "@types/fs-extra": "^11.0.4", "@types/mri": "^1.2.0", "@types/node": "^20.12.7", + "@types/semver": "^7.5.8", "@vitest/coverage-v8": "^1.5.2", + "@volar/typescript": "^2.2.0-alpha.10", + "@vue/language-core": "^2.0.14", "c8": "latest", "changelogen": "^0.5.5", "eslint": "^8.57.0", @@ -57,14 +62,15 @@ "sass": "^1.75.0", "typescript": "^5.4.5", "unbuild": "^2.0.0", + "vitest": "^1.5.2", "vue": "^3.4.25", - "vue-tsc": "^1.8.27", - "vitest": "^1.5.2" + "vue-tsc": "^2.0.14", + "vue-tsc1": "npm:vue-tsc@^1.8.27" }, "peerDependencies": { "sass": "^1.75.0", "typescript": ">=5.4.5", - "vue-tsc": "^1.8.27" + "vue-tsc": "^1.8.27 || ^2.0.14" }, "peerDependenciesMeta": { "sass": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d4c8080..7b75b5c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,12 +41,18 @@ importers: pathe: specifier: ^1.1.2 version: 1.1.2 + pkg-types: + specifier: ^1.1.0 + version: 1.1.0 postcss: specifier: ^8.4.38 version: 8.4.38 postcss-nested: specifier: ^6.0.1 version: 6.0.1(postcss@8.4.38) + semver: + specifier: ^7.6.0 + version: 7.6.0 devDependencies: '@types/fs-extra': specifier: ^11.0.4 @@ -57,9 +63,18 @@ importers: '@types/node': specifier: ^20.12.7 version: 20.12.7 + '@types/semver': + specifier: ^7.5.8 + version: 7.5.8 '@vitest/coverage-v8': specifier: ^1.5.2 - version: 1.5.2(vitest@1.5.2) + version: 1.5.2(vitest@1.5.2(@types/node@20.12.7)(sass@1.75.0)) + '@volar/typescript': + specifier: ^2.2.0-alpha.10 + version: 2.2.0-alpha.10 + '@vue/language-core': + specifier: ^2.0.14 + version: 2.0.14(typescript@5.4.5) c8: specifier: latest version: 9.1.0 @@ -94,8 +109,11 @@ importers: specifier: ^3.4.25 version: 3.4.25(typescript@5.4.5) vue-tsc: - specifier: ^1.8.27 - version: 1.8.27(typescript@5.4.5) + specifier: ^2.0.14 + version: 2.0.14(typescript@5.4.5) + vue-tsc1: + specifier: npm:vue-tsc@^1.8.27 + version: vue-tsc@1.8.27(typescript@5.4.5) packages: @@ -713,8 +731,8 @@ packages: '@types/resolve@1.20.2': resolution: {integrity: sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==} - '@types/semver@7.5.6': - resolution: {integrity: sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A==} + '@types/semver@7.5.8': + resolution: {integrity: sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ==} '@typescript-eslint/eslint-plugin@5.62.0': resolution: {integrity: sha512-TiZzBSJja/LbhNPvk6yc0JrX9XqhQ0hdh6M2svYfsHGejaKFIAGd9MQ+ERIMzLGlN/kZoYIgdxFV0PuljTKXag==} @@ -800,21 +818,24 @@ packages: '@volar/language-core@1.11.1': resolution: {integrity: sha512-dOcNn3i9GgZAcJt43wuaEykSluAuOkQgzni1cuxLxTV0nJKanQztp7FxyswdRILaKH+P2XZMPRp2S4MV/pElCw==} + '@volar/language-core@2.2.0-alpha.10': + resolution: {integrity: sha512-njVJLtpu0zMvDaEk7K5q4BRpOgbyEUljU++un9TfJoJNhxG0z/hWwpwgTRImO42EKvwIxF3XUzeMk+qatAFy7Q==} + '@volar/source-map@1.11.1': resolution: {integrity: sha512-hJnOnwZ4+WT5iupLRnuzbULZ42L7BWWPMmruzwtLhJfpDVoZLjNBxHDi2sY2bgZXCKlpU5XcsMFoYrsQmPhfZg==} + '@volar/source-map@2.2.0-alpha.10': + resolution: {integrity: sha512-nrdWApVkP5cksAnDEyy1JD9rKdwOJsEq1B+seWO4vNXmZNcxQQCx4DULLBvKt7AzRUAQiAuw5aQkb9RBaSqdVA==} + '@volar/typescript@1.11.1': resolution: {integrity: sha512-iU+t2mas/4lYierSnoFOeRFQUhAEMgsFuQxoxvwn5EdQopw43j+J27a4lt9LMInx1gLJBC6qL14WYGlgymaSMQ==} - '@vue/compiler-core@3.4.21': - resolution: {integrity: sha512-MjXawxZf2SbZszLPYxaFCjxfibYrzr3eYbKxwpLR9EQN+oaziSu3qKVbwBERj1IFIB8OLUewxB5m/BFzi613og==} + '@volar/typescript@2.2.0-alpha.10': + resolution: {integrity: sha512-GCa0vTVVdA9ULUsu2Rx7jwsIuyZQPvPVT9o3NrANTbYv+523Ao1gv3glC5vzNSDPM6bUl37r94HbCj7KINQr+g==} '@vue/compiler-core@3.4.25': resolution: {integrity: sha512-Y2pLLopaElgWnMNolgG8w3C5nNUVev80L7hdQ5iIKPtMJvhVpG0zhnBG/g3UajJmZdvW0fktyZTotEHD1Srhbg==} - '@vue/compiler-dom@3.4.21': - resolution: {integrity: sha512-IZC6FKowtT1sl0CR5DpXSiEB5ayw75oT2bma1BEhV7RRR1+cfwLrxc2Z8Zq/RGFzJ8w5r9QtCOvTjQgdn0IKmA==} - '@vue/compiler-dom@3.4.25': resolution: {integrity: sha512-Ugz5DusW57+HjllAugLci19NsDK+VyjGvmbB2TXaTcSlQxwL++2PETHx/+Qv6qFwNLzSt7HKepPe4DcTE3pBWg==} @@ -832,6 +853,14 @@ packages: typescript: optional: true + '@vue/language-core@2.0.14': + resolution: {integrity: sha512-3q8mHSNcGTR7sfp2X6jZdcb4yt8AjBXAfKk0qkZIh7GAJxOnoZ10h5HToZglw4ToFvAnq+xu/Z2FFbglh9Icag==} + peerDependencies: + typescript: '*' + peerDependenciesMeta: + typescript: + optional: true + '@vue/reactivity@3.4.25': resolution: {integrity: sha512-mKbEtKr1iTxZkAG3vm3BtKHAOhuI4zzsVcN0epDldU/THsrvfXRKzq+lZnjczZGnTdh3ojd86/WrP+u9M51pWQ==} @@ -846,9 +875,6 @@ packages: peerDependencies: vue: 3.4.25 - '@vue/shared@3.4.21': - resolution: {integrity: sha512-PuJe7vDIi6VYSinuEbUIQgMIRZGgM8e4R+G+/dQTk0X1NEdvgvvgv7m+rfmDH1gZzyA1OjjoWskvHlfRNfQf3g==} - '@vue/shared@3.4.25': resolution: {integrity: sha512-k0yappJ77g2+KNrIaF0FFnzwLvUBLUYr8VOwz+/6vLsmItFp51AcxLL7Ey3iPd7BIRyWPOcqUjMnm7OkahXllA==} @@ -1080,6 +1106,9 @@ packages: concat-map@0.0.1: resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} + confbox@0.1.7: + resolution: {integrity: sha512-uJcB/FKZtBMCJpK8MQji6bJHgu1tixKPxRLeGkNzBoOZzpnZUJm0jm2/sBDWcuBx1dYgxV4JU+g5hmNxCyAmdA==} + consola@3.2.3: resolution: {integrity: sha512-I5qxpzLv+sJhTVEoLYNcTW+bThDCPsit0vLNKShZx6rLtpilNpmmeTPaeqJb9ZE9dV3DGaeby6Vuhrw38WjeyQ==} engines: {node: ^14.18.0 || >=16.10.0} @@ -1908,9 +1937,6 @@ packages: engines: {node: '>=6'} hasBin: true - jsonc-parser@3.2.0: - resolution: {integrity: sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==} - jsonfile@6.1.0: resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==} @@ -2072,6 +2098,9 @@ packages: muggle-string@0.3.1: resolution: {integrity: sha512-ckmWDJjphvd/FvZawgygcUeQCxzvohjFO5RxTjj4eq8kw359gFF3E1brjfI+viLMxss5JrHTDRHZvu2/tuy0Qg==} + muggle-string@0.4.1: + resolution: {integrity: sha512-VNTrAak/KhO2i8dqqnqnAHOa3cYBwXEZe9h+D5h/1ZqFSTEFHdM65lR7RoIqq3tBBYavsOXV84NoHXZ0AkPyqQ==} + nanoid@3.3.7: resolution: {integrity: sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==} engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} @@ -2241,8 +2270,8 @@ packages: resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==} engines: {node: '>=8.6'} - pkg-types@1.0.3: - resolution: {integrity: sha512-nN7pYi0AQqJnoLPC9eHFQ8AcyaixBUOwvqc5TDnIKCMEE6I0y8P7OKA7fPexsXGCGxQDl/cmrLAp26LhcwxZ4A==} + pkg-types@1.1.0: + resolution: {integrity: sha512-/RpmvKdxKf8uILTtoOhAgf30wYbP2Qw+L9p3Rvshx1JZVX+XQNZQFjlbmGHEGIm4CkVPlSn+NXmIM8+9oWQaSA==} pluralize@8.0.0: resolution: {integrity: sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==} @@ -2719,8 +2748,8 @@ packages: resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==} hasBin: true - semver@7.5.4: - resolution: {integrity: sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==} + semver@7.6.0: + resolution: {integrity: sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==} engines: {node: '>=10'} hasBin: true @@ -3067,6 +3096,12 @@ packages: peerDependencies: typescript: '*' + vue-tsc@2.0.14: + resolution: {integrity: sha512-DgAO3U1cnCHOUO7yB35LENbkapeRsBZ7Ugq5hGz/QOHny0+1VQN8eSwSBjYbjLVPfvfw6EY7sNPjbuHHUhckcg==} + hasBin: true + peerDependencies: + typescript: '*' + vue@3.4.25: resolution: {integrity: sha512-HWyDqoBHMgav/OKiYA2ZQg+kjfMgLt/T0vg4cbIF7JbXAjDexRf5JRg+PWAfrAkSmTd2I8aPSXtooBFWHB98cg==} peerDependencies: @@ -3486,8 +3521,9 @@ snapshots: '@rollup/plugin-alias@5.1.0(rollup@3.29.4)': dependencies: - rollup: 3.29.4 slash: 4.0.0 + optionalDependencies: + rollup: 3.29.4 '@rollup/plugin-commonjs@25.0.7(rollup@3.29.4)': dependencies: @@ -3497,11 +3533,13 @@ snapshots: glob: 8.1.0 is-reference: 1.2.1 magic-string: 0.30.7 + optionalDependencies: rollup: 3.29.4 '@rollup/plugin-json@6.1.0(rollup@3.29.4)': dependencies: '@rollup/pluginutils': 5.1.0(rollup@3.29.4) + optionalDependencies: rollup: 3.29.4 '@rollup/plugin-node-resolve@15.2.3(rollup@3.29.4)': @@ -3512,12 +3550,14 @@ snapshots: is-builtin-module: 3.2.1 is-module: 1.0.0 resolve: 1.22.8 + optionalDependencies: rollup: 3.29.4 '@rollup/plugin-replace@5.0.5(rollup@3.29.4)': dependencies: '@rollup/pluginutils': 5.1.0(rollup@3.29.4) magic-string: 0.30.7 + optionalDependencies: rollup: 3.29.4 '@rollup/pluginutils@5.1.0(rollup@3.29.4)': @@ -3525,6 +3565,7 @@ snapshots: '@types/estree': 1.0.5 estree-walker: 2.0.2 picomatch: 2.3.1 + optionalDependencies: rollup: 3.29.4 '@rollup/rollup-android-arm-eabi@4.9.6': @@ -3601,9 +3642,9 @@ snapshots: '@types/resolve@1.20.2': {} - '@types/semver@7.5.6': {} + '@types/semver@7.5.8': {} - '@typescript-eslint/eslint-plugin@5.62.0(@typescript-eslint/parser@5.62.0)(eslint@8.57.0)(typescript@5.4.5)': + '@typescript-eslint/eslint-plugin@5.62.0(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint@8.57.0)(typescript@5.4.5)': dependencies: '@eslint-community/regexpp': 4.10.0 '@typescript-eslint/parser': 5.62.0(eslint@8.57.0)(typescript@5.4.5) @@ -3615,8 +3656,9 @@ snapshots: graphemer: 1.4.0 ignore: 5.3.0 natural-compare-lite: 1.4.0 - semver: 7.5.4 + semver: 7.6.0 tsutils: 3.21.0(typescript@5.4.5) + optionalDependencies: typescript: 5.4.5 transitivePeerDependencies: - supports-color @@ -3628,6 +3670,7 @@ snapshots: '@typescript-eslint/typescript-estree': 5.62.0(typescript@5.4.5) debug: 4.3.4 eslint: 8.57.0 + optionalDependencies: typescript: 5.4.5 transitivePeerDependencies: - supports-color @@ -3644,6 +3687,7 @@ snapshots: debug: 4.3.4 eslint: 8.57.0 tsutils: 3.21.0(typescript@5.4.5) + optionalDependencies: typescript: 5.4.5 transitivePeerDependencies: - supports-color @@ -3657,8 +3701,9 @@ snapshots: debug: 4.3.4 globby: 11.1.0 is-glob: 4.0.3 - semver: 7.5.4 + semver: 7.6.0 tsutils: 3.21.0(typescript@5.4.5) + optionalDependencies: typescript: 5.4.5 transitivePeerDependencies: - supports-color @@ -3667,13 +3712,13 @@ snapshots: dependencies: '@eslint-community/eslint-utils': 4.4.0(eslint@8.57.0) '@types/json-schema': 7.0.15 - '@types/semver': 7.5.6 + '@types/semver': 7.5.8 '@typescript-eslint/scope-manager': 5.62.0 '@typescript-eslint/types': 5.62.0 '@typescript-eslint/typescript-estree': 5.62.0(typescript@5.4.5) eslint: 8.57.0 eslint-scope: 5.1.1 - semver: 7.5.4 + semver: 7.6.0 transitivePeerDependencies: - supports-color - typescript @@ -3685,7 +3730,7 @@ snapshots: '@ungap/structured-clone@1.2.0': {} - '@vitest/coverage-v8@1.5.2(vitest@1.5.2)': + '@vitest/coverage-v8@1.5.2(vitest@1.5.2(@types/node@20.12.7)(sass@1.75.0))': dependencies: '@ampproject/remapping': 2.2.1 '@bcoe/v8-coverage': 0.2.3 @@ -3737,22 +3782,27 @@ snapshots: dependencies: '@volar/source-map': 1.11.1 + '@volar/language-core@2.2.0-alpha.10': + dependencies: + '@volar/source-map': 2.2.0-alpha.10 + '@volar/source-map@1.11.1': dependencies: muggle-string: 0.3.1 + '@volar/source-map@2.2.0-alpha.10': + dependencies: + muggle-string: 0.4.1 + '@volar/typescript@1.11.1': dependencies: '@volar/language-core': 1.11.1 path-browserify: 1.0.1 - '@vue/compiler-core@3.4.21': + '@volar/typescript@2.2.0-alpha.10': dependencies: - '@babel/parser': 7.24.0 - '@vue/shared': 3.4.21 - entities: 4.5.0 - estree-walker: 2.0.2 - source-map-js: 1.2.0 + '@volar/language-core': 2.2.0-alpha.10 + path-browserify: 1.0.1 '@vue/compiler-core@3.4.25': dependencies: @@ -3762,11 +3812,6 @@ snapshots: estree-walker: 2.0.2 source-map-js: 1.2.0 - '@vue/compiler-dom@3.4.21': - dependencies: - '@vue/compiler-core': 3.4.21 - '@vue/shared': 3.4.21 - '@vue/compiler-dom@3.4.25': dependencies: '@vue/compiler-core': 3.4.25 @@ -3793,14 +3838,27 @@ snapshots: dependencies: '@volar/language-core': 1.11.1 '@volar/source-map': 1.11.1 - '@vue/compiler-dom': 3.4.21 - '@vue/shared': 3.4.21 + '@vue/compiler-dom': 3.4.25 + '@vue/shared': 3.4.25 computeds: 0.0.1 minimatch: 9.0.3 muggle-string: 0.3.1 path-browserify: 1.0.1 + vue-template-compiler: 2.7.16 + optionalDependencies: typescript: 5.4.5 + + '@vue/language-core@2.0.14(typescript@5.4.5)': + dependencies: + '@volar/language-core': 2.2.0-alpha.10 + '@vue/compiler-dom': 3.4.25 + '@vue/shared': 3.4.25 + computeds: 0.0.1 + minimatch: 9.0.3 + path-browserify: 1.0.1 vue-template-compiler: 2.7.16 + optionalDependencies: + typescript: 5.4.5 '@vue/reactivity@3.4.25': dependencies: @@ -3817,14 +3875,12 @@ snapshots: '@vue/shared': 3.4.25 csstype: 3.1.3 - '@vue/server-renderer@3.4.25(vue@3.4.25)': + '@vue/server-renderer@3.4.25(vue@3.4.25(typescript@5.4.5))': dependencies: '@vue/compiler-ssr': 3.4.25 '@vue/shared': 3.4.25 vue: 3.4.25(typescript@5.4.5) - '@vue/shared@3.4.21': {} - '@vue/shared@3.4.25': {} acorn-jsx@5.3.2(acorn@8.11.3): @@ -3958,7 +4014,7 @@ snapshots: builtins@5.0.1: dependencies: - semver: 7.5.4 + semver: 7.6.0 bundle-name@3.0.0: dependencies: @@ -3975,7 +4031,7 @@ snapshots: ohash: 1.1.3 pathe: 1.1.2 perfect-debounce: 1.0.0 - pkg-types: 1.0.3 + pkg-types: 1.1.0 rc9: 2.1.1 c8@9.1.0: @@ -4046,9 +4102,9 @@ snapshots: ofetch: 1.3.3 open: 9.1.0 pathe: 1.1.2 - pkg-types: 1.0.3 + pkg-types: 1.1.0 scule: 1.2.0 - semver: 7.5.4 + semver: 7.6.0 std-env: 3.7.0 yaml: 2.3.4 @@ -4110,6 +4166,8 @@ snapshots: concat-map@0.0.1: {} + confbox@0.1.7: {} + consola@3.2.3: {} convert-gitmoji@0.1.3: {} @@ -4463,22 +4521,22 @@ snapshots: dependencies: eslint: 8.57.0 - eslint-config-standard@17.1.0(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.6.2)(eslint-plugin-promise@6.1.1)(eslint@8.57.0): + eslint-config-standard@17.1.0(eslint-plugin-import@2.29.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0))(eslint-plugin-n@16.6.2(eslint@8.57.0))(eslint-plugin-promise@6.1.1(eslint@8.57.0))(eslint@8.57.0): dependencies: eslint: 8.57.0 - eslint-plugin-import: 2.29.1(@typescript-eslint/parser@5.62.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) + eslint-plugin-import: 2.29.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) eslint-plugin-n: 16.6.2(eslint@8.57.0) eslint-plugin-promise: 6.1.1(eslint@8.57.0) eslint-config-unjs@0.2.1(eslint@8.57.0)(typescript@5.4.5): dependencies: - '@typescript-eslint/eslint-plugin': 5.62.0(@typescript-eslint/parser@5.62.0)(eslint@8.57.0)(typescript@5.4.5) + '@typescript-eslint/eslint-plugin': 5.62.0(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint@8.57.0)(typescript@5.4.5) '@typescript-eslint/parser': 5.62.0(eslint@8.57.0)(typescript@5.4.5) eslint: 8.57.0 eslint-config-prettier: 8.10.0(eslint@8.57.0) - eslint-config-standard: 17.1.0(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.6.2)(eslint-plugin-promise@6.1.1)(eslint@8.57.0) - eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@5.62.0)(eslint-plugin-import@2.29.1)(eslint@8.57.0) - eslint-plugin-import: 2.29.1(@typescript-eslint/parser@5.62.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) + eslint-config-standard: 17.1.0(eslint-plugin-import@2.29.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0))(eslint-plugin-n@16.6.2(eslint@8.57.0))(eslint-plugin-promise@6.1.1(eslint@8.57.0))(eslint@8.57.0) + eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0) + eslint-plugin-import: 2.29.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) eslint-plugin-n: 16.6.2(eslint@8.57.0) eslint-plugin-node: 11.1.0(eslint@8.57.0) eslint-plugin-promise: 6.1.1(eslint@8.57.0) @@ -4497,13 +4555,13 @@ snapshots: transitivePeerDependencies: - supports-color - eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@5.62.0)(eslint-plugin-import@2.29.1)(eslint@8.57.0): + eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0): dependencies: debug: 4.3.4 enhanced-resolve: 5.15.0 eslint: 8.57.0 - eslint-module-utils: 2.8.0(@typescript-eslint/parser@5.62.0)(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) - eslint-plugin-import: 2.29.1(@typescript-eslint/parser@5.62.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) + eslint-module-utils: 2.8.0(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0) + eslint-plugin-import: 2.29.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) fast-glob: 3.3.2 get-tsconfig: 4.7.2 is-core-module: 2.13.1 @@ -4514,13 +4572,14 @@ snapshots: - eslint-import-resolver-webpack - supports-color - eslint-module-utils@2.8.0(@typescript-eslint/parser@5.62.0)(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0): + eslint-module-utils@2.8.0(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0): dependencies: - '@typescript-eslint/parser': 5.62.0(eslint@8.57.0)(typescript@5.4.5) debug: 3.2.7 + optionalDependencies: + '@typescript-eslint/parser': 5.62.0(eslint@8.57.0)(typescript@5.4.5) eslint: 8.57.0 eslint-import-resolver-node: 0.3.9 - eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@5.62.0)(eslint-plugin-import@2.29.1)(eslint@8.57.0) + eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0) transitivePeerDependencies: - supports-color @@ -4537,9 +4596,8 @@ snapshots: eslint-utils: 2.1.0 regexpp: 3.2.0 - eslint-plugin-import@2.29.1(@typescript-eslint/parser@5.62.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0): + eslint-plugin-import@2.29.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0): dependencies: - '@typescript-eslint/parser': 5.62.0(eslint@8.57.0)(typescript@5.4.5) array-includes: 3.1.7 array.prototype.findlastindex: 1.2.3 array.prototype.flat: 1.3.2 @@ -4548,7 +4606,7 @@ snapshots: doctrine: 2.1.0 eslint: 8.57.0 eslint-import-resolver-node: 0.3.9 - eslint-module-utils: 2.8.0(@typescript-eslint/parser@5.62.0)(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) + eslint-module-utils: 2.8.0(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0) hasown: 2.0.0 is-core-module: 2.13.1 is-glob: 4.0.3 @@ -4558,6 +4616,8 @@ snapshots: object.values: 1.1.7 semver: 6.3.1 tsconfig-paths: 3.15.0 + optionalDependencies: + '@typescript-eslint/parser': 5.62.0(eslint@8.57.0)(typescript@5.4.5) transitivePeerDependencies: - eslint-import-resolver-typescript - eslint-import-resolver-webpack @@ -4576,7 +4636,7 @@ snapshots: is-core-module: 2.13.1 minimatch: 3.1.2 resolve: 1.22.8 - semver: 7.5.4 + semver: 7.6.0 eslint-plugin-node@11.1.0(eslint@8.57.0): dependencies: @@ -4609,7 +4669,7 @@ snapshots: regexp-tree: 0.1.27 regjsparser: 0.10.0 safe-regex: 2.1.1 - semver: 7.5.4 + semver: 7.6.0 strip-indent: 3.0.0 eslint-scope@5.1.1: @@ -5141,8 +5201,6 @@ snapshots: json5@2.2.3: {} - jsonc-parser@3.2.0: {} - jsonfile@6.1.0: dependencies: universalify: 2.0.1 @@ -5165,7 +5223,7 @@ snapshots: local-pkg@0.5.0: dependencies: mlly: 1.6.1 - pkg-types: 1.0.3 + pkg-types: 1.1.0 locate-path@5.0.0: dependencies: @@ -5215,7 +5273,7 @@ snapshots: make-dir@4.0.0: dependencies: - semver: 7.5.4 + semver: 7.6.0 mdn-data@2.0.28: {} @@ -5278,6 +5336,7 @@ snapshots: pathe: 1.1.2 postcss: 8.4.38 postcss-nested: 6.0.1(postcss@8.4.38) + optionalDependencies: sass: 1.75.0 typescript: 5.4.5 @@ -5285,7 +5344,7 @@ snapshots: dependencies: acorn: 8.11.3 pathe: 1.1.2 - pkg-types: 1.0.3 + pkg-types: 1.1.0 ufo: 1.3.2 modern-normalize@2.0.0: {} @@ -5298,6 +5357,8 @@ snapshots: muggle-string@0.3.1: {} + muggle-string@0.4.1: {} + nanoid@3.3.7: {} natural-compare-lite@1.4.0: {} @@ -5463,9 +5524,9 @@ snapshots: picomatch@2.3.1: {} - pkg-types@1.0.3: + pkg-types@1.1.0: dependencies: - jsonc-parser: 3.2.0 + confbox: 0.1.7 mlly: 1.6.1 pathe: 1.1.2 @@ -5923,7 +5984,7 @@ snapshots: semver@6.3.1: {} - semver@7.5.4: + semver@7.6.0: dependencies: lru-cache: 6.0.0 @@ -6178,13 +6239,14 @@ snapshots: mkdist: 1.4.0(sass@1.75.0)(typescript@5.4.5) mlly: 1.6.1 pathe: 1.1.2 - pkg-types: 1.0.3 + pkg-types: 1.1.0 pretty-bytes: 6.1.1 rollup: 3.29.4 rollup-plugin-dts: 6.1.0(rollup@3.29.4)(typescript@5.4.5) scule: 1.2.0 - typescript: 5.4.5 untyped: 1.4.2 + optionalDependencies: + typescript: 5.4.5 transitivePeerDependencies: - sass - supports-color @@ -6251,17 +6313,16 @@ snapshots: vite@5.0.12(@types/node@20.12.7)(sass@1.75.0): dependencies: - '@types/node': 20.12.7 esbuild: 0.19.11 postcss: 8.4.38 rollup: 4.9.6 - sass: 1.75.0 optionalDependencies: + '@types/node': 20.12.7 fsevents: 2.3.3 + sass: 1.75.0 vitest@1.5.2(@types/node@20.12.7)(sass@1.75.0): dependencies: - '@types/node': 20.12.7 '@vitest/expect': 1.5.2 '@vitest/runner': 1.5.2 '@vitest/snapshot': 1.5.2 @@ -6282,6 +6343,8 @@ snapshots: vite: 5.0.12(@types/node@20.12.7)(sass@1.75.0) vite-node: 1.5.2(@types/node@20.12.7)(sass@1.75.0) why-is-node-running: 2.2.2 + optionalDependencies: + '@types/node': 20.12.7 transitivePeerDependencies: - less - lightningcss @@ -6300,7 +6363,14 @@ snapshots: dependencies: '@volar/typescript': 1.11.1 '@vue/language-core': 1.8.27(typescript@5.4.5) - semver: 7.5.4 + semver: 7.6.0 + typescript: 5.4.5 + + vue-tsc@2.0.14(typescript@5.4.5): + dependencies: + '@volar/typescript': 2.2.0-alpha.10 + '@vue/language-core': 2.0.14(typescript@5.4.5) + semver: 7.6.0 typescript: 5.4.5 vue@3.4.25(typescript@5.4.5): @@ -6308,8 +6378,9 @@ snapshots: '@vue/compiler-dom': 3.4.25 '@vue/compiler-sfc': 3.4.25 '@vue/runtime-dom': 3.4.25 - '@vue/server-renderer': 3.4.25(vue@3.4.25) + '@vue/server-renderer': 3.4.25(vue@3.4.25(typescript@5.4.5)) '@vue/shared': 3.4.25 + optionalDependencies: typescript: 5.4.5 which-boxed-primitive@1.0.2: diff --git a/src/utils/vue-dts.ts b/src/utils/vue-dts.ts index 72c48cd..03351fd 100644 --- a/src/utils/vue-dts.ts +++ b/src/utils/vue-dts.ts @@ -1,5 +1,9 @@ import { createRequire } from "node:module"; -import { CompilerOptions } from "typescript"; +import { CompilerOptions, CreateProgramOptions } from "typescript"; +import { readPackageJSON } from "pkg-types"; +import { resolve as resolveModule } from "mlly"; +import { major } from "semver"; +import { normalize } from "pathe"; import { MkdistOptions } from "../make"; import { extractDeclarations } from "./dts"; @@ -12,6 +16,7 @@ const compilerOptions: CompilerOptions = { skipLibCheck: true, strictNullChecks: true, emitDeclarationOnly: true, + allowNonTsExtensions: true, }; export async function getVueDeclarations( @@ -25,16 +30,46 @@ export async function getVueDeclarations( return; } - const vueTsc = await import("vue-tsc") - .then((r) => r.default || r) - .catch(() => undefined); - if (!vueTsc) { + const pkgInfo = await readPackageJSON("vue-tsc").catch(() => {}); + if (!pkgInfo) { console.warn( "[mkdist] Please install `vue-tsc` to generate Vue SFC declarations.", ); return; } + const majorVersion = major(pkgInfo.version); + switch (majorVersion) { + case 1: { + await emitVueTscV1(vfs, srcFiles); + break; + } + case 2: { + await emitVueTscV2(vfs, srcFiles); + break; + } + } + + return extractDeclarations(vfs, originFiles, opts); +} + +const SFC_EXT_RE = /\.vue\.[cm]?[jt]s$/; + +function getFileMapping(vfs: Map): Record { + const files: Record = Object.create(null); + for (const [srcPath] of vfs) { + if (SFC_EXT_RE.test(srcPath)) { + files[srcPath.replace(SFC_EXT_RE, ".vue")] = srcPath; + } + } + return files; +} + +async function emitVueTscV1(vfs: Map, srcFiles: string[]) { + const vueTsc: typeof import("vue-tsc1") = await import("vue-tsc") + .then((r) => r.default || r) + .catch(() => undefined); + // Inside vue-tsc, `require` is used instead of `import`. In order to override `ts.sys`, it is necessary to import it in the same way as vue-tsc for them to refer to the same file. const ts = require("typescript") as typeof import("typescript/lib/tsserverlibrary"); @@ -53,30 +88,74 @@ export async function getVueDeclarations( return _tsSysReadFile(filename, encoding); }; - const program = vueTsc.createProgram({ - rootNames: srcFiles, - options: compilerOptions, - host: tsHost, - }); - try { + const program = vueTsc.createProgram({ + rootNames: srcFiles, + options: compilerOptions, + host: tsHost, + }); + program.emit(); } finally { ts.sys.writeFile = _tsSysWriteFile; ts.sys.readFile = _tsSysReadFile; } - - return extractDeclarations(vfs, originFiles, opts); } -const SFC_EXT_RE = /\.vue\.[cm]?[jt]s$/; +async function emitVueTscV2(vfs: Map, srcFiles: string[]) { + const ts: typeof import("typescript") = await import("typescript"); + const vueTsc: typeof import("vue-tsc") = await import("vue-tsc"); + const requireFromVueTsc = createRequire(await resolveModule("vue-tsc")); + const vueLanguageCore: typeof import("@vue/language-core") = + requireFromVueTsc("@vue/language-core"); + const volarTs: typeof import("@volar/typescript") = + requireFromVueTsc("@volar/typescript"); -function getFileMapping(vfs: Map): Record { - const files: Record = Object.create(null); - for (const [srcPath] of vfs) { - if (SFC_EXT_RE.test(srcPath)) { - files[srcPath.replace(SFC_EXT_RE, ".vue")] = srcPath; + const tsHost = ts.createCompilerHost(compilerOptions); + tsHost.writeFile = (filename, content) => { + vfs.set(filename, vueTsc.removeEmitGlobalTypes(content)); + }; + const _tsReadFile = tsHost.readFile.bind(tsHost); + tsHost.readFile = (filename) => { + if (vfs.has(filename)) { + return vfs.get(filename); } - } - return files; + return _tsReadFile(filename); + }; + const _tsFileExist = tsHost.fileExists.bind(tsHost); + tsHost.fileExists = (filename) => { + return vfs.has(filename) || _tsFileExist(filename); + }; + + const programOptions: CreateProgramOptions = { + rootNames: srcFiles, + options: compilerOptions, + host: tsHost, + }; + + const createProgram = volarTs.proxyCreateProgram( + ts, + ts.createProgram, + (ts, options) => { + const vueLanguagePlugin = vueLanguageCore.createVueLanguagePlugin( + ts, + (id) => id, + options.host?.useCaseSensitiveFileNames?.() ?? false, + () => "", + () => options.rootNames.map((rootName) => normalize(rootName)), + options.options, + vueLanguageCore.resolveVueCompilerOptions({}), + ); + return [vueLanguagePlugin]; + }, + (fileName) => { + if ([".vue"].some((ext) => fileName.endsWith(ext))) { + return "vue"; + } + return vueLanguageCore.resolveCommonLanguageId(fileName); + }, + ); + + const program = createProgram(programOptions); + program.emit(); } diff --git a/test/index.test.ts b/test/index.test.ts index c0a544a..6e96515 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,10 +1,23 @@ import { readFile } from "node:fs/promises"; import { resolve } from "pathe"; -import { describe, it, expect, beforeEach } from "vitest"; -import { mkdist } from "../src/make"; +import { + describe, + it, + expect, + beforeEach, + vi, + beforeAll, + afterAll, +} from "vitest"; import { createLoader } from "../src/loader"; describe("mkdist", () => { + let mkdist: typeof import("../src/make").mkdist; + + beforeAll(async () => { + mkdist = (await import("../src/make")).mkdist; + }); + it("mkdist", async () => { const rootDir = resolve(__dirname, "fixture"); const { writtenFiles } = await mkdist({ rootDir }); @@ -131,6 +144,16 @@ describe("mkdist", () => { expect( await readFile(resolve(rootDir, "dist/bar/esm.d.mts"), "utf8"), ).toMatch("declare"); + expect( + await readFile(resolve(rootDir, "dist/components/ts.vue.d.ts"), "utf8"), + ).toMatchInlineSnapshot(` + "declare const _default: import("vue").DefineComponent<{}, {}, { + test: string; + str: "test"; + }, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly>, {}, {}>; + export default _default; + " + `); }, 50_000); describe("mkdist (sass compilation)", () => { @@ -373,3 +396,106 @@ describe("mkdist", () => { `); }); }); + +describe("mkdist with vue-tsc v1", () => { + beforeAll(() => { + vi.resetModules(); + + vi.doMock("pkg-types", async (importOriginal) => { + const original = await importOriginal(); + return { + ...original, + getPackageInfo: (path: string) => { + if (path === "vue-tsc") { + return original.readPackageJSON("vue-tsc1"); + } + return original.readPackageJSON(path); + }, + }; + }); + vi.doMock("vue-tsc", async () => { + const vueTsc1 = await import("vue-tsc1"); + return { + removeEmitGlobalTypes: (content) => content, + ...vueTsc1, + }; + }); + }); + + afterAll(() => { + vi.doUnmock("pkg-types"); + vi.doUnmock("vue-tsc"); + }); + + it("mkdist (emit types)", async () => { + const rootDir = resolve(__dirname, "fixture"); + const { mkdist } = await import("../src/make"); + + const { writtenFiles } = await mkdist({ + rootDir, + declaration: true, + addRelativeDeclarationExtensions: true, + }); + expect(writtenFiles.sort()).toEqual( + [ + "dist/README.md", + "dist/demo.css", + "dist/foo.mjs", + "dist/foo.d.ts", + "dist/index.mjs", + "dist/index.d.ts", + "dist/star/index.mjs", + "dist/star/index.d.ts", + "dist/star/other.mjs", + "dist/star/other.d.ts", + "dist/types.d.ts", + "dist/components/blank.vue", + "dist/components/js.vue", + "dist/components/js.vue.d.ts", + "dist/components/script-setup-ts.vue", + "dist/components/ts.vue", + "dist/components/ts.vue.d.ts", + "dist/components/jsx.mjs", + "dist/components/tsx.mjs", + "dist/components/jsx.d.ts", + "dist/components/tsx.d.ts", + "dist/bar/index.mjs", + "dist/bar/index.d.ts", + "dist/bar/esm.mjs", + "dist/bar/esm.d.mts", + "dist/ts/test1.mjs", + "dist/ts/test2.mjs", + "dist/ts/test1.d.mts", + "dist/ts/test2.d.cts", + "dist/nested.css", + ] + .map((f) => resolve(rootDir, f)) + .sort(), + ); + + expect(await readFile(resolve(rootDir, "dist/foo.d.ts"), "utf8")).toMatch( + "manual declaration", + ); + + expect(await readFile(resolve(rootDir, "dist/star/index.d.ts"), "utf8")) + .toMatchInlineSnapshot(` + "export * from "./other.js"; + export type { Other } from "./other.js"; + " + `); + expect( + await readFile(resolve(rootDir, "dist/bar/esm.d.mts"), "utf8"), + ).toMatch("declare"); + + expect( + await readFile(resolve(rootDir, "dist/components/ts.vue.d.ts"), "utf8"), + ).toMatchInlineSnapshot(` + "declare const _default: import("vue").DefineComponent<{}, {}, { + test: string; + str: "test"; + }, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly>, {}, {}>; + export default _default; + " + `); + }, 50_000); +});