Skip to content

Commit

Permalink
fix: ng-packagr issue with symlinked workspace components
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Dec 4, 2023
1 parent be8304b commit 0f1d2dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
22 changes: 12 additions & 10 deletions angular/devkit/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand All @@ -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')) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -155,7 +157,7 @@ export function generateTsConfig(
];
tsconfigJSON.exclude = [
...tsconfigJSON.exclude.map((file: string) => posix.join(pAppPath, file)),
...excludePaths,
...excludePaths
];
tsconfigJSON.compilerOptions.paths = tsPaths;

Expand Down Expand Up @@ -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 {
Expand All @@ -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);

Expand All @@ -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) {
Expand All @@ -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);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions angular/devkit/compiler/ng-packagr/ng-packagr.compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -389,7 +390,7 @@ export class NgPackagrCompiler implements Compiler {
const application = context.getAspect<ApplicationMain>(ApplicationAspect.id);
const depResolver = context.getAspect<DependencyResolverMain>(DependencyResolverAspect.id);
const isolator = context.getAspect<IsolatorMain>(IsolatorAspect.id);
const nodeModulesPaths = getNodeModulesPaths(true, isolator, workspace);
const nodeModulesPaths = getNodeModulesPaths(true, isolator, workspace, true);

return new NgPackagrCompiler(
ngPackagrModulePath,
Expand Down

0 comments on commit 0f1d2dd

Please sign in to comment.