Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(webpack): Add useTsconfigPaths to app-webpack-plugin #29291

Merged
merged 2 commits into from
Dec 11, 2024
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
1 change: 1 addition & 0 deletions packages/next/plugins/component-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,6 +18,7 @@ export function normalizeOptions(
): NormalizedRspackExecutorSchema {
const normalizedOptions = {
...options,
useTsconfigPaths: !isUsingTsSolutionSetup(),
root,
projectRoot,
sourceRoot,
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/executors/rspack/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ export interface NormalizedRspackExecutorSchema extends RspackExecutorSchema {
root: string;
projectRoot: string;
sourceRoot: string;
useTsconfigPaths: boolean;
}
10 changes: 8 additions & 2 deletions packages/rspack/src/plugins/utils/apply-base-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ function applyNxDependentConfig(
const tsConfig = options.tsConfig ?? getRootTsConfigPath();
const plugins: RspackPluginInstance[] = [];

const isUsingTsSolution = isUsingTsSolutionSetup();

const executorContext: Partial<ExecutorContext> = {
projectName: options.projectName,
targetName: options.targetName,
Expand All @@ -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({
Expand Down
4 changes: 4 additions & 0 deletions packages/rspack/src/plugins/utils/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function ensureNxRspackExecutionContext(ctx: NxRspackExecutionContext): void {
outputFileName: undefined,
outputPath: undefined,
rspackConfig: undefined,
useTsconfigPaths: undefined,
};
ctx.context ??= {
projectName,
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/utils/read-rspack-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function readRspackOptions(
tsConfig: '',
outputPath: '',
rspackConfig: '',
useTsconfigPaths: undefined,
},
context: {
root: workspaceRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,6 +18,7 @@ export function normalizeOptions(
): NormalizedWebpackExecutorOptions {
const normalizedOptions = {
...options,
useTsconfigPaths: !isUsingTsSolutionSetup(),
root,
projectRoot,
sourceRoot,
Expand Down
1 change: 1 addition & 0 deletions packages/webpack/src/executors/webpack/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ export interface NormalizedWebpackExecutorOptions
root: string;
projectRoot: string;
sourceRoot: string;
useTsconfigPaths: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/webpack/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function ensureNxWebpackExecutionContext(ctx: NxWebpackExecutionContext): void {
outputPath: undefined,
tsConfig: undefined,
outputFileName: undefined,
useTsconfigPaths: undefined,
};
ctx.context ??= {
projectName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function readWebpackOptions(
outputFileName: undefined,
outputPath: undefined,
assets: undefined,
useTsconfigPaths: undefined,
},
context: {
root: workspaceRoot,
Expand Down
Loading