Skip to content

Commit

Permalink
feat: use @vue/compiler-sfc as a compiler for TS if available
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cexbrayat committed Feb 19, 2020
1 parent b9a6765 commit 2b2f03a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/@vue/cli-plugin-typescript/index.js
Expand Up @@ -77,12 +77,24 @@ module.exports = (api, projectOptions) => {
})

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(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: true,
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
Expand Down
7 changes: 7 additions & 0 deletions packages/@vue/cli-plugin-typescript/vue-compiler-sfc-shim.js
@@ -0,0 +1,7 @@
const compilerSFC = require('@vue/compiler-sfc')

module.exports = {
parseComponent (content, options) {
return compilerSFC.parse(content, options)
}
}

0 comments on commit 2b2f03a

Please sign in to comment.