From 810b584c334fd0deff178bba04d0884ea16f069a Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Tue, 25 Jul 2023 16:35:29 -0400 Subject: [PATCH] fix(js): generate ts standalone project with better import path (#18197) --- e2e/storybook/src/storybook-nested.test.ts | 2 +- .../js/src/generators/library/library.spec.ts | 27 +++++++++++++++++++ packages/js/src/generators/library/library.ts | 8 +++--- .../new/files-root-app/package.json__tmpl__ | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/e2e/storybook/src/storybook-nested.test.ts b/e2e/storybook/src/storybook-nested.test.ts index c4377052361bc..a40db2abfcd3f 100644 --- a/e2e/storybook/src/storybook-nested.test.ts +++ b/e2e/storybook/src/storybook-nested.test.ts @@ -79,7 +79,7 @@ describe('Storybook generators and executors for standalone workspaces - using R writeFileSync( tmpProjPath(`src/app/test-button.tsx`), ` - import { MyLib } from '@${appName}/my-lib'; + import { MyLib } from 'my-lib'; export function TestButton() { return ( diff --git a/packages/js/src/generators/library/library.spec.ts b/packages/js/src/generators/library/library.spec.ts index e0b546f1df59d..97a91e97cb737 100644 --- a/packages/js/src/generators/library/library.spec.ts +++ b/packages/js/src/generators/library/library.spec.ts @@ -383,6 +383,33 @@ describe('lib', () => { expect.assertions(1); }); + + it('should provide a default import path using npm scope', async () => { + await libraryGenerator(tree, { + ...defaultOptions, + name: 'myLib', + }); + + const tsconfigJson = readJson(tree, '/tsconfig.base.json'); + expect( + tsconfigJson.compilerOptions.paths['@proj/my-lib'] + ).toBeDefined(); + }); + + it('should read import path from existing name in package.json', async () => { + updateJson(tree, 'package.json', (json) => { + json.name = '@acme/core'; + return json; + }); + await libraryGenerator(tree, { + ...defaultOptions, + rootProject: true, + name: 'myLib', + }); + + const tsconfigJson = readJson(tree, '/tsconfig.base.json'); + expect(tsconfigJson.compilerOptions.paths['@acme/core']).toBeDefined(); + }); }); describe('--pascalCaseFiles', () => { diff --git a/packages/js/src/generators/library/library.ts b/packages/js/src/generators/library/library.ts index 550f12f33f421..3a84b740f25a6 100644 --- a/packages/js/src/generators/library/library.ts +++ b/packages/js/src/generators/library/library.ts @@ -12,6 +12,7 @@ import { names, offsetFromRoot, ProjectConfiguration, + readJson, runTasksInSerial, toJS, Tree, @@ -35,8 +36,6 @@ import { swcHelpersVersion, tsLibVersion, typesNodeVersion, - swcNodeVersion, - swcCoreVersion, } from '../../utils/versions'; import jsInitGenerator from '../init/init'; import { type PackageJson } from 'nx/src/utils/package-json'; @@ -539,8 +538,9 @@ function normalizeOptions( ? options.tags.split(',').map((s) => s.trim()) : []; - const importPath = - options.importPath || getImportPath(tree, projectDirectory); + const importPath = options.rootProject + ? readJson(tree, 'package.json').name ?? getImportPath(tree, 'core') + : options.importPath || getImportPath(tree, projectDirectory); options.minimal ??= false; diff --git a/packages/workspace/src/generators/new/files-root-app/package.json__tmpl__ b/packages/workspace/src/generators/new/files-root-app/package.json__tmpl__ index f1c9ad1d5c23e..43ba92facc48f 100644 --- a/packages/workspace/src/generators/new/files-root-app/package.json__tmpl__ +++ b/packages/workspace/src/generators/new/files-root-app/package.json__tmpl__ @@ -1,5 +1,5 @@ { - "name": "@<%= formattedNames.fileName %>/source", + "name": "<%= formattedNames.fileName %>", "version": "0.0.0", "license": "MIT", "scripts": {