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
42 changes: 42 additions & 0 deletions e2e/cases/extension-alias/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`resolve.extensionAlias should work 1`] = `
"\\"use strict\\";
var __webpack_exports__ = {};

;// CONCATENATED MODULE: ./src/bar.js
const bar = 'bar';

;// CONCATENATED MODULE: ./src/foo.ts
const foo = 'foo';

;// CONCATENATED MODULE: ./src/index.ts
// Relative import paths need explicit file extensions in ECMAScript imports
// when '--moduleResolution' is 'node16' or 'nodenext'.


console.log(foo + bar);

var __webpack_export_target__ = exports;
for(var i in __webpack_exports__) __webpack_export_target__[i] = __webpack_exports__[i];
if(__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', { value: true });
"
`;

exports[`resolve.extensionAlias should work 2`] = `
"
;// CONCATENATED MODULE: ./src/bar.js
const bar = 'bar';

;// CONCATENATED MODULE: ./src/foo.ts
const foo = 'foo';

;// CONCATENATED MODULE: ./src/index.ts
// Relative import paths need explicit file extensions in ECMAScript imports
// when '--moduleResolution' is 'node16' or 'nodenext'.


console.log(foo + bar);

"
`;
10 changes: 10 additions & 0 deletions e2e/cases/extension-alias/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { buildAndGetResults } from '@e2e/helper';
import { expect, test } from 'vitest';

test('resolve.extensionAlias should work', async () => {
const fixturePath = __dirname;
const { entries } = await buildAndGetResults(fixturePath);

expect(entries.cjs).toMatchSnapshot();
expect(entries.esm).toMatchSnapshot();
});
6 changes: 6 additions & 0 deletions e2e/cases/extension-alias/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "extension-alias-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
14 changes: 14 additions & 0 deletions e2e/cases/extension-alias/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [generateBundleEsmConfig(), generateBundleCjsConfig()],
source: {
entry: {
index: './src/index.ts',
},
alias: {
'@src': 'src',
},
},
});
1 change: 1 addition & 0 deletions e2e/cases/extension-alias/src/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const bar = 'bar';
1 change: 1 addition & 0 deletions e2e/cases/extension-alias/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
6 changes: 6 additions & 0 deletions e2e/cases/extension-alias/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Relative import paths need explicit file extensions in ECMAScript imports
// when '--moduleResolution' is 'node16' or 'nodenext'.
import { bar } from './bar.js';
import { foo } from './foo.js';

console.log(foo + bar);
7 changes: 7 additions & 0 deletions e2e/cases/extension-alias/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "Node16",
"moduleResolution": "Node16"
},
"include": ["src"]
}
10 changes: 10 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ export async function createInternalRsbuildConfig(): Promise<RsbuildConfig> {
},
},
},
// TypeScript-specific behavior: if the extension is ".js" or ".jsx", try replacing it with ".ts" or ".tsx"
// see https://github.com/web-infra-dev/rslib/issues/41
resolve: {
extensionAlias: {
'.js': ['.ts', '.tsx', '.js', '.jsx'],
'.jsx': ['.tsx', '.jsx'],
'.mjs': ['.mts', '.mjs'],
'.cjs': ['.cts', '.cjs'],
},
},
},
},
output: {
Expand Down
66 changes: 66 additions & 0 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"optimization": {
"moduleIds": "named",
},
"resolve": {
"extensionAlias": {
".cjs": [
".cts",
".cjs",
],
".js": [
".ts",
".tsx",
".js",
".jsx",
],
".jsx": [
".tsx",
".jsx",
],
".mjs": [
".mts",
".mjs",
],
},
},
},
],
},
Expand Down Expand Up @@ -154,6 +176,28 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"optimization": {
"moduleIds": "named",
},
"resolve": {
"extensionAlias": {
".cjs": [
".cts",
".cjs",
],
".js": [
".ts",
".tsx",
".js",
".jsx",
],
".jsx": [
".tsx",
".jsx",
],
".mjs": [
".mts",
".mjs",
],
},
},
},
],
},
Expand Down Expand Up @@ -221,6 +265,28 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"optimization": {
"moduleIds": "named",
},
"resolve": {
"extensionAlias": {
".cjs": [
".cts",
".cjs",
],
".js": [
".ts",
".tsx",
".js",
".jsx",
],
".jsx": [
".tsx",
".jsx",
],
".mjs": [
".mts",
".mjs",
],
},
},
},
],
},
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mjsx
modularly
mtsx
napi
nodenext
nolyfill
ntqry
onclosetag
Expand Down