Skip to content

node-resolve: “browser” field not being used for sub imports in dependencies #781

Closed
@mikeal

Description

@mikeal
  • Rollup Plugin Name: node-resolve
  • Rollup Plugin Version: 11.1.1
  • Rollup Version: 2.38.1
  • Operating System (or Browser): linux
  • Node Version: 14.x
  • Link to reproduction (⚠️ read below):

This can’t be reproduced using the above guidelines because it requires a dependency be installed via Node.js that is importing a sub export in an export map that has both “import” and “browser” fields.

The issue is simple to explain. I have a dependency installed that is written in native ESM. One of the exports in the export
map has an import and a browser field. If the plugin applied the same logic as the main field then I would get back the browser entrypoint w/ { browser: true } configured. But I don’t, I get back the import target which is intended for Node.js.

I’ve tracked the issue all the way down and it’s here https://github.com/rollup/plugins/blob/master/packages/node-resolve/src/package/resolvePackageTarget.js#L88-L105

  if (target && typeof target === 'object') {
    for (const [key, value] of Object.entries(target)) {
      if (key === 'default' || context.conditions.includes(key)) {
        const resolved = await resolvePackageTarget(context, {
          target: value,
          subpath,
          pattern,
          internal
        });

        // return if defined or null, but not undefined
        if (resolved !== undefined) {
          return resolved;
        }
      }
    }
    return undefined;
  }

A few problems:

  1. “browser” is never added by this plugin to context.conditions. I can work around this locally by adding a config setting of { exportConditions: [ “browser” ] } but that still leaves me with the second problem.

  2. This resolution logic is applied in the order it is defined in package.json rather than the order defined by the conditions. This is really not what you want, you want to configure the preferred ordering in the compiler and treat the export map as unordered. It’s just generally a bad idea to rely on map ordering.

While I can tell you for certain that this is where the bug is, I’m not sure this is where you want to fix it. This module is rather complicated and there’s a lot of userBrowserOverride logic in other places that just isn’t applied when you’re importing something other than the main field.

Expected Behavior

Read above.

Actual Behavior

Read above.

Additional Information

This complicated issue template is a big barrier to entry for logging issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions