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 21, 2025
1 parent 251e95d commit 59b78b0
Show file tree
Hide file tree
Showing 27 changed files with 208 additions and 160 deletions.
19 changes: 17 additions & 2 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import type {
NormalizedLibraryGeneratorOptions,
} from './schema';
import { sortPackageJsonFields } from '../../utils/package-json/sort-fields';
import { getImportPath } from '../../utils/get-import-path';

const defaultOutputDirectory = 'dist';

Expand Down Expand Up @@ -365,8 +366,20 @@ async function configureProject(
delete projectConfiguration.tags;
}

// We want a minimal setup, so unless targets and tags are set, just skip the `nx` property in `package.json`.
if (options.isUsingTsSolutionConfig) {
delete projectConfiguration.projectType;
delete projectConfiguration.sourceRoot;
}

// empty targets are cleaned up automatically by `updateProjectConfiguration`
updateProjectConfiguration(tree, options.name, projectConfiguration);
updateProjectConfiguration(
tree,
options.isUsingTsSolutionConfig
? options.importPath ?? options.name
: options.name,
projectConfiguration
);
} else if (options.config === 'workspace' || options.config === 'project') {
addProjectConfiguration(tree, options.name, projectConfiguration);
} else {
Expand Down Expand Up @@ -889,7 +902,9 @@ async function normalizeOptions(
return {
...options,
fileName,
name: projectName,
name: isUsingTsSolutionConfig
? getImportPath(tree, projectName)
: projectName,
projectNames,
projectRoot,
parsedTags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope';
import type { LibraryGeneratorSchema as JsLibraryGeneratorSchema } from '@nx/js/src/generators/library/schema';
import { Linter } from '@nx/eslint';
import type { LibraryGeneratorOptions, NormalizedOptions } from '../schema';
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { getImportPath } from '@nx/js/src/utils/get-import-path';

export async function normalizeOptions(
tree: Tree,
Expand Down Expand Up @@ -47,7 +49,9 @@ export async function normalizeOptions(
linter: options.linter ?? Linter.EsLint,
parsedTags,
prefix: getNpmScope(tree), // we could also allow customizing this
projectName,
projectName: isUsingTsSolutionSetup(tree)
? getImportPath(tree, projectName)
: projectName,
projectRoot,
importPath,
service: options.service ?? false,
Expand Down
9 changes: 0 additions & 9 deletions packages/node/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ describe('lib', () => {
"main",
"types",
"exports",
"nx",
"dependencies",
]
`);
Expand All @@ -580,11 +579,6 @@ describe('lib', () => {
},
"main": "./src/index.ts",
"name": "@proj/mylib",
"nx": {
"name": "mylib",
"projectType": "library",
"sourceRoot": "mylib/src",
},
"private": true,
"types": "./src/index.ts",
"version": "0.0.1",
Expand Down Expand Up @@ -684,9 +678,6 @@ describe('lib', () => {
"module": "./dist/index.js",
"name": "@proj/mylib",
"nx": {
"name": "mylib",
"projectType": "library",
"sourceRoot": "mylib/src",
"targets": {
"build": {
"executor": "@nx/js:swc",
Expand Down
20 changes: 12 additions & 8 deletions packages/node/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ export async function libraryGenerator(tree: Tree, schema: Schema) {

export async function libraryGeneratorInternal(tree: Tree, schema: Schema) {
const options = await normalizeOptions(tree, schema);

// If we are using the new TS solution
// We need to update the workspace file (package.json or pnpm-workspaces.yaml) to include the new project
if (options.isUsingTsSolutionConfig) {
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
}

const tasks: GeneratorCallback[] = [];

if (options.publishable === true && !schema.importPath) {
Expand Down Expand Up @@ -112,12 +119,6 @@ export async function libraryGeneratorInternal(tree: Tree, schema: Schema) {
tasks.push(() => installPackagesTask(tree, true));
}

// If we are using the new TS solution
// We need to update the workspace file (package.json or pnpm-workspaces.yaml) to include the new project
if (options.isUsingTsSolutionConfig) {
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
}

sortPackageJsonFields(tree, options.projectRoot);

if (!schema.skipFormat) {
Expand Down Expand Up @@ -163,14 +164,17 @@ async function normalizeOptions(
? options.tags.split(',').map((s) => s.trim())
: [];

const isUsingTsSolutionConfig = isUsingTsSolutionSetup(tree);
return {
...options,
fileName,
projectName,
projectName: isUsingTsSolutionConfig
? getImportPath(tree, projectName)
: projectName,
projectRoot,
parsedTags,
importPath,
isUsingTsSolutionConfig: isUsingTsSolutionSetup(tree),
isUsingTsSolutionConfig,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ export async function reactNativeApplicationGeneratorInternal(
options.appProjectRoot,
options.js,
options.skipPackageJson,
options.addPlugin
options.addPlugin,
'tsconfig.app.json'
);
tasks.push(jestTask);

const webTask = await webConfigurationGenerator(host, {
...options,
project: options.name,
project: options.projectName,
skipFormat: true,
});
tasks.push(webTask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ export function addProject(host: Tree, options: NormalizedSchema) {

if (isUsingTsSolutionSetup(host)) {
writeJson(host, joinPathFragments(options.appProjectRoot, 'package.json'), {
name: getImportPath(host, options.name),
name: options.projectName,
version: '0.0.1',
private: true,
nx: {
name: options.name,
projectType: 'application',
sourceRoot: `${options.appProjectRoot}/src`,
targets: hasPlugin ? {} : getTargets(options),
tags: options.parsedTags?.length ? options.parsedTags : undefined,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@nx/devkit/src/generators/project-name-and-root-utils';
import { Schema } from '../schema';
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { getImportPath } from '@nx/js/src/utils/get-import-path';

export interface NormalizedSchema extends Schema {
className: string; // app name in class case
Expand Down Expand Up @@ -56,14 +57,18 @@ export async function normalizeOptions(

const entryFile = options.js ? 'src/main.js' : 'src/main.tsx';

const isTsSolutionSetup = isUsingTsSolutionSetup(host);

return {
...options,
name: projectNames.projectSimpleName,
className,
fileName,
lowerCaseName: className.toLowerCase(),
displayName: options.displayName || className,
projectName: appProjectName,
projectName: isTsSolutionSetup
? getImportPath(host, appProjectName)
: appProjectName,
appProjectRoot,
iosProjectRoot,
androidProjectRoot,
Expand All @@ -72,6 +77,6 @@ export async function normalizeOptions(
rootProject,
e2eProjectName,
e2eProjectRoot,
isTsSolutionSetup: isUsingTsSolutionSetup(host),
isTsSolutionSetup,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@nx/devkit/src/generators/project-name-and-root-utils';
import { Schema } from '../schema';
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { getImportPath } from '@nx/js/src/utils/get-import-path';

export interface NormalizedSchema extends Schema {
name: string;
Expand Down Expand Up @@ -44,15 +45,18 @@ export async function normalizeOptions(
? options.tags.split(',').map((s) => s.trim())
: [];

const isUsingTsSolutionConfig = isUsingTsSolutionSetup(host);
const normalized: NormalizedSchema = {
...options,
fileName: projectName,
routePath: `/${projectNames.projectSimpleName}`,
name: projectName,
name: isUsingTsSolutionConfig
? getImportPath(host, projectName)
: projectName,
projectRoot,
parsedTags,
importPath,
isUsingTsSolutionConfig: isUsingTsSolutionSetup(host),
isUsingTsSolutionConfig,
};

return normalized;
Expand Down
10 changes: 4 additions & 6 deletions packages/react-native/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ export async function reactNativeLibraryGeneratorInternal(
options.projectRoot,
options.js,
options.skipPackageJson,
options.addPlugin
options.addPlugin,
'tsconfig.lib.json'
);
tasks.push(jestTask);

Expand Down Expand Up @@ -171,14 +172,11 @@ async function addProject(
'package.json'
);
if (options.isUsingTsSolutionConfig) {
writeJson(host, packageJsonPath, {
name: getImportPath(host, options.name),
writeJson(host, joinPathFragments(options.projectRoot, 'package.json'), {
name: options.name,
version: '0.0.1',
...determineEntryFields(options),
nx: {
name: options.name,
sourceRoot: joinPathFragments(options.projectRoot, 'src'),
projectType: 'library',
tags: options.parsedTags?.length ? options.parsedTags : undefined,
},
});
Expand Down
4 changes: 3 additions & 1 deletion packages/react-native/src/utils/add-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export async function addJest(
appProjectRoot: string,
js: boolean,
skipPackageJson: boolean,
addPlugin: boolean
addPlugin: boolean,
runtimeTsconfigFileName: string
) {
if (unitTestRunner !== 'jest') {
return () => {};
Expand All @@ -24,6 +25,7 @@ export async function addJest(
skipPackageJson,
skipFormat: true,
addPlugin,
runtimeTsconfigFileName,
});

// overwrite the jest.config.ts file because react native needs to have special transform property
Expand Down
5 changes: 1 addition & 4 deletions packages/react/src/generators/application/lib/add-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ export function addProject(host: Tree, options: NormalizedSchema) {

if (options.isUsingTsSolutionConfig) {
writeJson(host, joinPathFragments(options.appProjectRoot, 'package.json'), {
name: getImportPath(host, options.name),
name: options.projectName,
version: '0.0.1',
private: true,
nx: {
name: options.name,
projectType: 'application',
sourceRoot: `${options.appProjectRoot}/src`,
tags: options.parsedTags?.length ? options.parsedTags : undefined,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assertValidStyle } from '../../../utils/assertion';
import { NormalizedSchema, Schema } from '../schema';
import { findFreePort } from './find-free-port';
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { getImportPath } from '@nx/js/src/utils/get-import-path';

export function normalizeDirectory(options: Schema) {
options.directory = options.directory?.replace(/\\{1,2}/g, '/');
Expand Down Expand Up @@ -57,18 +58,21 @@ export async function normalizeOptions<T extends Schema = Schema>(

assertValidStyle(options.style);

const isUsingTsSolutionConfig = isUsingTsSolutionSetup(host);
const normalized = {
...options,
name: names(options.name).fileName,
projectName: appProjectName,
name: appProjectName,
projectName: isUsingTsSolutionConfig
? getImportPath(host, appProjectName)
: appProjectName,
appProjectRoot,
e2eProjectName,
e2eProjectRoot,
parsedTags,
fileName,
styledModule,
hasStyles: options.style !== 'none',
isUsingTsSolutionConfig: isUsingTsSolutionSetup(host),
isUsingTsSolutionConfig,
} as NormalizedSchema;

normalized.routing = normalized.routing ?? false;
Expand Down
1 change: 0 additions & 1 deletion packages/react/src/generators/host/host.rspack.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as devkit from '@nx/devkit';
import { Tree, updateJson, writeJson } from '@nx/devkit';
import { ProjectGraph, readJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/generators/host/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function hostGenerator(
const initTask = await applicationGenerator(host, {
...options,
directory: options.appProjectRoot,
name: options.projectName,
name: options.name,
// The target use-case is loading remotes as child routes, thus always enable routing.
routing: true,
skipFormat: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function normalizeOptions(
bundler,
fileName,
routePath: `/${projectNames.projectSimpleName}`,
name: projectName,
name: isUsingTsSolutionConfig ? importPath : projectName,
projectRoot,
parsedTags,
importPath,
Expand Down
Loading

0 comments on commit 59b78b0

Please sign in to comment.