Skip to content

Emitted files should be added as assets to the compilation #813

Open
@sgarfinkel

Description

@sgarfinkel

Feature motivation

Currently, if the checker emits files to the webpack outputPath, setting { output: { clean: true } } will remove them. The reason is because Webpack does not know that emitted files, such as declarations and declaration maps, should be treated as assets generated by the bundle.

At a minimum, it would be nice if the set of emitted files was available in a tapable hook that runs before the compilation finishes. It looks like you can sort of get this by tapping the issues hook and then globbing the output folder, but it seems like this hook fires too late. In Webpack 5, you'll get

(node:98113) [DEP_WEBPACK_COMPILATION_ASSETS] DeprecationWarning: Compilation.assets will be frozen in future, all modifications are deprecated.
BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
        Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
        Make sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.
(Use `node --trace-deprecation ...` to show where the warning was created)

Feature description

It would be nice if the plugin just did this out of the box, but at a minimum, it would be nice to add a new hook that runs during the emit/processAsset phase and passes in the list of emitted files to the tap.

Feature implementation

Here's an example that uses the existing hooks, as well as fast-glob to grab the set of generated files and add them as assets. The problem is that it runs too late and throws deprecation warnings in Webpack 5.

{
  apply(compiler) {
    ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler).issues.tap('example', (_, compilation) => {
      glob.sync(`${compilation.outputOptions.path}/types/**/*`).forEach((file) => {
        compilation.emitAsset(
          path.relative(compilation.outputOptions.path, file),
          new compiler.webpack.sources.RawSource(readFileSync(file), false)
        );
      });
    });
  },
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions