Skip to content

[node-resolve] .js files imported from a.ts file gets resolved to .ts extension #853

@motss

Description

@motss

Facing an issue with @web/dev-server at modernweb-dev/web#1376 where the node resolution breaks for dependencies that are using node's exports map, e.g.

// main.ts
import { highlight } from 'nodemod/dist/lib/prismjs.js'; // nodemod@2.7.3 is using `exports` map in node

FYI, nodemod's package.json is as follows:

// nodemod's package.json
...
"exports": {
  "./dist/*": "./dist/*",
  ...
}

What's happening in @web/dev-server is that files are imported with .ts extension due to the following code as pointed out by the author of @web/dev-server:

if (importer && importee.endsWith('.js')) {
for (const ext of ['.ts', '.tsx']) {
if (importer.endsWith(ext) && extensions.includes(ext)) {
importSpecifierList.push(importee.replace(/.js$/, ext));
}
}
}

Expected behavior

import { highlight } from 'nodemod/dist/lib/prismjs.js'; should resolve to 'node_modules/nodemod/dist/lib/prismjs.js';

Actual behavior

import { highlight } from 'nodemod/dist/lib/prismjs.js'; resolves to 'node_modules/nodemod/dist/lib/prismjs.ts';

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions