Skip to content

Commit 212b062

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

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

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

+29-8
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';
@@ -48,7 +49,6 @@ export class NgPackagrCompiler implements Compiler {
4849
private logger: Logger,
4950
private workspace: Workspace,
5051
private readDefaultTsConfig: (filename?: string) => any,
51-
// TODO(ocombe): use this to support custom tsConfig
5252
private tsCompilerOptions: TsCompilerOptions = {},
5353
private bitCompilerOptions: Partial<CompilerOptions> = {}
5454
) {
@@ -59,25 +59,42 @@ export class NgPackagrCompiler implements Compiler {
5959
this.artifactName = bitCompilerOptions.artifactName || 'dist';
6060
}
6161

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

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

7592
const parsedTsConfig = this.readDefaultTsConfig();
7693
parsedTsConfig.options = {...parsedTsConfig.options, ...tsCompilerOptions};
7794

7895
return this.ngPackagr
7996
.withTsConfig(parsedTsConfig)
80-
.forProject(join(pathToOutputFolder, 'package.json'))
97+
.forProject(join(pathToOutputFolder, PACKAGE_JSON))
8198
.build()
8299
.then(async () => {
83100
// copy over properties generated by ngPackagr
@@ -95,11 +112,11 @@ export class NgPackagrCompiler implements Compiler {
95112
});
96113
await packageJson.write();
97114
// delete the package.json file generated by ngPackagr
98-
await removeFilesAndEmptyDirsRecursively([resolve(join(pathToOutputFolder, 'dist', 'package.json'))]);
115+
await removeFilesAndEmptyDirsRecursively([resolve(join(pathToOutputFolder, 'dist', PACKAGE_JSON))]);
99116
}, (err: Error) => {
100117
if(err.message === ViewEngineTemplateError && !tsCompilerOptions.fullTemplateTypeCheck) {
101118
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})
119+
return this.ngPackagrCompilation(pathToComponent, pathToOutputFolder, {...tsCompilerOptions, fullTemplateTypeCheck: false}, packageJson)
103120
}
104121
console.error(err);
105122
})
@@ -112,7 +129,10 @@ export class NgPackagrCompiler implements Compiler {
112129
* used by `bit compile`
113130
*/
114131
async transpileComponent(params: TranspileComponentParams): Promise<void> {
115-
return this.ngPackagrCompilation(params.componentDir, params.outputDir, this.tsCompilerOptions);
132+
// recreate packageJson from component to make sure that its dependencies are updated with recent code changes
133+
const packageJson = PackageJsonFile.createFromComponent('', params.component);
134+
packageJson.workspaceDir = params.outputDir;
135+
return this.ngPackagrCompilation(params.componentDir, params.outputDir, this.tsCompilerOptions, packageJson);
116136
}
117137

118138
private getArtifactDefinition() {
@@ -142,7 +162,8 @@ export class NgPackagrCompiler implements Compiler {
142162
component,
143163
};
144164
try {
145-
await this.ngPackagrCompilation(capsule.path, capsule.path, this.tsCompilerOptions);
165+
const packageJson = await PackageJsonFile.loadFromCapsuleSync(capsule.path);
166+
await this.ngPackagrCompilation(capsule.path, capsule.path, this.tsCompilerOptions, packageJson);
146167
} catch (e) {
147168
currentComponentResult.errors = [e];
148169
}

0 commit comments

Comments
 (0)