Skip to content

Generated worker files are missing from compilation.chunksΒ #32

@bebraw

Description

@bebraw

I'm currently writing a small plugin to allow prefetching workers written with the plugin (the principle works). During this work, I noticed that webpack compilation.chunks is missing the worker files.

Here's my current plugin code:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const insertLinksIntoHead = require('./insert-links-into-head');

class PrefetchPlugin {
  constructor(options) {
    this.options = options;
  }
  apply(compiler) {
    const { include } = this.options;

    // TODO: Match using include
    compiler.hooks.compilation.tap('PrefetchPlugin', (compilation) => {
      console.log('include', include);

      HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync('PrefetchPlugin', (data, cb) => {
        const links = []; // TODO: Populate with HTML for prefetching

        const allFiles = compilation.chunks.reduce((accumulated, chunk) => {
          return accumulated.concat(chunk.files);
        }, []);

        console.log('chunks', allFiles);

        data.html = insertLinksIntoHead({
          links,
          html: data.html,
        });

        cb(null, data);
      });
    });
  }
}

module.exports = PrefetchPlugin;

That helper module is directly from https://github.com/GoogleChromeLabs/preload-webpack-plugin .

I peeked at the code and I noticed threads-plugin is using a child compiler within a loader to write the worker files. My feeling is that there's something missing that would communicate the emitted files to the parent compilation.

What's the technical reason for using a child compiler? Have you encountered something like this before?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions