Skip to content

Commit

Permalink
feat(js): remove nx property from package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Jan 23, 2025
1 parent d601561 commit 2a402a9
Show file tree
Hide file tree
Showing 68 changed files with 637 additions and 369 deletions.
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);
});
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type JestPresetExtension,
} from '../../../utils/config/config-file';
import type { NormalizedJestProjectSchema } from '../schema';
import { getProjectType } from '@nx/js/src/utils/typescript/ts-solution-setup';

export async function createJestConfig(
tree: Tree,
Expand Down Expand Up @@ -91,7 +92,13 @@ module.exports = { ...nxPreset };`
}

const jestProjectConfig = `jest.config.${
rootProjectConfig.projectType === 'application' ? 'app' : 'lib'
getProjectType(
tree,
rootProjectConfig.root,
rootProjectConfig.projectType
) === 'application'
? 'app'
: 'lib'
}.${options.js ? 'js' : 'ts'}`;

tree.rename(rootJestPath, jestProjectConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import {
type Tree,
} from '@nx/devkit';
import type { NormalizedJestProjectSchema } from '../schema';
import { getProjectType } from '@nx/js/src/utils/typescript/ts-solution-setup';

export function updateTsConfig(
host: Tree,
options: NormalizedJestProjectSchema
) {
const { root, projectType } = readProjectConfiguration(host, options.project);
const { root, projectType: _projectType } = readProjectConfiguration(
host,
options.project
);
if (!host.exists(joinPathFragments(root, 'tsconfig.json'))) {
throw new Error(
`Expected ${joinPathFragments(
Expand All @@ -31,6 +35,7 @@ export function updateTsConfig(
}
return json;
});
const projectType = getProjectType(host, root, _projectType);

// fall-back runtime tsconfig file path in case the user didn't provide one
let runtimeTsconfigPath = joinPathFragments(
Expand Down
Loading

0 comments on commit 2a402a9

Please sign in to comment.