Skip to content

Commit 3633cf5

Browse files
authored
feat: use @vue/compiler-sfc as a compiler for TS if available (vuejs#5170)
* feat: use @vue/compiler-sfc as a compiler for TS if available The `fork-ts-checker-webpack-plugin` is using `vue-template-compiler` by default, and this compiler is not the correct one to pick for vue-next. This commit tries to load `@vue/compiler-sfc` and falls back to `vue-template-compiler` if it does not find it. * chore: bump fork-ts-checker-webpack-plugin to v3.1.1
1 parent 12f3d3b commit 3633cf5

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

packages/@vue/cli-plugin-typescript/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,24 @@ module.exports = (api, projectOptions) => {
7777
})
7878

7979
if (!process.env.VUE_CLI_TEST) {
80+
// try to load `@vue/compiler-sfc` if the project is using Vue 3.
81+
// if it is not available, it uses `vue-template-compiler`
82+
let compiler = '@vue/compiler-sfc'
83+
try {
84+
require.resolve(compiler)
85+
// use a shim as @vue/compiler-sfc does not offer the `parseComponent` function
86+
// but a `parse` function
87+
// the shim only delegates to the parse function
88+
compiler = '@vue/cli-plugin-typescript/vue-compiler-sfc-shim'
89+
} catch (e) {
90+
compiler = 'vue-template-compiler'
91+
}
8092
// this plugin does not play well with jest + cypress setup (tsPluginE2e.spec.js) somehow
8193
// so temporarily disabled for vue-cli tests
8294
config
8395
.plugin('fork-ts-checker')
8496
.use(require('fork-ts-checker-webpack-plugin'), [{
85-
vue: true,
97+
vue: { enabled: true, compiler },
8698
tslint: projectOptions.lintOnSave !== false && fs.existsSync(api.resolve('tslint.json')),
8799
formatter: 'codeframe',
88100
// https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse

packages/@vue/cli-plugin-typescript/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@types/webpack-env": "^1.15.1",
2727
"@vue/cli-shared-utils": "^4.2.3",
2828
"cache-loader": "^4.1.0",
29-
"fork-ts-checker-webpack-plugin": "^1.5.1",
29+
"fork-ts-checker-webpack-plugin": "^3.1.1",
3030
"globby": "^9.2.0",
3131
"thread-loader": "^2.1.3",
3232
"ts-loader": "^6.2.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const compilerSFC = require('@vue/compiler-sfc')
2+
3+
module.exports = {
4+
parseComponent (content, options) {
5+
return compilerSFC.parse(content, options)
6+
}
7+
}

0 commit comments

Comments
 (0)