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

stdin not supports onLoad && onResolve plugin #720

Closed
hardfist opened this issue Jan 28, 2021 · 5 comments
Closed

stdin not supports onLoad && onResolve plugin #720

hardfist opened this issue Jan 28, 2021 · 5 comments

Comments

@hardfist
Copy link
Contributor

It seems that stdin contents isn't handled by onLoad && onResolve plugin, and it seems doesn't support namespace too

@zaydek
Copy link

zaydek commented Jan 28, 2021

@hardfist I’d recommend adding some reproducible code snippets -- whatever you tried for example.

@hardfist
Copy link
Contributor Author

hardfist commented Jan 28, 2021

if i pass entry throungh entrypoints, load and resolve plugin are called, but when i pass entry through stdin load and resolve plugins are not called

 const result = await build({
    // entryPoints: [path.join(__dirname, 'src/index.js')],  // resolve and load plugin are called
    stdin: { // resolve and load plugin are not called
      contents: 'export default 42',
      loader: 'js',
      resolveDir: '',
      sourcefile: 'test.js',
    },
    bundle: true,
    format: 'esm',
    splitting: true,
    outdir: 'dist',
    write: false,
    plugins: [
      {
        name: 'esbuild',
        setup(build) {
          build.onResolve({ filter: /.*/ }, (args) => {
            console.log('load args:', args);
          });
          build.onLoad({ filter: /.*/ }, (args) => {
            console.log('resolve args2:', args);
          });
        },
      },
    ],
  });

src/index.js

export default 42

@evanw
Copy link
Owner

evanw commented Jan 28, 2021

If I'm understanding your issue correctly, this is by design. The stdin API is a convenience API and has limited features. If you need the behavior I think you're asking for, you should be able to implement it yourself:

const result = await build({
  entryPoints: ['<stdin>'],
  bundle: true,
  format: 'esm',
  splitting: true,
  outdir: 'dist',
  write: false,
  plugins: [
    {
      name: 'esbuild',
      setup(build) {
        build.onResolve({ filter: /^<stdin>$/ }, (args) => {
          return { path: 'test.js', namespace: 'stdin' };
        });
        build.onLoad({ filter: /.*/, namespace: 'stdin' }, (args) => {
          return { contents: 'export default 42', loader: 'js' };
        });
      },
    },
  ],
});

@hardfist
Copy link
Contributor Author

If I'm understanding your issue correctly, this is by design. The stdin API is a convenience API and has limited features. If you need the behavior I think you're asking for, you should be able to implement it yourself:

const result = await build({
  entryPoints: ['<stdin>'],
  bundle: true,
  format: 'esm',
  splitting: true,
  outdir: 'dist',
  write: false,
  plugins: [
    {
      name: 'esbuild',
      setup(build) {
        build.onResolve({ filter: /^<stdin>$/ }, (args) => {
          return { path: 'test.js', namespace: 'stdin' };
        });
        build.onLoad({ filter: /.*/, namespace: 'stdin' }, (args) => {
          return { contents: 'export default 42', loader: 'js' };
        });
      },
    },
  ],
});

yes, It works , but kind of awkard, If it's by design, then I think it's ok。

@evanw
Copy link
Owner

evanw commented May 14, 2021

Closing since this is by design, and there is a workaround.

@evanw evanw closed this as completed May 14, 2021
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

3 participants