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

feat(js): remove nx property from generated package.json files #29705

Merged
merged 3 commits into from
Jan 24, 2025
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
40 changes: 20 additions & 20 deletions e2e/js/src/js-ts-solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,70 +114,70 @@ ${content}`

// check build
expect(runCLI(`build ${esbuildParentLib}`)).toContain(
`Successfully ran target build for project ${esbuildParentLib} and 5 tasks it depends on`
`Successfully ran target build for project @proj/${esbuildParentLib} and 5 tasks it depends on`
);
expect(runCLI(`build ${rollupParentLib}`)).toContain(
`Successfully ran target build for project ${rollupParentLib} and 5 tasks it depends on`
`Successfully ran target build for project @proj/${rollupParentLib} and 5 tasks it depends on`
);
expect(runCLI(`build ${swcParentLib}`)).toContain(
`Successfully ran target build for project ${swcParentLib} and 5 tasks it depends on`
`Successfully ran target build for project @proj/${swcParentLib} and 5 tasks it depends on`
);
expect(runCLI(`build ${tscParentLib}`)).toContain(
`Successfully ran target build for project ${tscParentLib} and 5 tasks it depends on`
`Successfully ran target build for project @proj/${tscParentLib} and 5 tasks it depends on`
);
expect(runCLI(`build ${viteParentLib}`)).toContain(
`Successfully ran target build for project ${viteParentLib} and 5 tasks it depends on`
`Successfully ran target build for project @proj/${viteParentLib} and 5 tasks it depends on`
);

// check typecheck
expect(runCLI(`typecheck ${esbuildParentLib}`)).toContain(
`Successfully ran target typecheck for project ${esbuildParentLib} and 5 tasks it depends on`
`Successfully ran target typecheck for project @proj/${esbuildParentLib} and 5 tasks it depends on`
);
expect(runCLI(`typecheck ${rollupParentLib}`)).toContain(
`Successfully ran target typecheck for project ${rollupParentLib} and 5 tasks it depends on`
`Successfully ran target typecheck for project @proj/${rollupParentLib} and 5 tasks it depends on`
);
expect(runCLI(`typecheck ${swcParentLib}`)).toContain(
`Successfully ran target typecheck for project ${swcParentLib} and 5 tasks it depends on`
`Successfully ran target typecheck for project @proj/${swcParentLib} and 5 tasks it depends on`
);
expect(runCLI(`typecheck ${tscParentLib}`)).toContain(
`Successfully ran target typecheck for project ${tscParentLib} and 5 tasks it depends on`
`Successfully ran target typecheck for project @proj/${tscParentLib} and 5 tasks it depends on`
);
expect(runCLI(`typecheck ${viteParentLib}`)).toContain(
`Successfully ran target typecheck for project ${viteParentLib} and 5 tasks it depends on`
`Successfully ran target typecheck for project @proj/${viteParentLib} and 5 tasks it depends on`
);

// check lint
expect(runCLI(`lint ${esbuildParentLib}`)).toContain(
`Successfully ran target lint for project ${esbuildParentLib}`
`Successfully ran target lint for project @proj/${esbuildParentLib}`
);
expect(runCLI(`lint ${rollupParentLib}`)).toContain(
`Successfully ran target lint for project ${rollupParentLib}`
`Successfully ran target lint for project @proj/${rollupParentLib}`
);
expect(runCLI(`lint ${swcParentLib}`)).toContain(
`Successfully ran target lint for project ${swcParentLib}`
`Successfully ran target lint for project @proj/${swcParentLib}`
);
expect(runCLI(`lint ${tscParentLib}`)).toContain(
`Successfully ran target lint for project ${tscParentLib}`
`Successfully ran target lint for project @proj/${tscParentLib}`
);
expect(runCLI(`lint ${viteParentLib}`)).toContain(
`Successfully ran target lint for project ${viteParentLib}`
`Successfully ran target lint for project @proj/${viteParentLib}`
);

// check test
expect(runCLI(`test ${esbuildParentLib}`)).toContain(
`Successfully ran target test for project ${esbuildParentLib}`
`Successfully ran target test for project @proj/${esbuildParentLib}`
);
expect(runCLI(`test ${rollupParentLib}`)).toContain(
`Successfully ran target test for project ${rollupParentLib}`
`Successfully ran target test for project @proj/${rollupParentLib}`
);
expect(runCLI(`test ${swcParentLib}`)).toContain(
`Successfully ran target test for project ${swcParentLib}`
`Successfully ran target test for project @proj/${swcParentLib}`
);
expect(runCLI(`test ${tscParentLib}`)).toContain(
`Successfully ran target test for project ${tscParentLib}`
`Successfully ran target test for project @proj/${tscParentLib}`
);
expect(runCLI(`test ${viteParentLib}`)).toContain(
`Successfully ran target test for project ${viteParentLib}`
`Successfully ran target test for project @proj/${viteParentLib}`
);
}, 300_000);
});
20 changes: 10 additions & 10 deletions e2e/plugin/src/nx-plugin-ts-solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ describe('Nx Plugin (TS solution)', () => {
`generate @nx/plugin:migration packages/${plugin}/src/migrations/update-${migrationVersion}/update-${migrationVersion} --packageVersion=${migrationVersion} --packageJsonUpdates=false`
);

expect(runCLI(`lint ${plugin}`)).toContain(
`Successfully ran target lint for project ${plugin}`
expect(runCLI(`lint @proj/${plugin}`)).toContain(
`Successfully ran target lint for project @proj/${plugin}`
);
expect(runCLI(`typecheck ${plugin}`)).toContain(
`Successfully ran target typecheck for project ${plugin}`
expect(runCLI(`typecheck @proj/${plugin}`)).toContain(
`Successfully ran target typecheck for project @proj/${plugin}`
);
expect(runCLI(`build ${plugin}`)).toContain(
`Successfully ran target build for project ${plugin}`
expect(runCLI(`build @proj/${plugin}`)).toContain(
`Successfully ran target build for project @proj/${plugin}`
);
checkFilesExist(
// entry point
Expand All @@ -71,11 +71,11 @@ describe('Nx Plugin (TS solution)', () => {
`packages/${plugin}/dist/migrations/update-${migrationVersion}/update-${migrationVersion}.js`,
`packages/${plugin}/dist/migrations/update-${migrationVersion}/update-${migrationVersion}.d.ts`
);
expect(runCLI(`test ${plugin}`)).toContain(
`Successfully ran target test for project ${plugin}`
expect(runCLI(`test @proj/${plugin}`)).toContain(
`Successfully ran target test for project @proj/${plugin}`
);
expect(runCLI(`e2e ${plugin}-e2e`)).toContain(
`Successfully ran target e2e for project ${plugin}-e2e`
expect(runCLI(`e2e @proj/${plugin}-e2e`)).toContain(
`Successfully ran target e2e for project @proj/${plugin}-e2e`
);
}, 90000);

Expand Down
32 changes: 23 additions & 9 deletions e2e/vite/src/vite-ts-solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,27 @@ describe('Vite - TS solution setup', () => {
const viteLib = uniq('vite-lib');
const noBundlerLib = uniq('no-bundler-lib');

runCLI(`generate @nx/react:app apps/${reactApp} --bundler=vite`);
runCLI(`generate @nx/js:lib packages/${esbuildLib} --bundler=esbuild`);
runCLI(`generate @nx/js:lib packages/${rollupLib} --bundler=rollup`);
runCLI(`generate @nx/js:lib packages/${swcLib} --bundler=swc`);
runCLI(`generate @nx/js:lib packages/${tscLib} --bundler=tsc`);
runCLI(`generate @nx/js:lib packages/${viteLib} --bundler=vite`);
runCLI(`generate @nx/js:lib packages/${noBundlerLib} --bundler=none`);
runCLI(
`generate @nx/react:app apps/${reactApp} --bundler=vite --e2eTestRunner=none`
);
runCLI(
`generate @nx/js:lib packages/${esbuildLib} --bundler=esbuild --e2eTestRunner=none`
);
runCLI(
`generate @nx/js:lib packages/${rollupLib} --bundler=rollup --e2eTestRunner=none`
);
runCLI(
`generate @nx/js:lib packages/${swcLib} --bundler=swc --e2eTestRunner=none`
);
runCLI(
`generate @nx/js:lib packages/${tscLib} --bundler=tsc --e2eTestRunner=none`
);
runCLI(
`generate @nx/js:lib packages/${viteLib} --bundler=vite --e2eTestRunner=none`
);
runCLI(
`generate @nx/js:lib packages/${noBundlerLib} --bundler=none --e2eTestRunner=none`
);

// import all libs from the app
updateFile(
Expand Down Expand Up @@ -87,12 +101,12 @@ ${content}`

// check build
expect(runCLI(`build ${reactApp}`)).toContain(
`Successfully ran target build for project ${reactApp} and 5 tasks it depends on`
`Successfully ran target build for project @proj/${reactApp} and 5 tasks it depends on`
);

// check typecheck
expect(runCLI(`typecheck ${reactApp}`)).toContain(
`Successfully ran target typecheck for project ${reactApp} and 6 tasks it depends on`
`Successfully ran target typecheck for project @proj/${reactApp} and 6 tasks it depends on`
);
}, 300_000);
});
12 changes: 6 additions & 6 deletions e2e/vue/src/vue-ts-solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ describe('Vue Plugin', () => {
`
);

expect(() => runCLI(`lint ${app}`)).not.toThrow();
expect(() => runCLI(`test ${app}`)).not.toThrow();
expect(() => runCLI(`build ${app}`)).not.toThrow();
expect(() => runCLI(`lint ${lib}`)).not.toThrow();
expect(() => runCLI(`test ${lib}`)).not.toThrow();
expect(() => runCLI(`build ${lib}`)).not.toThrow();
expect(() => runCLI(`lint @proj/${app}`)).not.toThrow();
expect(() => runCLI(`test @proj/${app}`)).not.toThrow();
expect(() => runCLI(`build @proj/${app}`)).not.toThrow();
expect(() => runCLI(`lint @proj/${lib}`)).not.toThrow();
expect(() => runCLI(`test @proj/${lib}`)).not.toThrow();
expect(() => runCLI(`build @proj/${lib}`)).not.toThrow();
}, 300_000);
});
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,13 @@ export function updateTsConfigForComponentTesting(
tree: Tree,
projectConfig: ProjectConfiguration
) {
const tsConfigPath = joinPathFragments(
projectConfig.root,
projectConfig.projectType === 'library'
? 'tsconfig.lib.json'
: 'tsconfig.app.json'
);
let tsConfigPath: string | null = null;
for (const candidate of ['tsconfig.lib.json', 'tsconfig.app.json']) {
const p = joinPathFragments(projectConfig.root, candidate);
if (tree.exists(p)) tsConfigPath = p;
}

if (tree.exists(tsConfigPath)) {
if (tsConfigPath !== null) {
updateJson(tree, tsConfigPath, (json) => {
const excluded = new Set([
...(json.exclude || []),
Expand Down
48 changes: 20 additions & 28 deletions packages/devkit/src/generators/update-ts-configs-to-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@ export function updateTsConfigsToJs(
tree: Tree,
options: { projectRoot: string }
): void {
let updateConfigPath: string;
let updateConfigPath: string | null = null;

const paths = {
tsConfig: `${options.projectRoot}/tsconfig.json`,
tsConfigLib: `${options.projectRoot}/tsconfig.lib.json`,
tsConfigApp: `${options.projectRoot}/tsconfig.app.json`,
};

const getProjectType = (tree: Tree) => {
if (tree.exists(paths.tsConfigApp)) {
return 'application';
}
if (tree.exists(paths.tsConfigLib)) {
return 'library';
}

throw new Error(
`project is missing tsconfig.lib.json or tsconfig.app.json`
);
};

updateJson(tree, paths.tsConfig, (json) => {
if (json.compilerOptions) {
json.compilerOptions.allowJs = true;
Expand All @@ -34,25 +21,30 @@ export function updateTsConfigsToJs(
return json;
});

const projectType = getProjectType(tree);

if (projectType === 'library') {
if (tree.exists(paths.tsConfigLib)) {
updateConfigPath = paths.tsConfigLib;
}
if (projectType === 'application') {

if (tree.exists(paths.tsConfigApp)) {
updateConfigPath = paths.tsConfigApp;
}

updateJson(tree, updateConfigPath, (json) => {
json.include = uniq([...json.include, 'src/**/*.js']);
json.exclude = uniq([
...json.exclude,
'src/**/*.spec.js',
'src/**/*.test.js',
]);

return json;
});
if (updateConfigPath) {
updateJson(tree, updateConfigPath, (json) => {
json.include = uniq([...json.include, 'src/**/*.js']);
json.exclude = uniq([
...json.exclude,
'src/**/*.spec.js',
'src/**/*.test.js',
]);

return json;
});
} else {
throw new Error(
`project is missing tsconfig.lib.json or tsconfig.app.json`
);
}
}

const uniq = <T extends string[]>(value: T) => [...new Set(value)] as T;
11 changes: 7 additions & 4 deletions packages/eslint/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
} from '../../utils/config-file';
import { hasEslintPlugin } from '../utils/plugin';
import { setupRootEsLint } from './setup-root-eslint';
import { getProjectType } from '@nx/js/src/utils/typescript/ts-solution-setup';

interface LintProjectOptions {
project: string;
Expand Down Expand Up @@ -99,7 +100,7 @@ export async function lintProjectGeneratorInternal(
lintFilePatterns &&
lintFilePatterns.length &&
!lintFilePatterns.includes('{projectRoot}') &&
isBuildableLibraryProject(projectConfig)
isBuildableLibraryProject(tree, projectConfig)
) {
lintFilePatterns.push(`{projectRoot}/package.json`);
}
Expand Down Expand Up @@ -175,7 +176,7 @@ export async function lintProjectGeneratorInternal(

// Buildable libs need source analysis enabled for linting `package.json`.
if (
isBuildableLibraryProject(projectConfig) &&
isBuildableLibraryProject(tree, projectConfig) &&
!isJsAnalyzeSourceFilesEnabled(tree)
) {
updateJson(tree, 'nx.json', (json) => {
Expand Down Expand Up @@ -225,7 +226,7 @@ function createEsLintConfiguration(

const addDependencyChecks =
options.addPackageJsonDependencyChecks ||
isBuildableLibraryProject(projectConfig);
isBuildableLibraryProject(tree, projectConfig);

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

function isBuildableLibraryProject(
tree: Tree,
projectConfig: ProjectConfiguration
): boolean {
return (
projectConfig.projectType === 'library' &&
getProjectType(tree, projectConfig.root, projectConfig.projectType) ===
'library' &&
projectConfig.targets?.build &&
!!projectConfig.targets.build
);
Expand Down
4 changes: 3 additions & 1 deletion packages/expo/src/generators/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { NormalizedSchema, normalizeOptions } from './lib/normalize-options';
import { addImport } from './lib/add-import';
import { dirname, join, parse, relative } from 'path';
import { getProjectType } from '@nx/js/src/utils/typescript/ts-solution-setup';

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

function addExportsToBarrel(host: Tree, options: NormalizedSchema) {
const workspace = getProjects(host);
const proj = workspace.get(options.projectName);
const isApp =
workspace.get(options.projectName).projectType === 'application';
getProjectType(host, proj.root, proj.projectType) === 'application';

if (options.export && !isApp) {
const indexFilePath = joinPathFragments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type FileExtensionType,
} from '@nx/devkit/src/generators/artifact-name-and-directory-utils';
import { Schema } from '../schema';
import { getProjectType } from '@nx/js/src/utils/typescript/ts-solution-setup';

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

const { className } = names(name);
const project = getProjects(host).get(projectName);
const { sourceRoot: projectSourceRoot, projectType } = project;
const { root, sourceRoot: projectSourceRoot, projectType } = project;

if (options.export && projectType === 'application') {
if (
options.export &&
getProjectType(host, root, projectType) === 'application'
) {
logger.warn(
`The "--export" option should not be used with applications and will do nothing.`
);
Expand Down
Loading
Loading