Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions e2e/cases/bundle-false/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,21 @@ test('auto add extension for relative import', async () => {
const fixturePath = join(__dirname, 'relative-import');
const { contents } = await buildAndGetResults(fixturePath);

expect(Object.values(contents.esm)[1]).toContain(
for (const importer of [
'import * as __WEBPACK_EXTERNAL_MODULE__bar_js__ from "./bar.js";',
);
expect(Object.values(contents.cjs)[1]).toContain(
'import * as __WEBPACK_EXTERNAL_MODULE__baz_js__ from "./baz.js";',
'import * as __WEBPACK_EXTERNAL_MODULE__foo_js__ from "./foo.js";',
'import * as __WEBPACK_EXTERNAL_MODULE__qux_js__ from "./qux.js";',
]) {
expect(Object.values(contents.esm)[3]).toContain(importer);
}

for (const requirer of [
'var external_bar_cjs_namespaceObject = require("./bar.cjs");',
);
'var external_baz_cjs_namespaceObject = require("./baz.cjs");',
'var external_foo_cjs_namespaceObject = require("./foo.cjs");',
'var external_qux_cjs_namespaceObject = require("./qux.cjs");',
]) {
expect(Object.values(contents.cjs)[3]).toContain(requirer);
}
});
1 change: 1 addition & 0 deletions e2e/cases/bundle-false/relative-import/src/baz.js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const baz = 'baz';
1 change: 1 addition & 0 deletions e2e/cases/bundle-false/relative-import/src/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
7 changes: 6 additions & 1 deletion e2e/cases/bundle-false/relative-import/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { bar } from './bar';
import { baz } from './baz.js';
// @ts-ignore
import { foo } from './foo.js';
// @ts-ignore
import { qux } from './qux.cjs';

export const foo = 'foo' + bar;
export const text = foo + bar + baz + qux;
3 changes: 3 additions & 0 deletions e2e/cases/bundle-false/relative-import/src/qux.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
qux: 'qux',
};
21 changes: 13 additions & 8 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs';
import path, { dirname, isAbsolute, join } from 'node:path';
import path, { dirname, extname, isAbsolute, join } from 'node:path';
import {
type RsbuildConfig,
type RsbuildInstance,
Expand Down Expand Up @@ -475,13 +475,18 @@ const composeBundleConfig = (
// Prevent from externalizing entry modules here.
if (data.contextInfo.issuer) {
// Node.js ECMAScript module loader does no extension searching.
// So we add a file extension here when data.request is a relative path
return callback(
null,
data.request[0] === '.'
? `${data.request}${jsExtension}`
: data.request,
);
// 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] === '.') {
request = extname(request)
? request.replace(/\.[^.]+$/, jsExtension)
: `${request}${jsExtension}`;
}
return callback(null, request);
}
callback();
},
Expand Down