From 020c1d8cfa9cbdd48c189017cb251b6c2e1936b2 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 11 May 2018 10:23:05 +0100 Subject: [PATCH] fix(@schematics/angular): fix scoped library paths Partially address https://github.com/angular/angular-cli/issues/10615 --- .../files/__projectRoot__/ng-package.json | 4 ++-- .../__projectRoot__/ng-package.prod.json | 4 ++-- packages/schematics/angular/library/index.ts | 23 ++++++++++--------- .../schematics/angular/library/index_spec.ts | 6 ++++- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/schematics/angular/library/files/__projectRoot__/ng-package.json b/packages/schematics/angular/library/files/__projectRoot__/ng-package.json index 97c51503bb..96795b12e9 100644 --- a/packages/schematics/angular/library/files/__projectRoot__/ng-package.json +++ b/packages/schematics/angular/library/files/__projectRoot__/ng-package.json @@ -1,6 +1,6 @@ { - "$schema": "<%= projectRoot.split('/').map(x => '..').join('/') %>/node_modules/ng-packagr/ng-package.schema.json", - "dest": "<%= projectRoot.split('/').map(x => '..').join('/') %>/dist/<%= dasherize(packageName) %>", + "$schema": "<%= relativePathToWorkspaceRoot %>/node_modules/ng-packagr/ng-package.schema.json", + "dest": "<%= relativePathToWorkspaceRoot %>/<%= distRoot %>", "deleteDestPath": false, "lib": { "entryFile": "src/<%= entryFile %>.ts" diff --git a/packages/schematics/angular/library/files/__projectRoot__/ng-package.prod.json b/packages/schematics/angular/library/files/__projectRoot__/ng-package.prod.json index f41a75a4cc..c0f915caf7 100644 --- a/packages/schematics/angular/library/files/__projectRoot__/ng-package.prod.json +++ b/packages/schematics/angular/library/files/__projectRoot__/ng-package.prod.json @@ -1,6 +1,6 @@ { - "$schema": "<%= projectRoot.split('/').map(x => '..').join('/') %>/node_modules/ng-packagr/ng-package.schema.json", - "dest": "<%= projectRoot.split('/').map(x => '..').join('/') %>/dist/lib", + "$schema": "<%= relativePathToWorkspaceRoot %>/node_modules/ng-packagr/ng-package.schema.json", + "dest": "<%= relativePathToWorkspaceRoot %>/<%= distRoot %>", "lib": { "entryFile": "src/<%= entryFile %>.ts" } diff --git a/packages/schematics/angular/library/index.ts b/packages/schematics/angular/library/index.ts index 10fb3736e1..71d25e8e4e 100644 --- a/packages/schematics/angular/library/index.ts +++ b/packages/schematics/angular/library/index.ts @@ -69,7 +69,7 @@ function updateJsonFile(host: Tree, path: string, callback: UpdateJsonFn): return host; } -function updateTsConfig(npmPackageName: string) { +function updateTsConfig(packageName: string, distRoot: string) { return (host: Tree) => { if (!host.exists('tsconfig.json')) { return host; } @@ -78,10 +78,10 @@ function updateTsConfig(npmPackageName: string) { if (!tsconfig.compilerOptions.paths) { tsconfig.compilerOptions.paths = {}; } - if (!tsconfig.compilerOptions.paths[npmPackageName]) { - tsconfig.compilerOptions.paths[npmPackageName] = []; + if (!tsconfig.compilerOptions.paths[packageName]) { + tsconfig.compilerOptions.paths[packageName] = []; } - tsconfig.compilerOptions.paths[npmPackageName].push(`dist/${npmPackageName}`); + tsconfig.compilerOptions.paths[packageName].push(distRoot); }); }; } @@ -183,7 +183,7 @@ export default function (options: LibraryOptions): Rule { // If scoped project (i.e. "@foo/bar"), convert projectDir to "foo/bar". const packageName = options.name; - let scopeName = ''; + let scopeName = null; if (/^@.*\/.*/.test(options.name)) { const [scope, name] = options.name.split('/'); scopeName = scope.replace(/^@/, ''); @@ -192,11 +192,11 @@ export default function (options: LibraryOptions): Rule { const workspace = getWorkspace(host); const newProjectRoot = workspace.newProjectRoot; - let projectRoot = `${newProjectRoot}/${strings.dasherize(options.name)}`; - if (scopeName) { - projectRoot = - `${newProjectRoot}/${strings.dasherize(scopeName)}/${strings.dasherize(options.name)}`; - } + + const scopeFolder = scopeName ? strings.dasherize(scopeName) + '/' : ''; + const folderName = `${scopeFolder}${strings.dasherize(options.name)}`; + const projectRoot = `${newProjectRoot}/${folderName}`; + const distRoot = `dist/${folderName}`; const sourceDir = `${projectRoot}/src/lib`; const relativePathToWorkspaceRoot = projectRoot.split('/').map(x => '..').join('/'); @@ -207,6 +207,7 @@ export default function (options: LibraryOptions): Rule { ...options, packageName, projectRoot, + distRoot, relativePathToWorkspaceRoot, prefix, }), @@ -219,7 +220,7 @@ export default function (options: LibraryOptions): Rule { branchAndMerge(mergeWith(templateSource)), addAppToWorkspaceFile(options, workspace, projectRoot, packageName), options.skipPackageJson ? noop() : addDependenciesToPackageJson(), - options.skipTsConfig ? noop() : updateTsConfig(options.name), + options.skipTsConfig ? noop() : updateTsConfig(packageName, distRoot), schematic('module', { name: options.name, commonModule: false, diff --git a/packages/schematics/angular/library/index_spec.ts b/packages/schematics/angular/library/index_spec.ts index fe0e87dad1..54f299ffc0 100644 --- a/packages/schematics/angular/library/index_spec.ts +++ b/packages/schematics/angular/library/index_spec.ts @@ -240,11 +240,15 @@ describe('Library Schematic', () => { const cfg = JSON.parse(tree.readContent('/angular.json')); expect(cfg.projects['@myscope/mylib']).toBeDefined(); + + const rootTsCfg = JSON.parse(tree.readContent('/tsconfig.json')); + expect(rootTsCfg.compilerOptions.paths['@myscope/mylib']).toEqual(['dist/myscope/mylib']); }); it(`should dasherize scoped libraries`, () => { const scopedName = '@myScope/myLib'; const expectedScopeName = '@my-scope/my-lib'; + const expectedFolderName = 'my-scope/my-lib'; const options = { ...defaultOptions, name: scopedName }; const tree = schematicRunner.runSchematic('library', options, workspaceTree); @@ -252,7 +256,7 @@ describe('Library Schematic', () => { expect(tree.readContent(pkgJsonPath)).toContain(expectedScopeName); const ngPkgJsonPath = '/projects/my-scope/my-lib/ng-package.json'; - expect(tree.readContent(ngPkgJsonPath)).toContain(expectedScopeName); + expect(tree.readContent(ngPkgJsonPath)).toContain(expectedFolderName); const pkgJson = JSON.parse(tree.readContent(pkgJsonPath)); expect(pkgJson.name).toEqual(expectedScopeName);