From 0f1d2dd9b8ec77617127b4f152d062dc5753c592 Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Mon, 4 Dec 2023 13:48:24 +0100 Subject: [PATCH] fix: ng-packagr issue with symlinked workspace components --- angular/devkit/common/utils.ts | 22 ++++++++++--------- .../ng-packagr/ng-packagr.compiler.ts | 9 ++++---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/angular/devkit/common/utils.ts b/angular/devkit/common/utils.ts index d1659c9..198e7be 100644 --- a/angular/devkit/common/utils.ts +++ b/angular/devkit/common/utils.ts @@ -16,7 +16,7 @@ import { dirname, join, posix, resolve } from 'path'; import { readConfigFile, sys } from 'typescript'; export const NG_APP_NAME = 'ng-app'; -export const NG_APP_PATTERN = `*.${NG_APP_NAME}.*`; +export const NG_APP_PATTERN = `*.${ NG_APP_NAME }.*`; export enum BundlerSetup { Serve = 'serve', @@ -44,7 +44,7 @@ export function getWorkspace(context: EnvContext): Workspace | undefined { return undefined; } -export function getNodeModulesPaths(build: boolean, isolator: IsolatorMain, workspace?: Workspace): string[] { +export function getNodeModulesPaths(build: boolean, isolator: IsolatorMain, workspace?: Workspace, capsuleOnly = false): string[] { const nodeModulesPaths: string[] = []; if (workspace) { @@ -68,7 +68,9 @@ export function getNodeModulesPaths(build: boolean, isolator: IsolatorMain, work } // Add the workspace node modules - nodeModulesPaths.push(workspaceNodeModules, 'node_modules'); + if (!capsuleOnly) { + nodeModulesPaths.push(workspaceNodeModules, 'node_modules'); + } } if (!nodeModulesPaths.includes('node_modules')) { @@ -111,7 +113,7 @@ export function cmpIdToPkgName(componentId: ComponentID) { const name = componentId.fullName.replace(allSlashes, '.'); const scope = componentId.scope.split('.').join('/'); const partsToJoin = scope ? [scope, name] : [name]; - return `@${partsToJoin.join('.')}`; + return `@${ partsToJoin.join('.') }`; } export function isBuildContext(context: DevServerContext | BundlerContext): context is BundlerContext { @@ -155,7 +157,7 @@ export function generateTsConfig( ]; tsconfigJSON.exclude = [ ...tsconfigJSON.exclude.map((file: string) => posix.join(pAppPath, file)), - ...excludePaths, + ...excludePaths ]; tsconfigJSON.compilerOptions.paths = tsPaths; @@ -191,7 +193,7 @@ export function writeTsconfig( const capsules = context.capsuleNetwork.graphCapsules; const capsule = capsules.getCapsule(component.id); if (!capsule) { - throw new Error(`No capsule found for ${component.id} in network graph`); + throw new Error(`No capsule found for ${ component.id } in network graph`); } outputPath = normalizePath(capsule.path); } else { @@ -201,8 +203,8 @@ export function writeTsconfig( } // map the package names to the workspace component paths for typescript in case a package references another local package const pkgName = pkg.getPackageName(component); - tsPaths[pkgName] = [`${outputPath}/public-api.ts`]; - tsPaths[`${pkgName}/*`] = [`${outputPath}/*`]; + tsPaths[pkgName] = [`${ outputPath }/public-api.ts`]; + tsPaths[`${ pkgName }/*`] = [`${ outputPath }/*`]; includePaths.add(outputPath); @@ -215,7 +217,7 @@ export function writeTsconfig( const content = generateTsConfig(rootPath, Array.from(includePaths), Array.from(excludePaths), tsPaths); const hash = objectHash(content); - const targetPath = join(dirPath, `tsconfig/tsconfig-${timestamp}.json`); + const targetPath = join(dirPath, `tsconfig/tsconfig-${ timestamp }.json`); // write only if the link has changed (prevents triggering fs watches) if (writeHash.get(targetPath) !== hash) { @@ -238,7 +240,7 @@ export function dedupPaths(paths: (string | any)[]): string[] { * @return {string} - The absolute path to the specified file or directory. */ export function packagePath(packageName: string, path = ''): string { - return join(dirname(require.resolve(`${packageName}/package.json`)), path); + return join(dirname(require.resolve(`${ packageName }/package.json`)), path); } /** diff --git a/angular/devkit/compiler/ng-packagr/ng-packagr.compiler.ts b/angular/devkit/compiler/ng-packagr/ng-packagr.compiler.ts index 5c04f3c..14fed97 100644 --- a/angular/devkit/compiler/ng-packagr/ng-packagr.compiler.ts +++ b/angular/devkit/compiler/ng-packagr/ng-packagr.compiler.ts @@ -201,6 +201,7 @@ export class NgPackagrCompiler implements Compiler { await packageJson.write(); // create ng-package.json config file for ngPackagr + const ngPackageJsonPath = join(pathToOutputFolder, NG_PACKAGE_JSON); const ngPackageJson: NgPackageConfig = { dest: this.distDir, assets: ['src/assets'], @@ -216,7 +217,7 @@ export class NgPackagrCompiler implements Compiler { console.warn(chalk.yellow(`\nWARNING: You have dependencies declared as "runtime dependencies" ("${ allowedNonPeerDependencies.join('", "') }"). In most cases Angular recommends using peer dependencies instead (see: https://github.com/ng-packagr/ng-packagr/blob/main/docs/dependencies.md).\n`)); } - outputFileSync(join(pathToOutputFolder, NG_PACKAGE_JSON), JSON.stringify(ngPackageJson, null, 2)); + outputFileSync(ngPackageJsonPath, JSON.stringify(ngPackageJson, null, 2)); // add all node modules paths to TypeScript paths to ensure that it finds all existing dependencies // eslint-disable-next-line no-param-reassign @@ -229,7 +230,7 @@ export class NgPackagrCompiler implements Compiler { return this.ngPackagr .withTsConfig(parsedTsConfig) - .forProject(join(pathToOutputFolder, NG_PACKAGE_JSON)) + .forProject(ngPackageJsonPath) .build() .then(async() => { // copy over properties generated by ngPackagr @@ -239,7 +240,7 @@ export class NgPackagrCompiler implements Compiler { await packageJson.write(); // delete the [ng-]package.json file generated by ngPackagr await removeFilesAndEmptyDirsRecursively([resolve(join(pathToOutputFolder, this.distDir, PACKAGE_JSON))]); - await removeFilesAndEmptyDirsRecursively([resolve(join(pathToOutputFolder, NG_PACKAGE_JSON))]); + await removeFilesAndEmptyDirsRecursively([resolve(ngPackageJsonPath)]); // eslint-disable-next-line consistent-return }, async(err: Error) => { if (err.message === ViewEngineTemplateError && !tsCompilerOptions.fullTemplateTypeCheck) { @@ -389,7 +390,7 @@ export class NgPackagrCompiler implements Compiler { const application = context.getAspect(ApplicationAspect.id); const depResolver = context.getAspect(DependencyResolverAspect.id); const isolator = context.getAspect(IsolatorAspect.id); - const nodeModulesPaths = getNodeModulesPaths(true, isolator, workspace); + const nodeModulesPaths = getNodeModulesPaths(true, isolator, workspace, true); return new NgPackagrCompiler( ngPackagrModulePath,