Skip to content

Commit

Permalink
fix(remix): update lib generator to generate valid names in package.j…
Browse files Browse the repository at this point in the history
…son (#29219)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
`nx g @nx/remix:lib packages/foo` generates invalid package names in new
TS setup. e.g. `@acme/packages/foo`.

## Expected Behavior
The name should be valid in `package.json`.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
jaysoo authored Dec 5, 2024
1 parent f89fca9 commit 7c25cf1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import {
} from '@nx/devkit';
import { getRootTsConfigPathInTree } from '@nx/js';
import type { RemixLibraryOptions } from './normalize-options';
import { resolveImportPath } from '@nx/devkit/src/generators/project-name-and-root-utils';

export function addTsconfigEntryPoints(
tree: Tree,
options: RemixLibraryOptions
) {
const { sourceRoot } = readProjectConfiguration(tree, options.projectName);
const { root: projectRoot, sourceRoot } = readProjectConfiguration(
tree,
options.projectName
);
const serverFilePath = joinPathFragments(sourceRoot, 'server.ts');

tree.write(
Expand All @@ -20,14 +24,13 @@ export function addTsconfigEntryPoints(
);

const baseTsConfig = getRootTsConfigPathInTree(tree);
// Use same logic as `determineProjectNameAndRootOptions` to get the import path
const importPath = resolveImportPath(tree, options.name, projectRoot);
updateJson(tree, baseTsConfig, (json) => {
if (
json.compilerOptions.paths &&
json.compilerOptions.paths[options.importPath]
) {
json.compilerOptions.paths[
joinPathFragments(options.importPath, 'server')
] = [serverFilePath];
if (json.compilerOptions.paths && json.compilerOptions.paths[importPath]) {
json.compilerOptions.paths[joinPathFragments(importPath, 'server')] = [
serverFilePath,
];
}

return json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
determineProjectNameAndRootOptions,
ensureProjectName,
} from '@nx/devkit/src/generators/project-name-and-root-utils';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
import type { NxRemixGeneratorSchema } from '../schema';

export interface RemixLibraryOptions extends NxRemixGeneratorSchema {
Expand Down Expand Up @@ -31,12 +30,9 @@ export async function normalizeOptions(
nxJson.useInferencePlugins !== false;
options.addPlugin ??= addPluginDefault;

const importPath = options.importPath ?? getImportPath(tree, projectRoot);

return {
...options,
unitTestRunner: options.unitTestRunner ?? 'vitest',
importPath,
projectName,
projectRoot,
};
Expand Down
52 changes: 51 additions & 1 deletion packages/remix/src/generators/library/library.impl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import 'nx/src/internal-testing-utils/mock-project-graph';

import { readJson, readProjectConfiguration } from '@nx/devkit';
import {
readJson,
readProjectConfiguration,
Tree,
updateJson,
writeJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import applicationGenerator from '../application/application.impl';
import libraryGenerator from './library.impl';
Expand Down Expand Up @@ -129,4 +135,48 @@ describe('Remix Library Generator', () => {
expect(pkgJson.main).toEqual('./dist/index.cjs.js');
expect(pkgJson.typings).toEqual('./dist/index.d.ts');
});
describe('TS solution setup', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
updateJson(tree, 'package.json', (json) => {
json.workspaces = ['packages/*', 'apps/*'];
return json;
});
writeJson(tree, 'tsconfig.base.json', {
compilerOptions: {
composite: true,
declaration: true,
},
});
writeJson(tree, 'tsconfig.json', {
extends: './tsconfig.base.json',
files: [],
references: [],
});
});

it('should generate valid package.json', async () => {
await libraryGenerator(tree, {
directory: 'packages/foo',
style: 'css',
addPlugin: true,
});

expect(readJson(tree, 'packages/foo/package.json'))
.toMatchInlineSnapshot(`
{
"main": "./src/index.ts",
"name": "@proj/foo",
"nx": {
"name": "foo",
"projectType": "library",
"sourceRoot": "packages/foo/src",
},
"types": "./src/index.ts",
}
`);
});
});
});
4 changes: 2 additions & 2 deletions packages/remix/src/generators/library/library.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export async function remixLibraryGeneratorInternal(
tasks.push(jsInitTask);

const libGenTask = await libraryGenerator(tree, {
name: options.projectName,
directory: options.directory,
name: options.name,
style: options.style,
unitTestRunner: options.unitTestRunner,
tags: options.tags,
importPath: options.importPath,
directory: options.projectRoot,
skipFormat: true,
skipTsConfig: false,
linter: options.linter,
Expand Down

0 comments on commit 7c25cf1

Please sign in to comment.