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

resolveDir returned by load in virtual module is modified when passed to resolve callbacks #696

Closed
hardfist opened this issue Jan 20, 2021 · 6 comments

Comments

@hardfist
Copy link
Contributor

I write an real world esbuild unpkg plugin which simulates deno bundle, which reference https://esbuild.github.io/plugins/#http-plugin , It needs to deal with http redirect response.

import { Plugin } from 'esbuild';
import fetch from 'node-fetch';
const UNPKG_HOST = 'https://unpkg.apps.bytedance.net/';

async function fetchPkg(url: string) {
  const res = await fetch(url);
  return {
    url: res.url,
    content: await res.text(),
  };
}
export const pluginHttp = (): Plugin => {
  return {
    name: 'http',
    setup(build) {
      build.onResolve({ filter: /^https?:\/\// }, async (args) => {
        return {
          namespace: 'http-url',
          path: args.path,
        };
      });
      build.onResolve({ filter: /.*/, namespace: 'http-url' }, async (args) => {
        console.log('args:', args);
        return undefined;
      });
      build.onLoad({ filter: /.*/, namespace: 'http-url' }, async (args) => {
        const { content, url } = await fetchPkg(args.path);
        console.log('url:', url);
        return {
          contents: content,
          loader: 'ts',
          resolveDir: url,
        };
      });
    },
  };
};

even though I pass resolveDir(https://unpkg.com/[email protected]/add.js) in load results ,but I got resolveUrl(resolveDir: '/Users/admin/github/neo/packages/bundler/https:/unpkg.com/[email protected]/add.js') in resolve callback,which is unexpected。

@evanw
Copy link
Owner

evanw commented Jan 21, 2021

The resolve directory is intended for use with file system paths, and is being turned into a file system path. I believe it should be possible to implement this yourself without needing the resolve directory to behave differently.

Is it possible for you to solve this yourself by storing the original URL to redirect URL mapping in a map inside your plugin and then checking that map later on when resolving further imports?

@hardfist
Copy link
Contributor Author

yes,it can be done,I just think whether is there a better way to do this,for complex virtual module it would be better if there's some way to pass meta info other than dirinfo between load and resolve hook

@hardfist
Copy link
Contributor Author

@evanw
Copy link
Owner

evanw commented Jan 22, 2021

But in this case the load and resolve hook are part of the same plugin, and the path and namespace form a unique key for that information. So a map seems to me like the simplest and most direct solution in this case.

@hardfist
Copy link
Contributor Author

yes,if it happens in same plugin,closure map shuld work,but if it happens in different plugin,it would be kind of trciky to do that

@evanw
Copy link
Owner

evanw commented Jan 30, 2021

I'm adding a new pluginData field that can take arbitrary data and that is not modified by esbuild. I believe that should be sufficient for this use case.

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

2 participants