Skip to content

Commit

Permalink
fix(js): do not generate tsconfig.base.json when creating standalone …
Browse files Browse the repository at this point in the history
…projects (#15099)

Co-authored-by: FrozenPandaz <[email protected]>
  • Loading branch information
jaysoo and FrozenPandaz authored Feb 18, 2023
1 parent 804cb95 commit 71fd015
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 12 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/js/generators/init.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"description": "Skip formatting files.",
"default": true,
"x-priority": "internal"
},
"tsConfigName": {
"type": "string",
"description": "Customize the generated tsconfig file name.",
"x-priority": "internal"
}
},
"presets": []
Expand Down
3 changes: 3 additions & 0 deletions e2e/workspace-create/src/create-nx-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('create-nx-workspace', () => {

checkFilesExist('package.json');
checkFilesExist('src/app/app.routes.ts');
checkFilesDoNotExist('tsconfig.base.json');
checkFilesExist('project.json');
});

Expand Down Expand Up @@ -83,6 +84,7 @@ describe('create-nx-workspace', () => {
checkFilesExist('package.json');
checkFilesExist('project.json');
checkFilesExist('vite.config.ts');
checkFilesDoNotExist('tsconfig.base.json');
});

it('should create a workspace with a single react app with webpack at the root', () => {
Expand All @@ -99,6 +101,7 @@ describe('create-nx-workspace', () => {
checkFilesExist('package.json');
checkFilesExist('project.json');
checkFilesExist('webpack.config.js');
checkFilesDoNotExist('tsconfig.base.json');
});

it('should be able to create an empty workspace built for apps', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/angular/src/generators/init/angular-v14/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export async function angularInitGenerator(
const options = normalizeOptions(rawOptions);
setDefaults(host, options);
await jsInitGenerator(host, {
...options,
tsConfigName: options.rootProject ? 'tsconfig.json' : 'tsconfig.base.json',
js: false,
skipFormat: true,
});
Expand Down Expand Up @@ -78,6 +80,7 @@ function normalizeOptions(options: Schema): Required<Schema> {
skipPackageJson: options.skipPackageJson ?? false,
style: options.style ?? 'css',
unitTestRunner: options.unitTestRunner ?? UnitTestRunner.Jest,
rootProject: options.rootProject,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface Schema {
style?: Styles;
linter?: Linter;
skipPackageJson?: boolean;
rootProject?: boolean;
}
3 changes: 3 additions & 0 deletions packages/angular/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export async function angularInitGenerator(
const options = normalizeOptions(rawOptions);
setDefaults(tree, options);
await jsInitGenerator(tree, {
...options,
tsConfigName: options.rootProject ? 'tsconfig.json' : 'tsconfig.base.json',
js: false,
skipFormat: true,
});
Expand Down Expand Up @@ -102,6 +104,7 @@ function normalizeOptions(options: Schema): Required<Schema> {
skipPackageJson: options.skipPackageJson ?? false,
style: options.style ?? 'css',
unitTestRunner: options.unitTestRunner ?? UnitTestRunner.Jest,
rootProject: options.rootProject,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface Schema {
style?: Styles;
linter?: Linter;
skipPackageJson?: boolean;
rootProject?: boolean;
}
6 changes: 4 additions & 2 deletions packages/js/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export async function initGenerator(
}

// add tsconfig.base.json
if (!getRootTsConfigFileName()) {
generateFiles(host, joinPathFragments(__dirname, './files'), '.', {});
if (!getRootTsConfigFileName(host)) {
generateFiles(host, joinPathFragments(__dirname, './files'), '.', {
fileName: schema.tsConfigName ?? 'tsconfig.base.json',
});
}

if (!schema.skipFormat) {
Expand Down
1 change: 1 addition & 0 deletions packages/js/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface InitSchema {
js?: boolean;
skipFormat?: boolean;
tsConfigName?: string;
}
5 changes: 5 additions & 0 deletions packages/js/src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"description": "Skip formatting files.",
"default": true,
"x-priority": "internal"
},
"tsConfigName": {
"type": "string",
"description": "Customize the generated tsconfig file name.",
"x-priority": "internal"
}
}
}
11 changes: 2 additions & 9 deletions packages/js/src/utils/typescript/ts-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,16 @@ export function getRelativePathToRootTsConfig(
return offsetFromRoot(targetPath) + getRootTsConfigPathInTree(tree);
}

export function getRootTsConfigFileName(): string | null {
export function getRootTsConfigFileName(tree: Tree): string | null {
for (const tsConfigName of ['tsconfig.base.json', 'tsconfig.json']) {
const tsConfigPath = join(workspaceRoot, tsConfigName);
if (existsSync(tsConfigPath)) {
if (tree.exists(tsConfigName)) {
return tsConfigName;
}
}

return null;
}

export function getRootTsConfigPath(): string | null {
const tsConfigFileName = getRootTsConfigFileName();

return tsConfigFileName ? join(workspaceRoot, tsConfigFileName) : null;
}

export function updateRootTsConfig(
host: Tree,
options: {
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
const tasks: GeneratorCallback[] = [];

const initTask = await initGenerator(tree, {
...options,
...schema,
skipFormat: true,
});
tasks.push(initTask);
Expand Down
2 changes: 2 additions & 0 deletions packages/node/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function normalizeOptions(schema: Schema) {
export async function initGenerator(tree: Tree, schema: Schema) {
const options = normalizeOptions(schema);
await jsInitGenerator(tree, {
...schema,
tsConfigName: schema.rootProject ? 'tsconfig.json' : 'tsconfig.base.json',
js: schema.js,
skipFormat: true,
});
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface Schema {
unitTestRunner?: 'jest' | 'none';
skipFormat?: boolean;
js?: boolean;
rootProject?: boolean;
}
2 changes: 2 additions & 0 deletions packages/react/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ function initRootBabelConfig(tree: Tree, schema: InitSchema) {

export async function reactInitGenerator(host: Tree, schema: InitSchema) {
await jsInitGenerator(host, {
...schema,
tsConfigName: schema.rootProject ? 'tsconfig.json' : 'tsconfig.base.json',
js: schema.js,
skipFormat: true,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export interface InitSchema {
skipPackageJson?: boolean;
skipHelperLibs?: boolean;
js?: boolean;

rootProject?: boolean;
}

1 comment on commit 71fd015

@vercel
Copy link

@vercel vercel bot commented on 71fd015 Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.