Skip to content

[Bug]: Adding js extensions for index resolution #238

Closed
@Timeless0911

Description

@Timeless0911

Details

input

// folder/index.ts
export const folder = 'folder';
// index.ts
import { folder } from './folder';

export const text = folder;

Output

import * as __WEBPACK_EXTERNAL_MODULE__folder_js__ from "./folder.js";
const src_text = __WEBPACK_EXTERNAL_MODULE__folder_js__.folder;
export { src_text as text };

if (data.contextInfo.issuer) {
// Node.js ECMAScript module loader does no extension searching.
// Add a file extension according to autoExtension config
// when data.request is a relative path and do not have an extension.
// If data.request already have an extension, we replace it with new extension
// This may result in a change in semantics,
// user should use copy to keep origin file or use another separate entry to deal this
let request = data.request;
if (request[0] === '.') {
if (extname(request)) {
if (JS_EXTENSIONS_PATTERN.test(request)) {
request = request.replace(/\.[^.]+$/, jsExtension);
} else {
// If it does not match jsExtensionsPattern, we should do nothing, eg: ./foo.png
return callback();
}
} else {
request = `${request}${jsExtension}`;
}
}
return callback(null, request);
}
callback();
},
],
},
};
};

In line 710, we directly add a jsExtension in bundless mode to make bundleless esm outputs work.

request = `${request}${jsExtension}`;

  • Modern.js Module forces user to add /index in bundleless mode.
  • Tsup do not add js extensions automatically, it forces user to write extensions in sourcecode

Index resolution is a standard behaviour which takes effects in both nodejs and ts, and Rslib now use default resolve.mainFiles: ['index'] of Rspack.

We should handle when ./folder/index.ts and ./folder.ts exists at the same time, there may also be .ts, .tsx, .jsx existing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞 bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions