plugins: Incompatibility with subpath imports

Rollup Version

2.62.0

Operating System (or Browser)

Chrome

Node Version (if applicable)

15.4.0

Link To Reproduction

Check here

Expected Behaviour

Rollup should support by default the subpath imports feature provided NodeJS.

Actual Behaviour

In advance, sorry but this bug requires the package.json and can’t be reproduced without it.

My library takes advantage of the NodeJS subpath imports. My package.json looks like this:

{
  "name": "mylib",
  "version": "0.0.1",
  ...
  "imports": {
    "#db/*": "./db/*.js"
  }
  ...
}

and in my code, I import internal files as follows:

import db from "#db/connect";

Unfortunately, when I try to bundle my code, I get an Unresolved dependencies error from Rollup. It can’t resolve the #db/connect path.

For reference, here is my rollup.config.js:

import resolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";
import babel from "rollup-plugin-babel";
import pkg from "./package.json";

export default [
    {
        input: "src/index.js", // entry point
        output: {
            name: "mylib", // package name
            file: pkg.browser,
            format: "umd",
        },
        plugins: [
            resolve(),
            commonjs(),
            babel({
                exclude: ["node_modules/**"],
            }),
        ],
    },
    {
        input: "src/index.js", // entry point
        output: [
            { file: pkg.main, format: "cjs" },
            { file: pkg.module, format: "es" },
        ],
        plugins: [
            babel({
                exclude: ["node_modules/**"],
            }),
        ],
    },
];

Rollup should support the subpath import feature provided NodeJS by default.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 21 (2 by maintainers)

Most upvoted comments

I dove into this and it seems to be a problem with only some subpath imports.

If you define them as follows:

{
  "imports": {
    "#internal/*": "./src/*.js"
  }
}

…then the node-resolve plugin can understand them. But if you put the extension at the end…

{
  "imports": {
    "#internal/*.js": "./src/*.js"
  }
}

…the plugin doesn’t know what to do.

Consider the resolvePackageImportsExports function. I think it envisions three kinds of import keys: those without wildcards, those that end with wildcards, and those that end with path separators.

But the node docs specifically envision, and even prescribe, keys with wildcards that are followed by the file extension. These are falling through the cracks in resolvePackageImportsExports.

In my case, when I rewrote my internal imports to remove file extension, and rewrote my package.json to look like the first example, everything started working.

@shellscape, I think this needs to be re-opened. The plugin does not understand wildcard subpath imports when the wildcard does not start or end the identifier.

Yeah it seems to really work now 😄 https://replit.com/@OttoKruse/rollup-plugin-repro (same case, slightly tweaked, updated rollup and used right node resolve lib)

@ottokruse we’re very open to contributions for enhancing @rollup/plugin-node-resolve

@shellscape Thanks for the links. I was able to create a reproduction here.

@guybedford Unfortunately, I’m just trying for the first time rollup and don’t have the knowledge to propose a PR.