diff --git a/packages/next/plugins/component-testing.ts b/packages/next/plugins/component-testing.ts index 0c2ccba5bc3fb..efc0f3389505f 100644 --- a/packages/next/plugins/component-testing.ts +++ b/packages/next/plugins/component-testing.ts @@ -127,6 +127,7 @@ Able to find CT project, ${!!ctProjectConfig}.`); projectRoot: ctProjectConfig.root, sourceRoot: ctProjectConfig.sourceRoot, main: '', + useTsconfigPaths: undefined, fileReplacements: buildFileReplacements, assets: buildAssets, outputPath: buildOuputPath, diff --git a/packages/rspack/src/executors/rspack/lib/normalize-options.ts b/packages/rspack/src/executors/rspack/lib/normalize-options.ts index e1474a0e18138..b389b9a242b40 100644 --- a/packages/rspack/src/executors/rspack/lib/normalize-options.ts +++ b/packages/rspack/src/executors/rspack/lib/normalize-options.ts @@ -8,6 +8,7 @@ import type { RspackExecutorSchema, NormalizedRspackExecutorSchema, } from '../schema'; +import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup'; export function normalizeOptions( options: RspackExecutorSchema, @@ -17,6 +18,7 @@ export function normalizeOptions( ): NormalizedRspackExecutorSchema { const normalizedOptions = { ...options, + useTsconfigPaths: !isUsingTsSolutionSetup(), root, projectRoot, sourceRoot, diff --git a/packages/rspack/src/executors/rspack/schema.d.ts b/packages/rspack/src/executors/rspack/schema.d.ts index 286f42c82b4e5..2ef4a00d0d0fc 100644 --- a/packages/rspack/src/executors/rspack/schema.d.ts +++ b/packages/rspack/src/executors/rspack/schema.d.ts @@ -69,4 +69,5 @@ export interface NormalizedRspackExecutorSchema extends RspackExecutorSchema { root: string; projectRoot: string; sourceRoot: string; + useTsconfigPaths: boolean; } diff --git a/packages/rspack/src/plugins/utils/apply-base-config.ts b/packages/rspack/src/plugins/utils/apply-base-config.ts index 6112c8b6bb9ab..923d3a41382d2 100644 --- a/packages/rspack/src/plugins/utils/apply-base-config.ts +++ b/packages/rspack/src/plugins/utils/apply-base-config.ts @@ -217,6 +217,8 @@ function applyNxDependentConfig( const tsConfig = options.tsConfig ?? getRootTsConfigPath(); const plugins: RspackPluginInstance[] = []; + const isUsingTsSolution = isUsingTsSolutionSetup(); + const executorContext: Partial = { projectName: options.projectName, targetName: options.targetName, @@ -225,10 +227,14 @@ function applyNxDependentConfig( root: options.root, }; - plugins.push(new NxTsconfigPathsRspackPlugin({ ...options, tsConfig })); + options.useTsconfigPaths ??= !isUsingTsSolution; + // If the project is using ts solutions setup, the paths are not in tsconfig and we should not use the plugin's paths. + if (options.useTsconfigPaths) { + plugins.push(new NxTsconfigPathsRspackPlugin({ ...options, tsConfig })); + } // New TS Solution already has a typecheck target - if (!options?.skipTypeChecking && !isUsingTsSolutionSetup()) { + if (!options?.skipTypeChecking && !isUsingTsSolution) { const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin'); plugins.push( new ForkTsCheckerWebpackPlugin({ diff --git a/packages/rspack/src/plugins/utils/models.ts b/packages/rspack/src/plugins/utils/models.ts index 82455ce8fdbea..de43f367983ce 100644 --- a/packages/rspack/src/plugins/utils/models.ts +++ b/packages/rspack/src/plugins/utils/models.ts @@ -207,6 +207,10 @@ export interface NxAppRspackPluginOptions { * List of TypeScript Compiler Transformers Plugins. */ transformers?: TransformerEntry[]; + /** + * Use tsconfig-paths-webpack-plugin to resolve modules using paths in the tsconfig file. + */ + useTsconfigPaths?: boolean; /** * Generate a separate vendor chunk for 3rd party packages. */ diff --git a/packages/rspack/src/utils/config.ts b/packages/rspack/src/utils/config.ts index 7a2f191f622f6..d28fcafb0ccd4 100644 --- a/packages/rspack/src/utils/config.ts +++ b/packages/rspack/src/utils/config.ts @@ -99,6 +99,7 @@ function ensureNxRspackExecutionContext(ctx: NxRspackExecutionContext): void { outputFileName: undefined, outputPath: undefined, rspackConfig: undefined, + useTsconfigPaths: undefined, }; ctx.context ??= { projectName, diff --git a/packages/rspack/src/utils/read-rspack-options.ts b/packages/rspack/src/utils/read-rspack-options.ts index 6c69c8836b2fc..f303342fb048f 100644 --- a/packages/rspack/src/utils/read-rspack-options.ts +++ b/packages/rspack/src/utils/read-rspack-options.ts @@ -31,6 +31,7 @@ export async function readRspackOptions( tsConfig: '', outputPath: '', rspackConfig: '', + useTsconfigPaths: undefined, }, context: { root: workspaceRoot, diff --git a/packages/webpack/src/executors/webpack/lib/normalize-options.ts b/packages/webpack/src/executors/webpack/lib/normalize-options.ts index df6b0198897a7..294f787f9d0ec 100644 --- a/packages/webpack/src/executors/webpack/lib/normalize-options.ts +++ b/packages/webpack/src/executors/webpack/lib/normalize-options.ts @@ -8,6 +8,7 @@ import type { NormalizedWebpackExecutorOptions, WebpackExecutorOptions, } from '../schema'; +import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup'; export function normalizeOptions( options: WebpackExecutorOptions, @@ -17,6 +18,7 @@ export function normalizeOptions( ): NormalizedWebpackExecutorOptions { const normalizedOptions = { ...options, + useTsconfigPaths: !isUsingTsSolutionSetup(), root, projectRoot, sourceRoot, diff --git a/packages/webpack/src/executors/webpack/schema.d.ts b/packages/webpack/src/executors/webpack/schema.d.ts index 883e4d91ca9d5..bcc13c2f781e2 100644 --- a/packages/webpack/src/executors/webpack/schema.d.ts +++ b/packages/webpack/src/executors/webpack/schema.d.ts @@ -95,4 +95,5 @@ export interface NormalizedWebpackExecutorOptions root: string; projectRoot: string; sourceRoot: string; + useTsconfigPaths: boolean; } diff --git a/packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts b/packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts index 4c49571e3cc80..8378c6ac9abf3 100644 --- a/packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts +++ b/packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts @@ -231,10 +231,16 @@ function applyNxDependentConfig( root: options.root, }; - plugins.push(new NxTsconfigPathsWebpackPlugin({ ...options, tsConfig })); + const isUsingTsSolution = isUsingTsSolutionSetup(); + options.useTsconfigPaths ??= !isUsingTsSolution; + + // If the project is using ts solutions setup, the paths are not in tsconfig and we should not use the plugin's paths. + if (options.useTsconfigPaths) { + plugins.push(new NxTsconfigPathsWebpackPlugin({ ...options, tsConfig })); + } // New TS Solution already has a typecheck target - if (!options?.skipTypeChecking && !isUsingTsSolutionSetup()) { + if (!options?.skipTypeChecking && !isUsingTsSolution) { const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin'); plugins.push( new ForkTsCheckerWebpackPlugin({ diff --git a/packages/webpack/src/plugins/nx-webpack-plugin/nx-app-webpack-plugin-options.ts b/packages/webpack/src/plugins/nx-webpack-plugin/nx-app-webpack-plugin-options.ts index fac494d3ff5c6..cc6cac16aae06 100644 --- a/packages/webpack/src/plugins/nx-webpack-plugin/nx-app-webpack-plugin-options.ts +++ b/packages/webpack/src/plugins/nx-webpack-plugin/nx-app-webpack-plugin-options.ts @@ -211,6 +211,10 @@ export interface NxAppWebpackPluginOptions { * List of TypeScript Compiler Transformers Plugins. */ transformers?: TransformerEntry[]; + /** + * Use tsconfig-paths-webpack-plugin to resolve modules using paths in the tsconfig file. + */ + useTsconfigPaths?: boolean; /** * Generate a separate vendor chunk for 3rd party packages. */ diff --git a/packages/webpack/src/utils/config.ts b/packages/webpack/src/utils/config.ts index 67623d2babb1b..c86dd3bad0185 100644 --- a/packages/webpack/src/utils/config.ts +++ b/packages/webpack/src/utils/config.ts @@ -99,6 +99,7 @@ function ensureNxWebpackExecutionContext(ctx: NxWebpackExecutionContext): void { outputPath: undefined, tsConfig: undefined, outputFileName: undefined, + useTsconfigPaths: undefined, }; ctx.context ??= { projectName, diff --git a/packages/webpack/src/utils/webpack/read-webpack-options.ts b/packages/webpack/src/utils/webpack/read-webpack-options.ts index 50f8782d60c49..0e13f134de8df 100644 --- a/packages/webpack/src/utils/webpack/read-webpack-options.ts +++ b/packages/webpack/src/utils/webpack/read-webpack-options.ts @@ -28,6 +28,7 @@ export async function readWebpackOptions( outputFileName: undefined, outputPath: undefined, assets: undefined, + useTsconfigPaths: undefined, }, context: { root: workspaceRoot,