unjs / unplugin

Unified plugin system for Vite, Rollup, Webpack, esbuild, Rolldown, and more
https://unplugin.unjs.io
MIT License
2.9k stars 104 forks source link

How does watch mode work? #295

Open NullVoxPopuli opened 1 year ago

NullVoxPopuli commented 1 year ago

Environment

node 18, webpack 5

Reproduction

I have this plugin that I'm using in webpack:

const { createUnplugin } = require('unplugin');

const copyToPublic = createUnplugin((options) => {
  let { src, include, dest } = options ?? {};

  dest ??= src;
  include ??= '**/*';

  return {
    name: 'copy-files-to-public',
    async buildStart() {
      const files = globby.sync(include, { cwd: src });

      await Promise.all(
        files.map(async (file) => {
          let source = path.join(src, file);

          this.addWatchFile(source);

          await this.emitFile({
            type: 'asset',
            fileName: path.join(dest, file),
            source: fs.readFileSync(source).toString(),
          });
        })
      );
    },
  };
});

Like this:

        plugins: [
          copyToPublic.webpack({ src: 'docs' }),

        ],
      },

Describe the bug

when I tried to use the watchChange hook, it was never called (no console.log's were logged) (I made sure to change a few of the files found by globby)

Additional context

No response

Logs

No response