TypeStrong / fork-ts-checker-webpack-plugin

Webpack plugin that runs typescript type checker on a separate process.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can I check the specified file?

a145789 opened this issue · comments

Hello!

my project is using vue2 + ts + vite + webpack . I need webpack build, but vite can't check very well. So I want to do some checks in git push pre-commit.

I wrote a script

const TsCheck = require('fork-ts-checker-webpack-plugin')
const path = require('path')
const process = require('process')
const { chalk } = require('@vue/cli-shared-utils')

const mainDir = path.resolve(__dirname, '../')

const comply = {
  done: [],
}
const compilation = {
  errors: [],
  warnings: [],
}

const tsCheck = new TsCheck({
  vue: { enabled: true, compiler: 'vue-template-compiler' },
  tslint: mainDir + '/tslint.json',
  tsconfig: mainDir + '/tsconfig.json',
  formatter: 'codeframe',
  // https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse
  checkSyntacticErrors: true,
})

tsCheck.apply({
  options: {
    context: mainDir,
  },
  hooks: {
    run: {
      tapAsync(name, run) {
        comply.run = run
      },
    },
    watchRun: {
      tapAsync(name, watchRun) {
        comply.watchRun = watchRun
      },
    },
    watchClose: {
      tap(name, watchClose) {
        comply.watchClose = watchClose
      },
    },
    compile: {
      tap(name, compile) {
        comply.compile = compile
      },
    },
    emit: {
      tapAsync(name, emit) {
        comply.emit = emit
      },
    },
    done: {
      tap(name, done) {
        comply.done.push(done)
      },
    },
  },
})

comply.run(1, () => {})
comply.compile()
comply.emit(compilation, () => {
  console.log('done')
  comply.done.forEach((done) => done())
  if (compilation.errors.length) {
    console.log()
    console.log(chalk.redBright(`Failed to compile with ${compilation.errors.length} error`))
    compilation.errors.forEach(({ message }) => {
      console.log()
      console.log(message)
    })
    process.exit(1)
  }
})

but it's slow, I want to check only modified files or specified files, Is there any good way?

Looking forward to your reply.

Hi! This plugin will not help you much in terms of performance - it's optimized for watch mode and will not be faster than tsc in a single run. We're limited by the TypeScript API and as far as I know it's not possible to force TypeScript to check only some files.

By the way - I don't think type-checking only changed files will give you correct results. For example if you change type in one file, it can cause a type error in another file that wasn't touched.

Hi! This plugin will not help you much in terms of performance - it's optimized for watch mode and will not be faster than tsc in a single run. We're limited by the TypeScript API and as far as I know it's not possible to force TypeScript to check only some files.

By the way - I don't think type-checking only changed files will give you correct results. For example if you change type in one file, it can cause a type error in another file that wasn't touched.

thank you very much for your reply