Skip to content

Commit 89ab887

Browse files
authored
fix(core): Update bundlers to not typecheck if using new TS solution setup (#29227)
If we are using the new TS setup we should opt out of doing type checking during build since we already have a typecheck target and it may lead to doing type checking twice.
1 parent 04151ca commit 89ab887

File tree

13 files changed

+47
-14
lines changed

13 files changed

+47
-14
lines changed

packages/esbuild/src/executors/esbuild/esbuild.impl.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ export async function* esbuildExecutor(
133133
name: 'nx-watch-plugin',
134134
setup(build: esbuild.PluginBuild) {
135135
build.onEnd(async (result: esbuild.BuildResult) => {
136-
if (!options.skipTypeCheck) {
136+
if (
137+
!options.skipTypeCheck &&
138+
!options.isTsSolutionSetup
139+
) {
137140
const { errors } = await runTypeCheck(
138141
options,
139142
context
@@ -180,7 +183,7 @@ export async function* esbuildExecutor(
180183
);
181184
} else {
182185
// Run type-checks first and bail if they don't pass.
183-
if (!options.skipTypeCheck) {
186+
if (!options.skipTypeCheck && !options.isTsSolutionSetup) {
184187
const { errors } = await runTypeCheck(options, context);
185188
if (errors.length > 0) {
186189
yield { success: false };

packages/esbuild/src/executors/esbuild/lib/normalize.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ describe('normalizeOptions', () => {
6666
singleEntry: true,
6767
external: [],
6868
thirdParty: false,
69+
isTsSolutionSetup: false,
6970
});
7071
});
7172

@@ -93,6 +94,7 @@ describe('normalizeOptions', () => {
9394
singleEntry: false,
9495
external: [],
9596
thirdParty: false,
97+
isTsSolutionSetup: false,
9698
});
9799
});
98100

@@ -119,6 +121,7 @@ describe('normalizeOptions', () => {
119121
singleEntry: true,
120122
external: [],
121123
thirdParty: false,
124+
isTsSolutionSetup: false,
122125
});
123126
});
124127

@@ -162,6 +165,7 @@ describe('normalizeOptions', () => {
162165
singleEntry: true,
163166
external: [],
164167
thirdParty: false,
168+
isTsSolutionSetup: false,
165169
});
166170
});
167171

packages/esbuild/src/executors/esbuild/lib/normalize.ts

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export function normalizeOptions(
110110
userDefinedBuildOptions,
111111
external: options.external ?? [],
112112
singleEntry: false,
113+
isTsSolutionSetup,
113114
// Use the `main` file name as the output file name.
114115
// This is needed for `@nx/js:node` to know the main file to execute.
115116
// NOTE: The .js default extension may be replaced later in getOutfile() call.
@@ -126,6 +127,7 @@ export function normalizeOptions(
126127
userDefinedBuildOptions,
127128
external: options.external ?? [],
128129
singleEntry: true,
130+
isTsSolutionSetup,
129131
outputFileName:
130132
// NOTE: The .js default extension may be replaced later in getOutfile() call.
131133
options.outputFileName ?? `${path.parse(options.main).name}.js`,

packages/esbuild/src/executors/esbuild/schema.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ export interface NormalizedEsBuildExecutorOptions
3636
singleEntry: boolean;
3737
external: string[];
3838
userDefinedBuildOptions: esbuild.BuildOptions | undefined;
39+
isTsSolutionSetup?: boolean;
3940
}

packages/js/src/executors/swc/swc.impl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function normalizeOptions(
5656

5757
const outputPath = join(root, options.outputPath);
5858

59-
if (options.skipTypeCheck == null) {
59+
if (options.skipTypeCheck == null && !isTsSolutionSetup) {
6060
options.skipTypeCheck = false;
6161
}
6262

packages/js/src/generators/library/library.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,10 @@ async function configureProject(
297297
projectConfiguration.targets.build.options.format = ['cjs'];
298298
}
299299

300-
if (options.bundler === 'swc' && options.skipTypeCheck) {
300+
if (
301+
options.bundler === 'swc' &&
302+
(options.skipTypeCheck || options.isUsingTsSolutionConfig)
303+
) {
301304
projectConfiguration.targets.build.options.skipTypeCheck = true;
302305
}
303306

@@ -783,7 +786,7 @@ async function normalizeOptions(
783786

784787
if (
785788
(options.bundler === 'swc' || options.bundler === 'rollup') &&
786-
options.skipTypeCheck == null
789+
(options.skipTypeCheck == null || !isUsingTsSolutionConfig)
787790
) {
788791
options.skipTypeCheck = false;
789792
}

packages/js/src/utils/package-manager-workspaces.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ export function isWorkspacesEnabled(
5151
}
5252

5353
// yarn and npm both use the same 'workspaces' property in package.json
54-
const packageJson = readJson<PackageJson>(tree, 'package.json');
55-
return !!packageJson?.workspaces;
54+
if (tree.exists('package.json')) {
55+
const packageJson = readJson<PackageJson>(tree, 'package.json');
56+
return !!packageJson?.workspaces;
57+
}
58+
return false;
5659
}
5760

5861
export function getProjectPackageManagerWorkspaceStateWarningTask(

packages/js/src/utils/swc/compile-swc.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export async function compileSwc(
9090
logger.log(swcCmdLog.replace(/\n/, ''));
9191
const isCompileSuccess = swcCmdLog.includes('Successfully compiled');
9292

93-
if (normalizedOptions.skipTypeCheck) {
93+
if (normalizedOptions.skipTypeCheck || normalizedOptions.isTsSolutionSetup) {
9494
await postCompilationCallback();
9595
return { success: isCompileSuccess };
9696
}
@@ -159,7 +159,10 @@ export async function* compileSwcWatch(
159159
initialPostCompile = false;
160160
}
161161

162-
if (normalizedOptions.skipTypeCheck) {
162+
if (
163+
normalizedOptions.skipTypeCheck ||
164+
normalizedOptions.isTsSolutionSetup
165+
) {
163166
next(getResult(swcStatus));
164167
return;
165168
}

packages/rollup/src/executors/rollup/lib/normalize.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from 'path';
22
import { ExecutorContext } from '@nx/devkit';
33

44
import type { RollupExecutorOptions } from '../schema';
5+
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
56

67
export interface NormalizedRollupExecutorOptions extends RollupExecutorOptions {
78
projectRoot: string;
@@ -13,14 +14,15 @@ export function normalizeRollupExecutorOptions(
1314
context: ExecutorContext
1415
): NormalizedRollupExecutorOptions {
1516
const { root } = context;
17+
const skipTypeCheck = isUsingTsSolutionSetup() ? true : options.skipTypeCheck;
1618
return {
1719
...options,
1820
rollupConfig: []
1921
.concat(options.rollupConfig)
2022
.filter(Boolean)
2123
.map((p) => normalizePluginPath(p, root)),
2224
projectRoot: context.projectGraph.nodes[context.projectName].data.root,
23-
skipTypeCheck: options.skipTypeCheck || false,
25+
skipTypeCheck: skipTypeCheck || false,
2426
};
2527
}
2628

packages/rollup/src/plugins/with-nx/normalize-options.ts

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
RollupWithNxPluginOptions,
88
} from './with-nx-options';
99
import { createEntryPoints } from '@nx/js';
10+
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
1011

1112
export function normalizeOptions(
1213
projectRoot: string,
@@ -16,6 +17,12 @@ export function normalizeOptions(
1617
if (global.NX_GRAPH_CREATION)
1718
return options as NormalizedRollupWithNxPluginOptions;
1819
normalizeRelativePaths(projectRoot, options);
20+
21+
// New TS Solution already has a typecheck target
22+
if (isUsingTsSolutionSetup()) {
23+
options.skipTypeCheck = true;
24+
}
25+
1926
return {
2027
...options,
2128
additionalEntryPoints: createEntryPoints(

packages/rspack/src/plugins/utils/apply-base-config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { NxTsconfigPathsRspackPlugin } from './plugins/nx-tsconfig-paths-rspack-
1818
import { getTerserEcmaVersion } from './get-terser-ecma-version';
1919
import nodeExternals = require('webpack-node-externals');
2020
import { NormalizedNxAppRspackPluginOptions } from './models';
21+
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
2122

2223
const IGNORED_RSPACK_WARNINGS = [
2324
/The comment file/i,
@@ -226,7 +227,8 @@ function applyNxDependentConfig(
226227

227228
plugins.push(new NxTsconfigPathsRspackPlugin({ ...options, tsConfig }));
228229

229-
if (!options?.skipTypeChecking) {
230+
// New TS Solution already has a typecheck target
231+
if (!options?.skipTypeChecking && !isUsingTsSolutionSetup()) {
230232
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
231233
plugins.push(
232234
new ForkTsCheckerWebpackPlugin({

packages/vite/src/executors/build/build.impl.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
validateTypes,
2828
} from '../../utils/executor-utils';
2929
import { type Plugin } from 'vite';
30+
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
3031

3132
export async function* viteBuildExecutor(
3233
options: Record<string, any> & ViteBuildExecutorOptions,
@@ -85,8 +86,8 @@ export async function* viteBuildExecutor(
8586
...otherOptions,
8687
}
8788
);
88-
89-
if (!options.skipTypeCheck) {
89+
// New TS Solution already has a typecheck target
90+
if (!options.skipTypeCheck && !isUsingTsSolutionSetup()) {
9091
await validateTypes({
9192
workspaceRoot: context.root,
9293
tsconfig: tsConfigForBuild,

packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { createLoaderFromCompiler } from './compiler-loaders';
1919
import { NormalizedNxAppWebpackPluginOptions } from '../nx-app-webpack-plugin-options';
2020
import TerserPlugin = require('terser-webpack-plugin');
2121
import nodeExternals = require('webpack-node-externals');
22+
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
2223

2324
const IGNORED_WEBPACK_WARNINGS = [
2425
/The comment file/i,
@@ -232,7 +233,8 @@ function applyNxDependentConfig(
232233

233234
plugins.push(new NxTsconfigPathsWebpackPlugin({ ...options, tsConfig }));
234235

235-
if (!options?.skipTypeChecking) {
236+
// New TS Solution already has a typecheck target
237+
if (!options?.skipTypeChecking && !isUsingTsSolutionSetup()) {
236238
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
237239
plugins.push(
238240
new ForkTsCheckerWebpackPlugin({

0 commit comments

Comments
 (0)