evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
37.95k stars 1.14k forks source link

Getting the dependencies gathered by esbuild #1535

Open unilynx opened 3 years ago

unilynx commented 3 years ago

I'm trying to implement my own watch/incremental build system om top of esbuild. For this, I need to figure out which files should trigger recompilation of a bundle.

Is there a way to get a list of all the input dependencies used by esbuild? I'm looking for something more-or-less equivalent to webpack's fileDependencies and missingDependencies. I presume esbuild has these lists somewhere to implement its own watchlists but is not exporting it yet.

I've tried to set 'incremental:true' to esbuild but that doesn't return any further information.

I'm also looking at metafile: true which might allow me to simulate fileDependencies but not missingDependencies, and it looks like the information is incomplete when an onResolve intercepted the lookup (eg for esbuild-plugin-sass I'm seeing temporary files instead of the source files, but maybe that's something I need to fix in esbuild-plugin-sass.)

EDIT: oh, and a failed compilation due to a syntax error doesn't give me a metafile, so I don't know what files to watch to know if I can retry the compilation.

unilynx commented 3 years ago

Ah, I see https://github.com/evanw/esbuild/issues/1527#issuecomment-901145252 has made a similar request.

unilynx commented 3 years ago

as a workaround I've added the following plugin to capture most of the dependencies, even when compilation fails

class captureLoadPlugin
{
  constructor(loadcache)
  {
    this.loadcache = new Set;
  }
  getPlugin()
  {
    return { name: 'captureloads'
           , setup: build => this.setup(build)
           };
  }
  setup(build)
  {
    build.onLoad({filter:/./}, args =>
    {
      this.loadcache.add(args.path);
    });
  }
}

....

  let captureplugin = new captureLoadPlugin;

....

      , plugins: [ captureplugin.getPlugin(), .... ]

....

let fileDepencies = Array.from(captureplugin.loadcache);

after swapping esbuild-plugin-sass to esbuild-sass-plugin, I seem to have gotten reasonably close to what I had with webpack. this will probably not be compatible with all plugins and I haven't started working with incremental:true yet, I probably need to clear the 'loadcache' at some point then.

@arcanis maybe this helps you in your issue #1527 too?