Skip to content

Commit 7f1e1cf

Browse files
authored
feat(rspack): infer ts-node compiler options in rspack task env when using a typescript config file (#30345)
Infer the relevant ts-node compiler options to support an Rspack config file using TypeScript as per https://rspack.dev/config/#using-ts-node. ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes #
1 parent af1b16a commit 7f1e1cf

File tree

2 files changed

+126
-3
lines changed

2 files changed

+126
-3
lines changed

packages/rspack/src/plugins/plugin.spec.ts

+99
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('@nx/rspack', () => {
1212
let createNodesFunction = createNodesV2[1];
1313
let context: CreateNodesContext;
1414
let tempFs: TempFs;
15+
let originalCacheProjectGraph = process.env.NX_CACHE_PROJECT_GRAPH;
1516

1617
beforeEach(() => {
1718
(isUsingTsSolutionSetup as jest.Mock).mockReturnValue(false);
@@ -26,22 +27,120 @@ describe('@nx/rspack', () => {
2627
},
2728
workspaceRoot: tempFs.tempDir,
2829
};
30+
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
2931

3032
tempFs.createFileSync(
3133
'my-app/project.json',
3234
JSON.stringify({ name: 'my-app' })
3335
);
3436
tempFs.createFileSync('my-app/rspack.config.ts', `export default {};`);
37+
tempFs.createFileSync('package-lock.json', `{}`);
3538
});
3639

3740
afterEach(() => {
3841
jest.resetModules();
3942
tempFs.cleanup();
43+
if (originalCacheProjectGraph !== undefined) {
44+
process.env.NX_CACHE_PROJECT_GRAPH = originalCacheProjectGraph;
45+
} else {
46+
delete process.env.NX_CACHE_PROJECT_GRAPH;
47+
}
4048
});
4149

4250
it('should handle missing lock file', async () => {
51+
tempFs.removeFileSync('package-lock.json');
52+
4353
await expect(
4454
createNodesFunction(['my-app/rspack.config.ts'], {}, context)
4555
).resolves.not.toThrow();
4656
});
57+
58+
it('should infer tasks', async () => {
59+
await expect(createNodesFunction(['my-app/rspack.config.ts'], {}, context))
60+
.resolves.toMatchInlineSnapshot(`
61+
[
62+
[
63+
"my-app/rspack.config.ts",
64+
{
65+
"projects": {
66+
"my-app": {
67+
"metadata": {},
68+
"root": "my-app",
69+
"targets": {
70+
"build": {
71+
"cache": true,
72+
"command": "rspack build",
73+
"dependsOn": [
74+
"^build",
75+
],
76+
"inputs": [
77+
"production",
78+
"^production",
79+
{
80+
"externalDependencies": [
81+
"@rspack/cli",
82+
],
83+
},
84+
],
85+
"options": {
86+
"args": [
87+
"--node-env=production",
88+
],
89+
"cwd": "my-app",
90+
"env": {
91+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"Node10","module":"CommonJS"}",
92+
},
93+
},
94+
"outputs": [],
95+
},
96+
"build-deps": {
97+
"dependsOn": [
98+
"^build",
99+
],
100+
},
101+
"preview": {
102+
"command": "rspack serve",
103+
"options": {
104+
"args": [
105+
"--node-env=production",
106+
],
107+
"cwd": "my-app",
108+
"env": {
109+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"Node10","module":"CommonJS"}",
110+
},
111+
},
112+
},
113+
"serve": {
114+
"command": "rspack serve",
115+
"options": {
116+
"args": [
117+
"--node-env=development",
118+
],
119+
"cwd": "my-app",
120+
"env": {
121+
"TS_NODE_COMPILER_OPTIONS": "{"moduleResolution":"Node10","module":"CommonJS"}",
122+
},
123+
},
124+
},
125+
"serve-static": {
126+
"executor": "@nx/web:file-server",
127+
"options": {
128+
"buildTarget": "build",
129+
"spa": true,
130+
},
131+
},
132+
"watch-deps": {
133+
"command": "npx nx watch --projects my-app --includeDependentProjects -- npx nx build-deps my-app",
134+
"dependsOn": [
135+
"build-deps",
136+
],
137+
},
138+
},
139+
},
140+
},
141+
},
142+
],
143+
]
144+
`);
145+
});
47146
});

packages/rspack/src/plugins/plugin.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-
1616
import { existsSync, readdirSync } from 'fs';
1717
import { hashArray, hashFile, hashObject } from 'nx/src/hasher/file-hasher';
1818
import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
19-
import { dirname, isAbsolute, join, relative, resolve } from 'path';
19+
import { dirname, extname, isAbsolute, join, relative, resolve } from 'path';
2020
import { readRspackOptions } from '../utils/read-rspack-options';
2121
import { resolveUserDefinedRspackConfig } from '../utils/resolve-user-defined-rspack-config';
2222
import { addBuildAndWatchDepsTargets } from '@nx/js/src/plugins/typescript/util';
@@ -35,7 +35,13 @@ type RspackTargets = Pick<ProjectConfiguration, 'targets' | 'metadata'>;
3535
const pmc = getPackageManagerCommand();
3636

3737
function readTargetsCache(cachePath: string): Record<string, RspackTargets> {
38-
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
38+
try {
39+
return process.env.NX_CACHE_PROJECT_GRAPH !== 'false'
40+
? readJsonFile(cachePath)
41+
: {};
42+
} catch {
43+
return {};
44+
}
3945
}
4046

4147
function writeTargetsToCache(
@@ -172,9 +178,25 @@ async function createRspackTargets(
172178

173179
const targets = {};
174180

181+
const env: NodeJS.ProcessEnv = {};
182+
const isTsConfig = ['.ts', '.cts', '.mts'].includes(extname(configFilePath));
183+
if (isTsConfig) {
184+
// https://rspack.dev/config/#using-ts-node
185+
const existingValue = process.env['TS_NODE_COMPILER_OPTIONS'];
186+
env['TS_NODE_COMPILER_OPTIONS'] = JSON.stringify({
187+
...(existingValue ? JSON.parse(existingValue) : {}),
188+
module: 'CommonJS',
189+
moduleResolution: 'Node10',
190+
});
191+
}
192+
175193
targets[options.buildTargetName] = {
176194
command: `rspack build`,
177-
options: { cwd: projectRoot, args: ['--node-env=production'] },
195+
options: {
196+
cwd: projectRoot,
197+
args: ['--node-env=production'],
198+
env,
199+
},
178200
cache: true,
179201
dependsOn: [`^${options.buildTargetName}`],
180202
inputs:
@@ -201,6 +223,7 @@ async function createRspackTargets(
201223
options: {
202224
cwd: projectRoot,
203225
args: ['--node-env=development'],
226+
env,
204227
},
205228
};
206229

@@ -209,6 +232,7 @@ async function createRspackTargets(
209232
options: {
210233
cwd: projectRoot,
211234
args: ['--node-env=production'],
235+
env,
212236
},
213237
};
214238

0 commit comments

Comments
 (0)