Skip to content

Cannot bundle a TS file, which imports JS file from a package using NodeJS conditional exports #911

@bhovhannes

Description

@bhovhannes

Hi,

Issue description

Steps to reproduce

  1. git clone the https://github.com/bhovhannes/rollup-plugin-node-resolve-issue repo
  2. run npm ci
  3. run npm run build

Details

The reproduction link written above is a small Rollup project. Entrypoint - index.ts, is in TypeScript. TypeScript does not really matter here, the only thing which matters is the extension of the file - .ts.

Inside an entrypoint file we import a utility function using import sum from '@abcdef/lib-utils/dist/sum.js' syntax.

The package @abcdef/lib-utils has an exports field in its package.json containing:

"exports": {
    "./dist/*": {
        "import": "./dist_esm/*",
        "require": "./dist/*"
    }
}

That makes possible to let Rollup consume sum.js from dist_esm folder and if the tool does not support ESM (looking on you Jest) it will use sum.js from dist folder.

Lines https://github.com/rollup/plugins/blame/master/packages/node-resolve/src/index.js#L148-L155 in @rollup/plugin-node-resolve code check for the case when there is a .js import inside a .ts file and change extension to .ts. I'll be grateful for any explanation why this is needed.
After that, plugin resolves the package, taking exports field into account and ends up with node_modules/@abcdef/lib-utils/dist_esm/sum.ts url, which obviously does not exist.

Expected Behavior

Build should complete without errors.

Actual Behavior

Build errors out, trying to bundle non-existent TypeScript file. The error:

Error: Could not load /Users/hovhannesbabayan/Projects/github/bhovhannes/
rollup-plugin-node-resolve-issue/node_modules/@abcdef/lib-utils/dist_esm/sum.ts (imported by input.ts): 

ENOENT: no such file or directory, open '/Users/hovhannesbabayan/Projects/github/bhovhannes/
rollup-plugin-node-resolve-issue/node_modules/@abcdef/lib-utils/dist_esm/sum.ts'

Additional Information

Removing lines https://github.com/rollup/plugins/blame/master/packages/node-resolve/src/index.js#L148-L155 fixes the issue.

Or, perhaps we can check if the resolved file indeed exists in https://github.com/rollup/plugins/blob/master/packages/node-resolve/src/package/resolvePackageTarget.js#L13 ?

I'll be happy to submit a pull request, just I am not sure in which direction to proceed.

Thanks!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions