@@ -5,6 +5,7 @@ import { Component } from '@teambit/component';
5
5
import PackageJsonFile from '@teambit/legacy/dist/consumer/component/package-json-file' ;
6
6
import AbstractVinyl from '@teambit/legacy/dist/consumer/component/sources/abstract-vinyl' ;
7
7
import DataToPersist from '@teambit/legacy/dist/consumer/component/sources/data-to-persist' ;
8
+ import { PACKAGE_JSON } from '@teambit/legacy/dist/constants' ;
8
9
import removeFilesAndEmptyDirsRecursively from '@teambit/legacy/dist/utils/fs/remove-files-and-empty-dirs-recursively' ;
9
10
import { Logger } from '@teambit/logger' ;
10
11
import { Workspace } from '@teambit/workspace' ;
@@ -59,25 +60,42 @@ export class NgPackagrCompiler implements Compiler {
59
60
this . artifactName = bitCompilerOptions . artifactName || 'dist' ;
60
61
}
61
62
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 > {
63
64
// disable logger temporarily so that it doesn't mess up with ngPackagr logs
64
65
this . logger . off ( ) ;
65
66
66
67
// update ngPackage entry in package.json for ngPackagr
67
- const packageJson = await PackageJsonFile . load ( pathToOutputFolder , '' ) ;
68
68
packageJson . addOrUpdateProperty ( 'ngPackage' , {
69
69
lib : {
70
70
entryFile : join ( pathToComponent , 'public-api.ts' ) ,
71
71
} ,
72
72
} ) ;
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
73
91
await packageJson . write ( ) ;
74
92
75
93
const parsedTsConfig = this . readDefaultTsConfig ( ) ;
76
94
parsedTsConfig . options = { ...parsedTsConfig . options , ...tsCompilerOptions } ;
77
95
78
96
return this . ngPackagr
79
97
. withTsConfig ( parsedTsConfig )
80
- . forProject ( join ( pathToOutputFolder , 'package.json' ) )
98
+ . forProject ( join ( pathToOutputFolder , PACKAGE_JSON ) )
81
99
. build ( )
82
100
. then ( async ( ) => {
83
101
// copy over properties generated by ngPackagr
@@ -95,11 +113,11 @@ export class NgPackagrCompiler implements Compiler {
95
113
} ) ;
96
114
await packageJson . write ( ) ;
97
115
// 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 ) ) ] ) ;
99
117
} , ( err : Error ) => {
100
118
if ( err . message === ViewEngineTemplateError && ! tsCompilerOptions . fullTemplateTypeCheck ) {
101
119
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 )
103
121
}
104
122
console . error ( err ) ;
105
123
} )
@@ -112,7 +130,10 @@ export class NgPackagrCompiler implements Compiler {
112
130
* used by `bit compile`
113
131
*/
114
132
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 ) ;
116
137
}
117
138
118
139
private getArtifactDefinition ( ) {
@@ -142,7 +163,8 @@ export class NgPackagrCompiler implements Compiler {
142
163
component,
143
164
} ;
144
165
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 ) ;
146
168
} catch ( e ) {
147
169
currentComponentResult . errors = [ e ] ;
148
170
}
0 commit comments