Skip to content

Commit 3b54c12

Browse files
committed
fix package.json generated dependencies
Fixes #17
1 parent 441ca3c commit 3b54c12

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

packages/ng-packagr/ng-packagr.compiler.ts

+29-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Component } from '@teambit/component';
55
import PackageJsonFile from '@teambit/legacy/dist/consumer/component/package-json-file';
66
import AbstractVinyl from '@teambit/legacy/dist/consumer/component/sources/abstract-vinyl';
77
import DataToPersist from '@teambit/legacy/dist/consumer/component/sources/data-to-persist';
8+
import { PACKAGE_JSON } from '@teambit/legacy/dist/constants';
89
import removeFilesAndEmptyDirsRecursively from '@teambit/legacy/dist/utils/fs/remove-files-and-empty-dirs-recursively';
910
import { Logger } from '@teambit/logger';
1011
import { Workspace } from '@teambit/workspace';
@@ -59,25 +60,42 @@ export class NgPackagrCompiler implements Compiler {
5960
this.artifactName = bitCompilerOptions.artifactName || 'dist';
6061
}
6162

62-
private async ngPackagrCompilation(pathToComponent: string, pathToOutputFolder: string, tsCompilerOptions: TsCompilerOptions): Promise<void> {
63+
private async ngPackagrCompilation(pathToComponent: string, pathToOutputFolder: string, tsCompilerOptions: TsCompilerOptions, packageJson: PackageJsonFile): Promise<void> {
6364
// disable logger temporarily so that it doesn't mess up with ngPackagr logs
6465
this.logger.off();
6566

6667
// update ngPackage entry in package.json for ngPackagr
67-
const packageJson = await PackageJsonFile.load(pathToOutputFolder, '');
6868
packageJson.addOrUpdateProperty('ngPackage', {
6969
lib: {
7070
entryFile: join(pathToComponent, 'public-api.ts'),
7171
},
7272
});
73+
74+
// check for dependencies other than tslib and move them to peer dependencies
75+
// see https://github.com/ng-packagr/ng-packagr/blob/master/docs/dependencies.md#general-recommendation-use-peerdependencies-whenever-possible
76+
const dependencies = packageJson.packageJsonObject.dependencies;
77+
const peerDependencies = packageJson.packageJsonObject.peerDependencies;
78+
const dependenciesKeys = Object.keys(dependencies);
79+
if(dependenciesKeys.length > 1) {
80+
dependenciesKeys.forEach((dep: string) => {
81+
if (dep !== 'tslib') {
82+
peerDependencies[dep] = dependencies[dep];
83+
delete dependencies[dep];
84+
}
85+
});
86+
packageJson.addOrUpdateProperty('dependencies', dependencies);
87+
packageJson.addOrUpdateProperty('peerDependencies', peerDependencies);
88+
}
89+
90+
// update package.json
7391
await packageJson.write();
7492

7593
const parsedTsConfig = this.readDefaultTsConfig();
7694
parsedTsConfig.options = {...parsedTsConfig.options, ...tsCompilerOptions};
7795

7896
return this.ngPackagr
7997
.withTsConfig(parsedTsConfig)
80-
.forProject(join(pathToOutputFolder, 'package.json'))
98+
.forProject(join(pathToOutputFolder, PACKAGE_JSON))
8199
.build()
82100
.then(async () => {
83101
// copy over properties generated by ngPackagr
@@ -95,11 +113,11 @@ export class NgPackagrCompiler implements Compiler {
95113
});
96114
await packageJson.write();
97115
// delete the package.json file generated by ngPackagr
98-
await removeFilesAndEmptyDirsRecursively([resolve(join(pathToOutputFolder, 'dist', 'package.json'))]);
116+
await removeFilesAndEmptyDirsRecursively([resolve(join(pathToOutputFolder, 'dist', PACKAGE_JSON))]);
99117
}, (err: Error) => {
100118
if(err.message === ViewEngineTemplateError && !tsCompilerOptions.fullTemplateTypeCheck) {
101119
console.warn(`\nError "${err.message}" triggered by the Angular compiler, retrying compilation without "fullTemplateTypeCheck" (you should probably create a custom environment using "bit create ng-env my-custom-angular-env" to set this option by default and avoid this error message)\n`);
102-
return this.ngPackagrCompilation(pathToComponent, pathToOutputFolder, {...tsCompilerOptions, fullTemplateTypeCheck: false})
120+
return this.ngPackagrCompilation(pathToComponent, pathToOutputFolder, {...tsCompilerOptions, fullTemplateTypeCheck: false}, packageJson)
103121
}
104122
console.error(err);
105123
})
@@ -112,7 +130,10 @@ export class NgPackagrCompiler implements Compiler {
112130
* used by `bit compile`
113131
*/
114132
async transpileComponent(params: TranspileComponentParams): Promise<void> {
115-
return this.ngPackagrCompilation(params.componentDir, params.outputDir, this.tsCompilerOptions);
133+
// recreate packageJson from component to make sure that its dependencies are updated with recent code changes
134+
const packageJson = PackageJsonFile.createFromComponent('', params.component);
135+
packageJson.workspaceDir = params.outputDir;
136+
return this.ngPackagrCompilation(params.componentDir, params.outputDir, this.tsCompilerOptions, packageJson);
116137
}
117138

118139
private getArtifactDefinition() {
@@ -142,7 +163,8 @@ export class NgPackagrCompiler implements Compiler {
142163
component,
143164
};
144165
try {
145-
await this.ngPackagrCompilation(capsule.path, capsule.path, this.tsCompilerOptions);
166+
const packageJson = await PackageJsonFile.loadFromCapsuleSync(capsule.path);
167+
await this.ngPackagrCompilation(capsule.path, capsule.path, this.tsCompilerOptions, packageJson);
146168
} catch (e) {
147169
currentComponentResult.errors = [e];
148170
}

0 commit comments

Comments
 (0)