Skip to content

Commit 8e52325

Browse files
authored
fix: resolve project's tsconfig path correctly (#540)
1 parent 96f5364 commit 8e52325

File tree

9 files changed

+91
-24
lines changed

9 files changed

+91
-24
lines changed

e2e/projects/fixtures/packages/node/test/index.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { sayHi } from '../src/index';
1+
import { sayHi } from '@/src';
22

33
it('should test source code correctly', () => {
44
expect(sayHi()).toBe('hi');
55
});
66

77
it('should can not get document', () => {
8+
// @ts-expect-error
89
expect(global.document).toBeUndefined();
910
});
1011

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"lib": ["ES2020"],
5+
"module": "ESNext",
6+
"strict": true,
7+
"skipLibCheck": true,
8+
"isolatedModules": true,
9+
"resolveJsonModule": true,
10+
"moduleResolution": "bundler",
11+
"useDefineForClassFields": true,
12+
"paths": {
13+
"@/src": ["./src"]
14+
}
15+
},
16+
"include": ["src", "test"]
17+
}

e2e/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"allowImportingTsExtensions": true
1010
},
1111
"include": ["./"],
12-
"exclude": ["vue/"]
12+
"exclude": ["vue/", "projects/fixtures/"]
1313
}

packages/core/src/core/plugins/basic.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const pluginBasic: (context: RstestContext) => RsbuildPlugin = (
5555
},
5656
tools: {
5757
rspack: (config, { isProd, rspack }) => {
58+
config.context = rootPath;
5859
// treat `test` as development mode
5960
config.mode = isProd ? 'production' : 'development';
6061
config.output ??= {};

packages/core/src/core/rsbuild.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const prepareRsbuild = async (
7575
const rsbuildInstance = await createRsbuild({
7676
callerName: 'rstest',
7777
rsbuildConfig: {
78+
root: context.rootPath,
7879
server: {
7980
printUrls: false,
8081
strictPort: false,

packages/core/src/core/rstest.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { existsSync } from 'node:fs';
12
import { SnapshotManager } from '@vitest/snapshot/manager';
3+
import { join } from 'pathe';
24
import { isCI } from 'std-env';
35
import { withDefaultConfig } from '../config';
46
import { DefaultReporter } from '../reporter';
@@ -18,7 +20,7 @@ import type {
1820
TestFileResult,
1921
TestResult,
2022
} from '../types';
21-
import { castArray, getAbsolutePath } from '../utils/helper';
23+
import { castArray, getAbsolutePath, TS_CONFIG_FILE } from '../utils';
2224

2325
/**
2426
* Only letters, numbers, "-", "_", and "$" are allowed.
@@ -99,6 +101,15 @@ export class Rstest implements RstestContext {
99101
project.config,
100102
) as NormalizedProjectConfig;
101103
config.isolate = rstestConfig.isolate;
104+
config.source ??= {};
105+
106+
if (!config.source.tsconfigPath) {
107+
const tsconfigPath = join(config.root, TS_CONFIG_FILE);
108+
109+
if (existsSync(tsconfigPath)) {
110+
config.source.tsconfigPath = tsconfigPath;
111+
}
112+
}
102113
return {
103114
configFilePath: project.configFilePath,
104115
rootPath: config.root,

packages/core/src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ export const globalApis: (keyof Rstest)[] = [
3232
'rs',
3333
'assert',
3434
];
35+
36+
export const TS_CONFIG_FILE = 'tsconfig.json';

packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] = `
44
{
5-
"context": "<ROOT>",
5+
"context": "<ROOT>/packages/core",
66
"devtool": "source-map",
77
"entry": {},
88
"experiments": {
@@ -152,7 +152,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] =
152152
"isModule": "unknown",
153153
"jsc": {
154154
"experimental": {
155-
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
155+
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
156156
"keepImportAttributes": true,
157157
},
158158
"externalHelpers": true,
@@ -206,7 +206,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] =
206206
"isModule": "unknown",
207207
"jsc": {
208208
"experimental": {
209-
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
209+
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
210210
"keepImportAttributes": true,
211211
},
212212
"externalHelpers": true,
@@ -413,7 +413,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] =
413413
"library": {
414414
"type": "commonjs2",
415415
},
416-
"path": "<ROOT>/dist/.rstest-temp",
416+
"path": "<ROOT>/packages/core/dist/.rstest-temp",
417417
"pathinfo": false,
418418
"publicPath": "/",
419419
"webassemblyModuleFilename": "static/wasm/[contenthash:8].module.wasm",
@@ -469,7 +469,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] =
469469
"hoistMockModule": true,
470470
"importMetaPathName": true,
471471
"injectModulePathName": true,
472-
"manualMockRoot": "<ROOT>/__mocks__",
472+
"manualMockRoot": "<ROOT>/packages/core/__mocks__",
473473
},
474474
],
475475
"affectedHooks": undefined,
@@ -508,6 +508,10 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] =
508508
".json",
509509
".cjs",
510510
],
511+
"tsConfig": {
512+
"configFile": "<ROOT>/packages/core/tsconfig.json",
513+
"references": "auto",
514+
},
511515
},
512516
"target": "node",
513517
"watch": false,
@@ -520,7 +524,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] =
520524

521525
exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = `
522526
{
523-
"context": "<ROOT>",
527+
"context": "<ROOT>/packages/core",
524528
"devtool": "source-map",
525529
"entry": {},
526530
"experiments": {
@@ -671,7 +675,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = `
671675
"isModule": "unknown",
672676
"jsc": {
673677
"experimental": {
674-
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
678+
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
675679
"keepImportAttributes": true,
676680
},
677681
"externalHelpers": true,
@@ -725,7 +729,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = `
725729
"isModule": "unknown",
726730
"jsc": {
727731
"experimental": {
728-
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
732+
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
729733
"keepImportAttributes": true,
730734
},
731735
"externalHelpers": true,
@@ -921,7 +925,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = `
921925
"library": {
922926
"type": "commonjs2",
923927
},
924-
"path": "<ROOT>/dist/.rstest-temp",
928+
"path": "<ROOT>/packages/core/dist/.rstest-temp",
925929
"pathinfo": false,
926930
"publicPath": "/",
927931
"webassemblyModuleFilename": "static/wasm/[contenthash:8].module.wasm",
@@ -976,7 +980,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = `
976980
"hoistMockModule": true,
977981
"importMetaPathName": true,
978982
"injectModulePathName": true,
979-
"manualMockRoot": "<ROOT>/__mocks__",
983+
"manualMockRoot": "<ROOT>/packages/core/__mocks__",
980984
},
981985
],
982986
"affectedHooks": undefined,
@@ -1018,6 +1022,10 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = `
10181022
"mainFields": [
10191023
"main",
10201024
],
1025+
"tsConfig": {
1026+
"configFile": "<ROOT>/packages/core/tsconfig.json",
1027+
"references": "auto",
1028+
},
10211029
},
10221030
"target": "node",
10231031
"watch": false,
@@ -1030,7 +1038,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = `
10301038

10311039
exports[`prepareRsbuild > should generate rspack config correctly with projects 1`] = `
10321040
{
1033-
"context": "<ROOT>",
1041+
"context": "<ROOT>/packages/core",
10341042
"devtool": "source-map",
10351043
"entry": {},
10361044
"experiments": {
@@ -1180,7 +1188,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
11801188
"isModule": "unknown",
11811189
"jsc": {
11821190
"experimental": {
1183-
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
1191+
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
11841192
"keepImportAttributes": true,
11851193
},
11861194
"externalHelpers": true,
@@ -1234,7 +1242,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
12341242
"isModule": "unknown",
12351243
"jsc": {
12361244
"experimental": {
1237-
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
1245+
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
12381246
"keepImportAttributes": true,
12391247
},
12401248
"externalHelpers": true,
@@ -1441,7 +1449,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
14411449
"library": {
14421450
"type": "commonjs2",
14431451
},
1444-
"path": "<ROOT>/dist/.rstest-temp/test",
1452+
"path": "<ROOT>/packages/core/dist/.rstest-temp/test",
14451453
"pathinfo": false,
14461454
"publicPath": "/",
14471455
"webassemblyModuleFilename": "static/wasm/[contenthash:8].module.wasm",
@@ -1497,7 +1505,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
14971505
"hoistMockModule": true,
14981506
"importMetaPathName": true,
14991507
"injectModulePathName": true,
1500-
"manualMockRoot": "<ROOT>/__mocks__",
1508+
"manualMockRoot": "<ROOT>/packages/core/__mocks__",
15011509
},
15021510
],
15031511
"affectedHooks": undefined,
@@ -1536,6 +1544,10 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
15361544
".json",
15371545
".cjs",
15381546
],
1547+
"tsConfig": {
1548+
"configFile": "<ROOT>/packages/core/tsconfig.json",
1549+
"references": "auto",
1550+
},
15391551
},
15401552
"target": "node",
15411553
"watch": false,
@@ -1548,7 +1560,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
15481560

15491561
exports[`prepareRsbuild > should generate rspack config correctly with projects 2`] = `
15501562
{
1551-
"context": "<ROOT>",
1563+
"context": "<ROOT>/packages/core",
15521564
"devtool": "source-map",
15531565
"entry": {},
15541566
"experiments": {
@@ -1699,7 +1711,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
16991711
"isModule": "unknown",
17001712
"jsc": {
17011713
"experimental": {
1702-
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
1714+
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
17031715
"keepImportAttributes": true,
17041716
},
17051717
"externalHelpers": true,
@@ -1753,7 +1765,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
17531765
"isModule": "unknown",
17541766
"jsc": {
17551767
"experimental": {
1756-
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
1768+
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
17571769
"keepImportAttributes": true,
17581770
},
17591771
"externalHelpers": true,
@@ -1960,7 +1972,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
19601972
"library": {
19611973
"type": "commonjs2",
19621974
},
1963-
"path": "<ROOT>/dist/.rstest-temp/test-node",
1975+
"path": "<ROOT>/packages/core/dist/.rstest-temp/test-node",
19641976
"pathinfo": false,
19651977
"publicPath": "/",
19661978
"webassemblyModuleFilename": "static/wasm/[contenthash:8].module.wasm",
@@ -2016,7 +2028,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
20162028
"hoistMockModule": true,
20172029
"importMetaPathName": true,
20182030
"injectModulePathName": true,
2019-
"manualMockRoot": "<ROOT>/__mocks__",
2031+
"manualMockRoot": "<ROOT>/packages/core/__mocks__",
20202032
},
20212033
],
20222034
"affectedHooks": undefined,
@@ -2058,6 +2070,10 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
20582070
"mainFields": [
20592071
"main",
20602072
],
2073+
"tsConfig": {
2074+
"configFile": "<ROOT>/packages/core/tsconfig.json",
2075+
"references": "auto",
2076+
},
20612077
},
20622078
"target": "node",
20632079
"watch": false,
@@ -2104,7 +2120,7 @@ exports[`prepareRsbuild > should generate swc config correctly with user customi
21042120
"isModule": "unknown",
21052121
"jsc": {
21062122
"experimental": {
2107-
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
2123+
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
21082124
"keepImportAttributes": true,
21092125
},
21102126
"externalHelpers": true,

0 commit comments

Comments
 (0)