Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watchFiles and watchDirs are excluded from Metafile #1268

Open
alanorozco opened this issue May 10, 2021 · 3 comments
Open

watchFiles and watchDirs are excluded from Metafile #1268

alanorozco opened this issue May 10, 2021 · 3 comments

Comments

@alanorozco
Copy link

alanorozco commented May 10, 2021

Plugins recently got the ability to return watchFiles or watchDirs that cause a re-build when they are modified.

Our build pipeline (amphtml) is complicated enough that we can't use esbuild's {watch: true}. Instead, we rely on early Metafile results to trigger a re-build ourselves.

We expected watchFiles/Dirs to be included in the Metafile, but they are not.

Build config

import esbuild from 'esbuild';

const myPlugin = {
  name: 'my-plugin',
  setup(build) {
    build.onLoad({filter: /.+/}, (args) => {
      return {
        contents: args.path,
        loader: 'text',
        watchFiles: [args.path],
      };
    });
  },
};

esbuild
  .build({
    entryPoints: ['./source.mjs'],
    bundle: true,
    write: false,
    metafile: true,
    plugins: [myPlugin],
  })
  .then(({metafile}) => {
    console.log(JSON.stringify(metafile, undefined, 2));
  });

source.mjs

import foo from './foo.whatever';

Metafile output vs. expected

  {
    "inputs": {
      "source.mjs": {
        "bytes": 77,
        "imports": []
      },
+     "foo.whatever": {...}
    },
    "outputs": {
      "source.js": {
        "imports": [],
        "exports": [],
        "entryPoint": "source.mjs",
        "inputs": {
          "source.mjs": {
            "bytesInOutput": 132
          }
        },
        "bytes": 191
      }
    }
  }

I understand that the semantics of a watched file and an input file might be different. Regardless of whether they're a part of inputs or not, it would still be super useful if we could obtain returned watchFiles from the build() result.

@evanw
Copy link
Owner

evanw commented Oct 17, 2021

I just looked into this and I think there are two problems with this proposal:

  • Even if you had this information, you still wouldn't be able to watch the correct files since you still wouldn't have esbuild's internal list of paths to watch. I'd like to solve this once with a holistic solution instead of adding this now and then having to change it later.

  • People will want access to this data even if there are build errors. Currently the API returns an error instead of a build result when there are build errors, so putting this information in the build result means that it would be inaccessible if there's an error. The build API really needs to be redesigned to fix this.

See also:

@Kilcekru
Copy link

With version 0.17 context was introduced, is this now possible?

The metafile could have new top level properties watchFiles and watchDirs, so they can be differentiated from normal inputs.
If those paths would be normalized like input paths are, then we would have everything to do custom watching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants