Skip to content

Commit 45847a6

Browse files
authored
feat(js): remove nx property from generated package.json files (#29705)
This PR updates our generators to no longer generate with `nx` in `package.json` by default. The only times it is needed is if you pass add `tags` or `implicitDependencies` to the project config. This PR replaces our `projectType` checks to use the `getProjectType` util from `@nx/js` to prefer the project config, but otherwise will check for our conventions (e.g. using `exports` for libs, `tsconfig.lib.json` vs `tsconfig.app.json`). ## Impact - There shouldn't be any behavioral changes to existing projects that have explicit `projectType`, `name`, etc. in with `project.json` or `package.json` (via `nx` property). - For new projects created under the new TS setup, the `nx` property will no longer be there. Generators with logic that depend on `projectType` will now check for `tsconfig.lib.json` and `tsconfig.app.json` (so all of our generators are covered). If none of those tsconfig files are found, then we check `package.json`, since libraries are required to have `exports` to be consumed.
1 parent ae9aa5a commit 45847a6

File tree

73 files changed

+653
-384
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+653
-384
lines changed

e2e/js/src/js-ts-solution.test.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -114,70 +114,70 @@ ${content}`
114114

115115
// check build
116116
expect(runCLI(`build ${esbuildParentLib}`)).toContain(
117-
`Successfully ran target build for project ${esbuildParentLib} and 5 tasks it depends on`
117+
`Successfully ran target build for project @proj/${esbuildParentLib} and 5 tasks it depends on`
118118
);
119119
expect(runCLI(`build ${rollupParentLib}`)).toContain(
120-
`Successfully ran target build for project ${rollupParentLib} and 5 tasks it depends on`
120+
`Successfully ran target build for project @proj/${rollupParentLib} and 5 tasks it depends on`
121121
);
122122
expect(runCLI(`build ${swcParentLib}`)).toContain(
123-
`Successfully ran target build for project ${swcParentLib} and 5 tasks it depends on`
123+
`Successfully ran target build for project @proj/${swcParentLib} and 5 tasks it depends on`
124124
);
125125
expect(runCLI(`build ${tscParentLib}`)).toContain(
126-
`Successfully ran target build for project ${tscParentLib} and 5 tasks it depends on`
126+
`Successfully ran target build for project @proj/${tscParentLib} and 5 tasks it depends on`
127127
);
128128
expect(runCLI(`build ${viteParentLib}`)).toContain(
129-
`Successfully ran target build for project ${viteParentLib} and 5 tasks it depends on`
129+
`Successfully ran target build for project @proj/${viteParentLib} and 5 tasks it depends on`
130130
);
131131

132132
// check typecheck
133133
expect(runCLI(`typecheck ${esbuildParentLib}`)).toContain(
134-
`Successfully ran target typecheck for project ${esbuildParentLib} and 5 tasks it depends on`
134+
`Successfully ran target typecheck for project @proj/${esbuildParentLib} and 5 tasks it depends on`
135135
);
136136
expect(runCLI(`typecheck ${rollupParentLib}`)).toContain(
137-
`Successfully ran target typecheck for project ${rollupParentLib} and 5 tasks it depends on`
137+
`Successfully ran target typecheck for project @proj/${rollupParentLib} and 5 tasks it depends on`
138138
);
139139
expect(runCLI(`typecheck ${swcParentLib}`)).toContain(
140-
`Successfully ran target typecheck for project ${swcParentLib} and 5 tasks it depends on`
140+
`Successfully ran target typecheck for project @proj/${swcParentLib} and 5 tasks it depends on`
141141
);
142142
expect(runCLI(`typecheck ${tscParentLib}`)).toContain(
143-
`Successfully ran target typecheck for project ${tscParentLib} and 5 tasks it depends on`
143+
`Successfully ran target typecheck for project @proj/${tscParentLib} and 5 tasks it depends on`
144144
);
145145
expect(runCLI(`typecheck ${viteParentLib}`)).toContain(
146-
`Successfully ran target typecheck for project ${viteParentLib} and 5 tasks it depends on`
146+
`Successfully ran target typecheck for project @proj/${viteParentLib} and 5 tasks it depends on`
147147
);
148148

149149
// check lint
150150
expect(runCLI(`lint ${esbuildParentLib}`)).toContain(
151-
`Successfully ran target lint for project ${esbuildParentLib}`
151+
`Successfully ran target lint for project @proj/${esbuildParentLib}`
152152
);
153153
expect(runCLI(`lint ${rollupParentLib}`)).toContain(
154-
`Successfully ran target lint for project ${rollupParentLib}`
154+
`Successfully ran target lint for project @proj/${rollupParentLib}`
155155
);
156156
expect(runCLI(`lint ${swcParentLib}`)).toContain(
157-
`Successfully ran target lint for project ${swcParentLib}`
157+
`Successfully ran target lint for project @proj/${swcParentLib}`
158158
);
159159
expect(runCLI(`lint ${tscParentLib}`)).toContain(
160-
`Successfully ran target lint for project ${tscParentLib}`
160+
`Successfully ran target lint for project @proj/${tscParentLib}`
161161
);
162162
expect(runCLI(`lint ${viteParentLib}`)).toContain(
163-
`Successfully ran target lint for project ${viteParentLib}`
163+
`Successfully ran target lint for project @proj/${viteParentLib}`
164164
);
165165

166166
// check test
167167
expect(runCLI(`test ${esbuildParentLib}`)).toContain(
168-
`Successfully ran target test for project ${esbuildParentLib}`
168+
`Successfully ran target test for project @proj/${esbuildParentLib}`
169169
);
170170
expect(runCLI(`test ${rollupParentLib}`)).toContain(
171-
`Successfully ran target test for project ${rollupParentLib}`
171+
`Successfully ran target test for project @proj/${rollupParentLib}`
172172
);
173173
expect(runCLI(`test ${swcParentLib}`)).toContain(
174-
`Successfully ran target test for project ${swcParentLib}`
174+
`Successfully ran target test for project @proj/${swcParentLib}`
175175
);
176176
expect(runCLI(`test ${tscParentLib}`)).toContain(
177-
`Successfully ran target test for project ${tscParentLib}`
177+
`Successfully ran target test for project @proj/${tscParentLib}`
178178
);
179179
expect(runCLI(`test ${viteParentLib}`)).toContain(
180-
`Successfully ran target test for project ${viteParentLib}`
180+
`Successfully ran target test for project @proj/${viteParentLib}`
181181
);
182182
}, 300_000);
183183
});

e2e/plugin/src/nx-plugin-ts-solution.test.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ describe('Nx Plugin (TS solution)', () => {
4242
`generate @nx/plugin:migration packages/${plugin}/src/migrations/update-${migrationVersion}/update-${migrationVersion} --packageVersion=${migrationVersion} --packageJsonUpdates=false`
4343
);
4444

45-
expect(runCLI(`lint ${plugin}`)).toContain(
46-
`Successfully ran target lint for project ${plugin}`
45+
expect(runCLI(`lint @proj/${plugin}`)).toContain(
46+
`Successfully ran target lint for project @proj/${plugin}`
4747
);
48-
expect(runCLI(`typecheck ${plugin}`)).toContain(
49-
`Successfully ran target typecheck for project ${plugin}`
48+
expect(runCLI(`typecheck @proj/${plugin}`)).toContain(
49+
`Successfully ran target typecheck for project @proj/${plugin}`
5050
);
51-
expect(runCLI(`build ${plugin}`)).toContain(
52-
`Successfully ran target build for project ${plugin}`
51+
expect(runCLI(`build @proj/${plugin}`)).toContain(
52+
`Successfully ran target build for project @proj/${plugin}`
5353
);
5454
checkFilesExist(
5555
// entry point
@@ -71,11 +71,11 @@ describe('Nx Plugin (TS solution)', () => {
7171
`packages/${plugin}/dist/migrations/update-${migrationVersion}/update-${migrationVersion}.js`,
7272
`packages/${plugin}/dist/migrations/update-${migrationVersion}/update-${migrationVersion}.d.ts`
7373
);
74-
expect(runCLI(`test ${plugin}`)).toContain(
75-
`Successfully ran target test for project ${plugin}`
74+
expect(runCLI(`test @proj/${plugin}`)).toContain(
75+
`Successfully ran target test for project @proj/${plugin}`
7676
);
77-
expect(runCLI(`e2e ${plugin}-e2e`)).toContain(
78-
`Successfully ran target e2e for project ${plugin}-e2e`
77+
expect(runCLI(`e2e @proj/${plugin}-e2e`)).toContain(
78+
`Successfully ran target e2e for project @proj/${plugin}-e2e`
7979
);
8080
}, 90000);
8181

e2e/vite/src/vite-ts-solution.test.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,27 @@ describe('Vite - TS solution setup', () => {
3232
const viteLib = uniq('vite-lib');
3333
const noBundlerLib = uniq('no-bundler-lib');
3434

35-
runCLI(`generate @nx/react:app apps/${reactApp} --bundler=vite`);
36-
runCLI(`generate @nx/js:lib packages/${esbuildLib} --bundler=esbuild`);
37-
runCLI(`generate @nx/js:lib packages/${rollupLib} --bundler=rollup`);
38-
runCLI(`generate @nx/js:lib packages/${swcLib} --bundler=swc`);
39-
runCLI(`generate @nx/js:lib packages/${tscLib} --bundler=tsc`);
40-
runCLI(`generate @nx/js:lib packages/${viteLib} --bundler=vite`);
41-
runCLI(`generate @nx/js:lib packages/${noBundlerLib} --bundler=none`);
35+
runCLI(
36+
`generate @nx/react:app apps/${reactApp} --bundler=vite --e2eTestRunner=none`
37+
);
38+
runCLI(
39+
`generate @nx/js:lib packages/${esbuildLib} --bundler=esbuild --e2eTestRunner=none`
40+
);
41+
runCLI(
42+
`generate @nx/js:lib packages/${rollupLib} --bundler=rollup --e2eTestRunner=none`
43+
);
44+
runCLI(
45+
`generate @nx/js:lib packages/${swcLib} --bundler=swc --e2eTestRunner=none`
46+
);
47+
runCLI(
48+
`generate @nx/js:lib packages/${tscLib} --bundler=tsc --e2eTestRunner=none`
49+
);
50+
runCLI(
51+
`generate @nx/js:lib packages/${viteLib} --bundler=vite --e2eTestRunner=none`
52+
);
53+
runCLI(
54+
`generate @nx/js:lib packages/${noBundlerLib} --bundler=none --e2eTestRunner=none`
55+
);
4256

4357
// import all libs from the app
4458
updateFile(
@@ -87,12 +101,12 @@ ${content}`
87101

88102
// check build
89103
expect(runCLI(`build ${reactApp}`)).toContain(
90-
`Successfully ran target build for project ${reactApp} and 5 tasks it depends on`
104+
`Successfully ran target build for project @proj/${reactApp} and 5 tasks it depends on`
91105
);
92106

93107
// check typecheck
94108
expect(runCLI(`typecheck ${reactApp}`)).toContain(
95-
`Successfully ran target typecheck for project ${reactApp} and 6 tasks it depends on`
109+
`Successfully ran target typecheck for project @proj/${reactApp} and 6 tasks it depends on`
96110
);
97111
}, 300_000);
98112
});

e2e/vue/src/vue-ts-solution.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ describe('Vue Plugin', () => {
5050
`
5151
);
5252

53-
expect(() => runCLI(`lint ${app}`)).not.toThrow();
54-
expect(() => runCLI(`test ${app}`)).not.toThrow();
55-
expect(() => runCLI(`build ${app}`)).not.toThrow();
56-
expect(() => runCLI(`lint ${lib}`)).not.toThrow();
57-
expect(() => runCLI(`test ${lib}`)).not.toThrow();
58-
expect(() => runCLI(`build ${lib}`)).not.toThrow();
53+
expect(() => runCLI(`lint @proj/${app}`)).not.toThrow();
54+
expect(() => runCLI(`test @proj/${app}`)).not.toThrow();
55+
expect(() => runCLI(`build @proj/${app}`)).not.toThrow();
56+
expect(() => runCLI(`lint @proj/${lib}`)).not.toThrow();
57+
expect(() => runCLI(`test @proj/${lib}`)).not.toThrow();
58+
expect(() => runCLI(`build @proj/${lib}`)).not.toThrow();
5959
}, 300_000);
6060
});

packages/cypress/src/generators/component-configuration/component-configuration.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,13 @@ export function updateTsConfigForComponentTesting(
202202
tree: Tree,
203203
projectConfig: ProjectConfiguration
204204
) {
205-
const tsConfigPath = joinPathFragments(
206-
projectConfig.root,
207-
projectConfig.projectType === 'library'
208-
? 'tsconfig.lib.json'
209-
: 'tsconfig.app.json'
210-
);
205+
let tsConfigPath: string | null = null;
206+
for (const candidate of ['tsconfig.lib.json', 'tsconfig.app.json']) {
207+
const p = joinPathFragments(projectConfig.root, candidate);
208+
if (tree.exists(p)) tsConfigPath = p;
209+
}
211210

212-
if (tree.exists(tsConfigPath)) {
211+
if (tsConfigPath !== null) {
213212
updateJson(tree, tsConfigPath, (json) => {
214213
const excluded = new Set([
215214
...(json.exclude || []),

packages/devkit/src/generators/update-ts-configs-to-js.ts

+20-28
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,14 @@ export function updateTsConfigsToJs(
44
tree: Tree,
55
options: { projectRoot: string }
66
): void {
7-
let updateConfigPath: string;
7+
let updateConfigPath: string | null = null;
88

99
const paths = {
1010
tsConfig: `${options.projectRoot}/tsconfig.json`,
1111
tsConfigLib: `${options.projectRoot}/tsconfig.lib.json`,
1212
tsConfigApp: `${options.projectRoot}/tsconfig.app.json`,
1313
};
1414

15-
const getProjectType = (tree: Tree) => {
16-
if (tree.exists(paths.tsConfigApp)) {
17-
return 'application';
18-
}
19-
if (tree.exists(paths.tsConfigLib)) {
20-
return 'library';
21-
}
22-
23-
throw new Error(
24-
`project is missing tsconfig.lib.json or tsconfig.app.json`
25-
);
26-
};
27-
2815
updateJson(tree, paths.tsConfig, (json) => {
2916
if (json.compilerOptions) {
3017
json.compilerOptions.allowJs = true;
@@ -34,25 +21,30 @@ export function updateTsConfigsToJs(
3421
return json;
3522
});
3623

37-
const projectType = getProjectType(tree);
38-
39-
if (projectType === 'library') {
24+
if (tree.exists(paths.tsConfigLib)) {
4025
updateConfigPath = paths.tsConfigLib;
4126
}
42-
if (projectType === 'application') {
27+
28+
if (tree.exists(paths.tsConfigApp)) {
4329
updateConfigPath = paths.tsConfigApp;
4430
}
4531

46-
updateJson(tree, updateConfigPath, (json) => {
47-
json.include = uniq([...json.include, 'src/**/*.js']);
48-
json.exclude = uniq([
49-
...json.exclude,
50-
'src/**/*.spec.js',
51-
'src/**/*.test.js',
52-
]);
53-
54-
return json;
55-
});
32+
if (updateConfigPath) {
33+
updateJson(tree, updateConfigPath, (json) => {
34+
json.include = uniq([...json.include, 'src/**/*.js']);
35+
json.exclude = uniq([
36+
...json.exclude,
37+
'src/**/*.spec.js',
38+
'src/**/*.test.js',
39+
]);
40+
41+
return json;
42+
});
43+
} else {
44+
throw new Error(
45+
`project is missing tsconfig.lib.json or tsconfig.app.json`
46+
);
47+
}
5648
}
5749

5850
const uniq = <T extends string[]>(value: T) => [...new Set(value)] as T;

packages/eslint/src/generators/lint-project/lint-project.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
} from '../../utils/config-file';
4040
import { hasEslintPlugin } from '../utils/plugin';
4141
import { setupRootEsLint } from './setup-root-eslint';
42+
import { getProjectType } from '@nx/js/src/utils/typescript/ts-solution-setup';
4243

4344
interface LintProjectOptions {
4445
project: string;
@@ -99,7 +100,7 @@ export async function lintProjectGeneratorInternal(
99100
lintFilePatterns &&
100101
lintFilePatterns.length &&
101102
!lintFilePatterns.includes('{projectRoot}') &&
102-
isBuildableLibraryProject(projectConfig)
103+
isBuildableLibraryProject(tree, projectConfig)
103104
) {
104105
lintFilePatterns.push(`{projectRoot}/package.json`);
105106
}
@@ -175,7 +176,7 @@ export async function lintProjectGeneratorInternal(
175176

176177
// Buildable libs need source analysis enabled for linting `package.json`.
177178
if (
178-
isBuildableLibraryProject(projectConfig) &&
179+
isBuildableLibraryProject(tree, projectConfig) &&
179180
!isJsAnalyzeSourceFilesEnabled(tree)
180181
) {
181182
updateJson(tree, 'nx.json', (json) => {
@@ -225,7 +226,7 @@ function createEsLintConfiguration(
225226

226227
const addDependencyChecks =
227228
options.addPackageJsonDependencyChecks ||
228-
isBuildableLibraryProject(projectConfig);
229+
isBuildableLibraryProject(tree, projectConfig);
229230

230231
const overrides: Linter.ConfigOverride<Linter.RulesRecord>[] = useFlatConfig(
231232
tree
@@ -328,10 +329,12 @@ function isJsAnalyzeSourceFilesEnabled(tree: Tree): boolean {
328329
}
329330

330331
function isBuildableLibraryProject(
332+
tree: Tree,
331333
projectConfig: ProjectConfiguration
332334
): boolean {
333335
return (
334-
projectConfig.projectType === 'library' &&
336+
getProjectType(tree, projectConfig.root, projectConfig.projectType) ===
337+
'library' &&
335338
projectConfig.targets?.build &&
336339
!!projectConfig.targets.build
337340
);

packages/expo/src/generators/component/component.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { NormalizedSchema, normalizeOptions } from './lib/normalize-options';
1212
import { addImport } from './lib/add-import';
1313
import { dirname, join, parse, relative } from 'path';
14+
import { getProjectType } from '@nx/js/src/utils/typescript/ts-solution-setup';
1415

1516
export async function expoComponentGenerator(host: Tree, schema: Schema) {
1617
const options = await normalizeOptions(host, schema);
@@ -46,8 +47,9 @@ function createComponentFiles(host: Tree, options: NormalizedSchema) {
4647

4748
function addExportsToBarrel(host: Tree, options: NormalizedSchema) {
4849
const workspace = getProjects(host);
50+
const proj = workspace.get(options.projectName);
4951
const isApp =
50-
workspace.get(options.projectName).projectType === 'application';
52+
getProjectType(host, proj.root, proj.projectType) === 'application';
5153

5254
if (options.export && !isApp) {
5355
const indexFilePath = joinPathFragments(

packages/expo/src/generators/component/lib/normalize-options.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type FileExtensionType,
55
} from '@nx/devkit/src/generators/artifact-name-and-directory-utils';
66
import { Schema } from '../schema';
7+
import { getProjectType } from '@nx/js/src/utils/typescript/ts-solution-setup';
78

89
export interface NormalizedSchema extends Omit<Schema, 'js'> {
910
directory: string;
@@ -38,9 +39,12 @@ export async function normalizeOptions(
3839

3940
const { className } = names(name);
4041
const project = getProjects(host).get(projectName);
41-
const { sourceRoot: projectSourceRoot, projectType } = project;
42+
const { root, sourceRoot: projectSourceRoot, projectType } = project;
4243

43-
if (options.export && projectType === 'application') {
44+
if (
45+
options.export &&
46+
getProjectType(host, root, projectType) === 'application'
47+
) {
4448
logger.warn(
4549
`The "--export" option should not be used with applications and will do nothing.`
4650
);

0 commit comments

Comments
 (0)