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(core): allow for non-js libs to be moved without errors or the creation of any unneeded tsconfig files #28350

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 27 additions & 25 deletions packages/workspace/src/generators/move/lib/update-imports.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ChangeType,
ProjectConfiguration,
StringChange,
Tree,
applyChangesToString,
ChangeType,
getProjects,
getWorkspaceLayout,
joinPathFragments,
ProjectConfiguration,
readJson,
StringChange,
Tree,
visitNotIgnoredFiles,
writeJson,
} from '@nx/devkit';
Expand All @@ -17,11 +17,13 @@ import { getImportPath } from '../../../utilities/get-import-path';
import {
findNodes,
getRootTsConfigPathInTree,
TSConfig,
type TSConfigFileName,
} from '../../../utilities/ts-config';
import { ensureTypescript } from '../../../utilities/typescript';
import { isUsingTsSolutionSetup } from '../../../utilities/typescript/ts-solution-setup';
import { NormalizedSchema } from '../schema';
import { normalizePathSlashes } from './utils';
import { isUsingTsSolutionSetup } from '../../../utilities/typescript/ts-solution-setup';

let tsModule: typeof import('typescript');

Expand All @@ -46,41 +48,40 @@ export function updateImports(

// use the source root to find the from location
// this attempts to account for libs that have been created with --importPath
const tsConfigPath = isUsingTsSolution
? 'tsconfig.json'
: getRootTsConfigPathInTree(tree);
// If we are using a ts solution setup, we need to use tsconfig.json instead of tsconfig.base.json
let tsConfig: any;
let mainEntryPointImportPath: string;
let secondaryEntryPointImportPaths: string[];
let serverEntryPointImportPath: string;
if (tree.exists(tsConfigPath)) {
const tsConfigPath: TSConfigFileName | null = isUsingTsSolution
? ('tsconfig.json' as const)
: getRootTsConfigPathInTree(tree);
let tsConfig: TSConfig | undefined;
let paths: string[] = [];

let mainEntryPointImportPath: string | undefined;
let secondaryEntryPointImportPaths: string[] = [];
let serverEntryPointImportPath: string | undefined;

if (tsConfigPath !== null && tree.exists(tsConfigPath)) {
tsConfig = readJson(tree, tsConfigPath);
const sourceRoot =
project.sourceRoot ?? joinPathFragments(project.root, 'src');

mainEntryPointImportPath = Object.keys(
tsConfig.compilerOptions?.paths ?? {}
).find((path) =>
paths = Object.keys(tsConfig?.compilerOptions.paths ?? {});

mainEntryPointImportPath = paths.find((path) =>
tsConfig.compilerOptions.paths[path].some((x) =>
x.startsWith(ensureTrailingSlash(sourceRoot))
)
);
secondaryEntryPointImportPaths = Object.keys(
tsConfig.compilerOptions?.paths ?? {}
).filter((path) =>
secondaryEntryPointImportPaths = paths.filter((path) =>
tsConfig.compilerOptions.paths[path].some(
(x) =>
(x: string) =>
x.startsWith(ensureTrailingSlash(project.root)) &&
!x.startsWith(ensureTrailingSlash(sourceRoot))
)
);

// Next.js libs have a custom path for the server we need to update that as well
// example "paths": { @acme/lib/server : ['libs/lib/src/server.ts'] }
serverEntryPointImportPath = Object.keys(
tsConfig.compilerOptions?.paths ?? {}
).find((path) =>
serverEntryPointImportPath = paths.find((path) =>
tsConfig.compilerOptions.paths[path].some(
(x) =>
x.startsWith(ensureTrailingSlash(sourceRoot)) &&
Expand Down Expand Up @@ -166,13 +167,14 @@ export function updateImports(
} else {
updateTsConfigReferences(tsConfig, projectRoot, tsConfigPath, schema);
}

writeJson(tree, tsConfigPath, tsConfig);
}
}
}

function updateTsConfigReferences(
tsConfig: any,
tsConfig: TSConfig,
projectRoot: { from: string; to: string },
tsConfigPath: string,
schema: NormalizedSchema
Expand Down Expand Up @@ -208,7 +210,7 @@ function updateTsConfigReferences(
}

function updateTsConfigPaths(
tsConfig: any,
tsConfig: TSConfig,
projectRef: { from: string; to: string },
tsConfigPath: string,
projectRoot: { from: string; to: string },
Expand Down
22 changes: 15 additions & 7 deletions packages/workspace/src/utilities/ts-config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { offsetFromRoot, Tree, workspaceRoot } from '@nx/devkit';
import { existsSync } from 'fs';
import { dirname, join } from 'path';
import type { Node, SyntaxKind } from 'typescript';
import type { CompilerOptions, Node, SyntaxKind } from 'typescript';
import { ensureTypescript } from './typescript';

let tsModule: typeof import('typescript');

const TSCONFIG_FILE_NAMES = ['tsconfig.base.json', 'tsconfig.json'] as const;
export type TSConfigFileName = (typeof TSCONFIG_FILE_NAMES)[number];

export type TSConfig = {
compilerOptions: CompilerOptions;
references: { path: string }[];
[string: string]: unknown;
};

export function readTsConfig(tsConfigPath: string) {
if (!tsModule) {
tsModule = ensureTypescript();
Expand All @@ -20,15 +29,14 @@ export function readTsConfig(tsConfigPath: string) {
dirname(tsConfigPath)
);
}

export function getRootTsConfigPathInTree(tree: Tree): string | null {
for (const path of ['tsconfig.base.json', 'tsconfig.json']) {
export function getRootTsConfigPathInTree(tree: Tree): TSConfigFileName | null {
for (const path of TSCONFIG_FILE_NAMES) {
if (tree.exists(path)) {
return path;
}
}

return 'tsconfig.base.json';
return null;
}

export function getRelativePathToRootTsConfig(
Expand All @@ -38,8 +46,8 @@ export function getRelativePathToRootTsConfig(
return offsetFromRoot(targetPath) + getRootTsConfigPathInTree(tree);
}

export function getRootTsConfigFileName(): string | null {
for (const tsConfigName of ['tsconfig.base.json', 'tsconfig.json']) {
export function getRootTsConfigFileName(): TSConfigFileName | null {
for (const tsConfigName of TSCONFIG_FILE_NAMES) {
const tsConfigPath = join(workspaceRoot, tsConfigName);
if (existsSync(tsConfigPath)) {
return tsConfigName;
Expand Down