From 133a93077e6ae873a88cc304a0a4d622a610ab21 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Tue, 28 Jul 2020 17:26:41 +0800 Subject: [PATCH] fix: use fork-ts-checker-webpack-plugin v5 for vue 3 type checking Fixes #5712 Note this is a optional dependency so that we don't break compatibility with Node.js v8. --- packages/@vue/cli-plugin-typescript/index.js | 57 +++++++++------- .../@vue/cli-plugin-typescript/package.json | 3 + yarn.lock | 66 +++++++++++++++---- 3 files changed, 90 insertions(+), 36 deletions(-) diff --git a/packages/@vue/cli-plugin-typescript/index.js b/packages/@vue/cli-plugin-typescript/index.js index ce03f3a189..a411e990cf 100644 --- a/packages/@vue/cli-plugin-typescript/index.js +++ b/packages/@vue/cli-plugin-typescript/index.js @@ -4,6 +4,10 @@ module.exports = (api, projectOptions) => { const fs = require('fs') const useThreads = process.env.NODE_ENV === 'production' && !!projectOptions.parallel + const { semver, loadModule } = require('@vue/cli-shared-utils') + const vue = loadModule('vue', api.service.context) + const isVue3 = (vue && semver.major(vue.version) === 3) + api.chainWebpack(config => { config.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules')) @@ -76,30 +80,39 @@ module.exports = (api, projectOptions) => { return options }) + // this plugin does not play well with jest + cypress setup (tsPluginE2e.spec.js) somehow + // so temporarily disabled for vue-cli tests if (!process.env.VUE_CLI_TEST) { - // try to load `@vue/compiler-sfc` if the project is using Vue 3. - // if it is not available, it uses `vue-template-compiler` - let compiler = '@vue/compiler-sfc' - try { - require.resolve(compiler) - // use a shim as @vue/compiler-sfc does not offer the `parseComponent` function - // but a `parse` function - // the shim only delegates to the parse function - compiler = '@vue/cli-plugin-typescript/vue-compiler-sfc-shim' - } catch (e) { - compiler = 'vue-template-compiler' - } - // this plugin does not play well with jest + cypress setup (tsPluginE2e.spec.js) somehow - // so temporarily disabled for vue-cli tests - config - .plugin('fork-ts-checker') - .use(require('fork-ts-checker-webpack-plugin'), [{ - vue: { enabled: true, compiler }, - tslint: projectOptions.lintOnSave !== false && fs.existsSync(api.resolve('tslint.json')), - formatter: 'codeframe', - // https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse - checkSyntacticErrors: useThreads + if (isVue3) { + config + .plugin('fork-ts-checker') + .use(require('fork-ts-checker-webpack-plugin-v5'), [{ + typescript: { + extensions: { + vue: { + enabled: true, + // use a shim as @vue/compiler-sfc does not offer the `parseComponent` function + compiler: '@vue/cli-plugin-typescript/vue-compiler-sfc-shim' + } + }, + diagnosticOptions: { + semantic: true, + // https://github.com/TypeStrong/ts-loader#happypackmode + syntactic: useThreads + } + } }]) + } else { + config + .plugin('fork-ts-checker') + .use(require('fork-ts-checker-webpack-plugin'), [{ + vue: { enabled: true, compiler: 'vue-template-compiler' }, + tslint: projectOptions.lintOnSave !== false && fs.existsSync(api.resolve('tslint.json')), + formatter: 'codeframe', + // https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse + checkSyntacticErrors: useThreads + }]) + } } }) diff --git a/packages/@vue/cli-plugin-typescript/package.json b/packages/@vue/cli-plugin-typescript/package.json index ad010a7f36..a76ddb6ee0 100644 --- a/packages/@vue/cli-plugin-typescript/package.json +++ b/packages/@vue/cli-plugin-typescript/package.json @@ -34,6 +34,9 @@ "webpack": "^4.0.0", "yorkie": "^2.0.0" }, + "optionalDependencies": { + "fork-ts-checker-webpack-plugin-v5": "npm:fork-ts-checker-webpack-plugin@^5.0.11" + }, "peerDependencies": { "@vue/cli-service": "^3.0.0 || ^4.0.0-0", "@vue/compiler-sfc": "^3.0.0-beta.14", diff --git a/yarn.lock b/yarn.lock index 8ebfef40e7..d13769cd8c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -105,7 +105,7 @@ dependencies: xss "^1.0.6" -"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.10.4": +"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.10.4", "@babel/code-frame@^7.8.3": version "7.10.4" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.4.tgz#168da1a36e90da68ae8d49c0f1b48c7c6249213a" integrity sha512-vG6SvB6oYEhvgisZNFRmRCUkLz11c7rp+tbNTynGqc6mS1d5ATd/sGyV6W0KZZnXRKMTzZDRgQT3Ou9jhpAfUg== @@ -2691,7 +2691,7 @@ ast-types "0.12.1" recast "0.17.2" -"@types/json-schema@^7.0.3", "@types/json-schema@^7.0.4": +"@types/json-schema@^7.0.3", "@types/json-schema@^7.0.4", "@types/json-schema@^7.0.5": version "7.0.5" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.5.tgz#dcce4430e64b443ba8945f0290fb564ad5bac6dd" integrity sha512-7+2BITlgjgDhH0vvwZU/HZJVyk+2XUlvxXe8dFMedNX/aMkaOq++rMAFXc0tM7ij15QaWlbdQASBR9dihi+bDQ== @@ -2781,6 +2781,11 @@ resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA== +"@types/parse-json@^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" + integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA== + "@types/puppeteer@^1.11.0": version "1.20.4" resolved "https://registry.yarnpkg.com/@types/puppeteer/-/puppeteer-1.20.4.tgz#30cb0a4ee5394c420119cbdf9f079d6595a07f67" @@ -6421,6 +6426,17 @@ cosmiconfig@^5.0.0, cosmiconfig@^5.0.6, cosmiconfig@^5.1.0, cosmiconfig@^5.2.1: js-yaml "^3.13.1" parse-json "^4.0.0" +cosmiconfig@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-6.0.0.tgz#da4fee853c52f6b1e6935f41c1a2fc50bd4a9982" + integrity sha512-xb3ZL6+L8b9JLLCx3ZdoZy4+2ECphCMo2PwqgP1tlfVq6M6YReyzBJtvWWtbDSpNr9hn96pkCiZqUcFEc+54Qg== + dependencies: + "@types/parse-json" "^4.0.0" + import-fresh "^3.1.0" + parse-json "^5.0.0" + path-type "^4.0.0" + yaml "^1.7.2" + create-ecdh@^4.0.0: version "4.0.3" resolved "https://registry.yarnpkg.com/create-ecdh/-/create-ecdh-4.0.3.tgz#c9111b6f33045c4697f144787f9254cdc77c45ff" @@ -8907,6 +8923,23 @@ forever-agent@~0.6.1: resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE= +"fork-ts-checker-webpack-plugin-v5@npm:fork-ts-checker-webpack-plugin@^5.0.11": + version "5.0.11" + resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.0.11.tgz#40a49a45403910c118c6ae7f3415dbdb317e4574" + integrity sha512-7QFCTfjayFhoX7uxncnHHi3ICWwffUQMoYaHldElA6wYy/9aDTsRIi6QcS8MDAkZa7xdDyv0YKuqcGYjAzboyQ== + dependencies: + "@babel/code-frame" "^7.8.3" + "@types/json-schema" "^7.0.5" + chalk "^4.1.0" + cosmiconfig "^6.0.0" + deepmerge "^4.2.2" + fs-extra "^9.0.0" + memfs "^3.1.2" + minimatch "^3.0.4" + schema-utils "2.7.0" + semver "^7.3.2" + tapable "^1.0.0" + fork-ts-checker-webpack-plugin@^3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-3.1.1.tgz#a1642c0d3e65f50c2cc1742e9c0a80f441f86b19" @@ -9020,7 +9053,7 @@ fs-extra@^8.1.0: jsonfile "^4.0.0" universalify "^0.1.0" -fs-extra@^9.0.1: +fs-extra@^9.0.0, fs-extra@^9.0.1: version "9.0.1" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc" integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ== @@ -10321,7 +10354,7 @@ import-fresh@^2.0.0: caller-path "^2.0.0" resolve-from "^3.0.0" -import-fresh@^3.0.0: +import-fresh@^3.0.0, import-fresh@^3.1.0: version "3.2.1" resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.2.1.tgz#633ff618506e793af5ac91bf48b72677e15cbe66" integrity sha512-6e1q1cnWP2RXD9/keSkxHScg508CdXqXWgWBaETNhyuBFz+kUZlKboh+ISK+bU++DmbHimVBrOz/zzPe0sZ3sQ== @@ -12766,7 +12799,7 @@ mem@^4.0.0: mimic-fn "^2.0.0" p-is-promise "^2.0.0" -memfs@^3.2.0: +memfs@^3.1.2, memfs@^3.2.0: version "3.2.0" resolved "https://registry.yarnpkg.com/memfs/-/memfs-3.2.0.tgz#f9438e622b5acd1daa8a4ae160c496fdd1325b26" integrity sha512-f/xxz2TpdKv6uDn6GtHee8ivFyxwxmPuXatBb1FBwxYNuVpbM3k/Y1Z+vC0mH/dIXXrukYfe3qe5J32Dfjg93A== @@ -16301,6 +16334,15 @@ saxes@^3.1.9: dependencies: xmlchars "^2.1.1" +schema-utils@2.7.0, schema-utils@^2.0.0, schema-utils@^2.5.0, schema-utils@^2.6.1, schema-utils@^2.6.5, schema-utils@^2.6.6, schema-utils@^2.7.0: + version "2.7.0" + resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-2.7.0.tgz#17151f76d8eae67fbbf77960c33c676ad9f4efc7" + integrity sha512-0ilKFI6QQF5nxDZLFn2dMjvc4hjg/Wkg7rHd3jK6/A4a1Hl9VFdQWvgB1UMGoU94pad1P/8N7fMcEnLnSiju8A== + dependencies: + "@types/json-schema" "^7.0.4" + ajv "^6.12.2" + ajv-keywords "^3.4.1" + schema-utils@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-1.0.0.tgz#0b79a93204d7b600d4b2850d1f66c2a34951c770" @@ -16310,15 +16352,6 @@ schema-utils@^1.0.0: ajv-errors "^1.0.0" ajv-keywords "^3.1.0" -schema-utils@^2.0.0, schema-utils@^2.5.0, schema-utils@^2.6.1, schema-utils@^2.6.5, schema-utils@^2.6.6, schema-utils@^2.7.0: - version "2.7.0" - resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-2.7.0.tgz#17151f76d8eae67fbbf77960c33c676ad9f4efc7" - integrity sha512-0ilKFI6QQF5nxDZLFn2dMjvc4hjg/Wkg7rHd3jK6/A4a1Hl9VFdQWvgB1UMGoU94pad1P/8N7fMcEnLnSiju8A== - dependencies: - "@types/json-schema" "^7.0.4" - ajv "^6.12.2" - ajv-keywords "^3.4.1" - scrollparent@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/scrollparent/-/scrollparent-2.0.1.tgz#715d5b9cc57760fb22bdccc3befb5bfe06b1a317" @@ -19660,6 +19693,11 @@ yaml-js@0.0.8: resolved "https://registry.yarnpkg.com/yaml-js/-/yaml-js-0.0.8.tgz#87cfa5a9613f48e26005420d6a8ee0da6fe8daec" integrity sha1-h8+lqWE/SOJgBUINao7g2m/o2uw= +yaml@^1.7.2: + version "1.10.0" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e" + integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg== + yargs-parser@10.x: version "10.1.0" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"