Skip to content

Commit

Permalink
fix(js): generate ts standalone project with better import path
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Jul 21, 2023
1 parent 8407d7a commit 326f5d4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
27 changes: 27 additions & 0 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
names,
offsetFromRoot,
ProjectConfiguration,
readJson,
runTasksInSerial,
toJS,
Tree,
Expand All @@ -29,6 +30,7 @@ import { Bundler, LibraryGeneratorSchema } from '../../utils/schema';
import { addSwcConfig } from '../../utils/swc/add-swc-config';
import { addSwcDependencies } from '../../utils/swc/add-swc-dependencies';
import { getImportPath } from '../../utils/get-import-path';
import { getNpmScope } from '../../utils/package-json/get-npm-scope';
import {
esbuildVersion,
nxVersion,
Expand Down Expand Up @@ -539,8 +541,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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@<%= formattedNames.fileName %>/source",
"name": "<%= formattedNames.fileName %>",
"version": "0.0.0",
"license": "MIT",
"scripts": {
Expand Down

0 comments on commit 326f5d4

Please sign in to comment.