Giter VIP home page Giter VIP logo

Comments (5)

piotr-oles avatar piotr-oles commented on June 1, 2024

I think you can use regular webpack hooks to do that. In the example, you don't use data emitted by the plugin, so you can simply tap into webpack's hooks :)

from fork-ts-checker-webpack-plugin.

piotr-oles avatar piotr-oles commented on June 1, 2024

Ah, you want to wait for the new version of files to be emitted, right? In that case, you will have to set async: false in plugin options. This way webpack will wait for the plugin to finish type checking. The issues hook is called on afterCompile in async: false mode

from fork-ts-checker-webpack-plugin.

sgarfinkel avatar sgarfinkel commented on June 1, 2024

Cool, thanks! I'll close the issue then.

from fork-ts-checker-webpack-plugin.

sgarfinkel avatar sgarfinkel commented on June 1, 2024

Hi @piotr-oles sorry for necroing this. I'm trying this path again and am still having deprecation warnings. It looks like afterCompile is too late. In Webpack 4 you can modify the emitted assets at this hook, but in Webpack 5+ it is now an error to do so (I assume in Webpack 6 it will be blocked entirely). There is now an earlier hook, compilation.hooks.processAssets that is meant for modifying the set of emitted assets. Ideally it would be great if async: false tapped this hook.

from fork-ts-checker-webpack-plugin.

mishani0x0ef avatar mishani0x0ef commented on June 1, 2024

I have faced a similar issue.

Use case

  1. React library
  2. ts-loader replaced with esbuild-loader to speedup build
  3. d.ts generation delegated from ts-loader to ForkTsCheckerWebpackPlugin

Workaround

For now, I have come up with the following workaround:

  1. Configure types to be emitted into a separate folder (so, it's easier to control them)
  2. Use CleanWebpackPlugin to clean build assets
  3. Skip cleanup of types
  4. Clean types only once at the init phase using a custom plugin
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { rmSync, existsSync } = require('fs');

// Custom plugin that cleanup only definition files
class DefinitionsCleanupPlugin {
  apply(compiler) {
    const outputPath = compiler.options.output.path;

    if (!outputPath) {
      throw new Error('[DefinitionsCleanupPlugin]: No output path provided');
    }

    compiler.hooks.initialize.tap('DefinitionsCleanupPlugin', () => {
      const typesPath = `${outputPath}/types`;

      if (existsSync(typesPath)) {
        rmSync(typesPath, { force: true, recursive: true });
      }
    });
  }
}

...

plugins: [
    new ForkTsCheckerWebpackPlugin({
      typescript: {
        mode: 'write-dts',
      },
    }),
    new DefinitionsCleanupPlugin(),
    new CleanWebpackPlugin({
      // Skip cleanup of types
      cleanOnceBeforeBuildPatterns: ['**/*', '!types/**/*', '!types'],
    }),
]

Cons:

  • In watch mode, definitions are not cleaned during rebuilds (probably can be tuned by tapping other hooks in custom plugin, but it's ok for my scenario)
  • It still would be better to have some beforeEmit hook in the ForkTsCheckerWebpackPlugin itself and tap into it to perform a cleanup

from fork-ts-checker-webpack-plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.