Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit d715cc9

Browse files
filipesilvaBrocco
authored andcommitted
fix(@schematics/angular): fix scoped library paths
Partially address angular/angular-cli#10615
1 parent ddf7edd commit d715cc9

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

packages/schematics/angular/library/files/__projectRoot__/ng-package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"$schema": "<%= projectRoot.split('/').map(x => '..').join('/') %>/node_modules/ng-packagr/ng-package.schema.json",
3-
"dest": "<%= projectRoot.split('/').map(x => '..').join('/') %>/dist/<%= dasherize(packageName) %>",
2+
"$schema": "<%= relativePathToWorkspaceRoot %>/node_modules/ng-packagr/ng-package.schema.json",
3+
"dest": "<%= relativePathToWorkspaceRoot %>/<%= distRoot %>",
44
"deleteDestPath": false,
55
"lib": {
66
"entryFile": "src/<%= entryFile %>.ts"

packages/schematics/angular/library/files/__projectRoot__/ng-package.prod.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"$schema": "<%= projectRoot.split('/').map(x => '..').join('/') %>/node_modules/ng-packagr/ng-package.schema.json",
3-
"dest": "<%= projectRoot.split('/').map(x => '..').join('/') %>/dist/lib",
2+
"$schema": "<%= relativePathToWorkspaceRoot %>/node_modules/ng-packagr/ng-package.schema.json",
3+
"dest": "<%= relativePathToWorkspaceRoot %>/<%= distRoot %>",
44
"lib": {
55
"entryFile": "src/<%= entryFile %>.ts"
66
}

packages/schematics/angular/library/index.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function updateJsonFile<T>(host: Tree, path: string, callback: UpdateJsonFn<T>):
6969
return host;
7070
}
7171

72-
function updateTsConfig(npmPackageName: string) {
72+
function updateTsConfig(packageName: string, distRoot: string) {
7373

7474
return (host: Tree) => {
7575
if (!host.exists('tsconfig.json')) { return host; }
@@ -78,10 +78,10 @@ function updateTsConfig(npmPackageName: string) {
7878
if (!tsconfig.compilerOptions.paths) {
7979
tsconfig.compilerOptions.paths = {};
8080
}
81-
if (!tsconfig.compilerOptions.paths[npmPackageName]) {
82-
tsconfig.compilerOptions.paths[npmPackageName] = [];
81+
if (!tsconfig.compilerOptions.paths[packageName]) {
82+
tsconfig.compilerOptions.paths[packageName] = [];
8383
}
84-
tsconfig.compilerOptions.paths[npmPackageName].push(`dist/${npmPackageName}`);
84+
tsconfig.compilerOptions.paths[packageName].push(distRoot);
8585
});
8686
};
8787
}
@@ -183,7 +183,7 @@ export default function (options: LibraryOptions): Rule {
183183

184184
// If scoped project (i.e. "@foo/bar"), convert projectDir to "foo/bar".
185185
const packageName = options.name;
186-
let scopeName = '';
186+
let scopeName = null;
187187
if (/^@.*\/.*/.test(options.name)) {
188188
const [scope, name] = options.name.split('/');
189189
scopeName = scope.replace(/^@/, '');
@@ -192,11 +192,11 @@ export default function (options: LibraryOptions): Rule {
192192

193193
const workspace = getWorkspace(host);
194194
const newProjectRoot = workspace.newProjectRoot;
195-
let projectRoot = `${newProjectRoot}/${strings.dasherize(options.name)}`;
196-
if (scopeName) {
197-
projectRoot =
198-
`${newProjectRoot}/${strings.dasherize(scopeName)}/${strings.dasherize(options.name)}`;
199-
}
195+
196+
const scopeFolder = scopeName ? strings.dasherize(scopeName) + '/' : '';
197+
const folderName = `${scopeFolder}${strings.dasherize(options.name)}`;
198+
const projectRoot = `${newProjectRoot}/${folderName}`;
199+
const distRoot = `dist/${folderName}`;
200200

201201
const sourceDir = `${projectRoot}/src/lib`;
202202
const relativePathToWorkspaceRoot = projectRoot.split('/').map(x => '..').join('/');
@@ -207,6 +207,7 @@ export default function (options: LibraryOptions): Rule {
207207
...options,
208208
packageName,
209209
projectRoot,
210+
distRoot,
210211
relativePathToWorkspaceRoot,
211212
prefix,
212213
}),
@@ -219,7 +220,7 @@ export default function (options: LibraryOptions): Rule {
219220
branchAndMerge(mergeWith(templateSource)),
220221
addAppToWorkspaceFile(options, workspace, projectRoot, packageName),
221222
options.skipPackageJson ? noop() : addDependenciesToPackageJson(),
222-
options.skipTsConfig ? noop() : updateTsConfig(options.name),
223+
options.skipTsConfig ? noop() : updateTsConfig(packageName, distRoot),
223224
schematic('module', {
224225
name: options.name,
225226
commonModule: false,

packages/schematics/angular/library/index_spec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,23 @@ describe('Library Schematic', () => {
240240

241241
const cfg = JSON.parse(tree.readContent('/angular.json'));
242242
expect(cfg.projects['@myscope/mylib']).toBeDefined();
243+
244+
const rootTsCfg = JSON.parse(tree.readContent('/tsconfig.json'));
245+
expect(rootTsCfg.compilerOptions.paths['@myscope/mylib']).toEqual(['dist/myscope/mylib']);
243246
});
244247

245248
it(`should dasherize scoped libraries`, () => {
246249
const scopedName = '@myScope/myLib';
247250
const expectedScopeName = '@my-scope/my-lib';
251+
const expectedFolderName = 'my-scope/my-lib';
248252
const options = { ...defaultOptions, name: scopedName };
249253
const tree = schematicRunner.runSchematic('library', options, workspaceTree);
250254

251255
const pkgJsonPath = '/projects/my-scope/my-lib/package.json';
252256
expect(tree.readContent(pkgJsonPath)).toContain(expectedScopeName);
253257

254258
const ngPkgJsonPath = '/projects/my-scope/my-lib/ng-package.json';
255-
expect(tree.readContent(ngPkgJsonPath)).toContain(expectedScopeName);
259+
expect(tree.readContent(ngPkgJsonPath)).toContain(expectedFolderName);
256260

257261
const pkgJson = JSON.parse(tree.readContent(pkgJsonPath));
258262
expect(pkgJson.name).toEqual(expectedScopeName);

0 commit comments

Comments
 (0)