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

Feature request: add import/require information in plugin OnResolveArgs #879

Closed
yyx990803 opened this issue Feb 24, 2021 · 3 comments
Closed

Comments

@yyx990803
Copy link
Contributor

Currently in the onResolve hook of a plugin, we don't have information on whether the request was made via ESM import or CJS require. This information, however, is somewhat important when resolving Node dependencies, especially those with explicit sub path exports definitions.

Take @babel/runtime as an example: it defines exports for all its helpers, e.g.

"./helpers/interopRequireDefault": [
      {
        "node": "./helpers/interopRequireDefault/index.js",
        "require": "./helpers/interopRequireDefault/index.js",
        "default": "./helpers/interopRequireDefault/_index.mjs"
      },
      "./helpers/interopRequireDefault/index.js"
    ],

It probably does this because it wants to allow users to do both

// gets the cjs version
const foo = require('@babel/runtime/helpers/foo')

// resolves the mjs version
import foo from '@babel/runtime/helpers/foo'

Without the esm vs. cjs information though, the plugin onResolve hook can only guess which one it should resolve to. In the case for Vite, it will prefer ESM entries, so it resolves to the mjs version which, when required, fails to run because the helper is the default export and needs to be accessed as foo.default.

Since esbuild seems to allow the mix of ESM and CJS, it would be beneficial to somehow expose that information to plugins sp that resolvers can properly resolve conditional exports.

@yyx990803 yyx990803 changed the title Feature request: add import/require type in plugin OnResolveArgs Feature request: add import/require information in plugin OnResolveArgs Feb 24, 2021
@evanw evanw closed this as completed in cab83db Feb 24, 2021
@evanw
Copy link
Owner

evanw commented Feb 24, 2021

Makes sense. This is already available internally so exposing this is straightforward. Here's what will be in the next release (from the release notes):

  • Provide kind to onResolve plugins (#879)

    Plugins that add onResolve callbacks now have access to the kind parameter which tells you what kind of import is being resolved. It will be one of the following values:

    • "entry-point" in JS (api.ResolveEntryPoint in Go)

      An entry point provided by the user

    • "import-statement" in JS (api.ResolveJSImportStatement in Go)

      A JavaScript import or export statement

    • "require-call" in JS (api.ResolveJSRequireCall in Go)

      A JavaScript call to require(...) with a string argument

    • "dynamic-import" in JS (api.ResolveJSDynamicImport in Go)

      A JavaScript import(...) expression with a string argument

    • "require-resolve" in JS (api.ResolveJSRequireResolve in Go)

      A JavaScript call to require.resolve(...) with a string argument

    • "import-rule" in JS (api.ResolveCSSImportRule in Go)

      A CSS @import rule

    • "url-token" in JS (api.ResolveCSSURLToken in Go)

      A CSS url(...) token

    These values are pretty much identical to the kind field in the JSON metadata file.

@evanw
Copy link
Owner

evanw commented Feb 25, 2021

FYI this was just published as version 0.8.52.

@yyx990803
Copy link
Contributor Author

Thanks!

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

Successfully merging a pull request may close this issue.

2 participants