1
- import { red } from 'chalk' ;
2
- import { spawn } from 'child_process' ;
3
- import { readFileSync , writeFileSync } from 'fs' ;
4
- import { sync as glob } from 'glob' ;
5
- import { join , resolve as resolvePath } from 'path' ;
1
+ import { join } from 'path' ;
6
2
import { main as ngc } from '@angular/tsc-wrapped' ;
7
3
import { PackageBundler } from './build-bundles' ;
8
4
import { buildConfig } from './build-config' ;
9
5
import { getSecondaryEntryPointsForPackage } from './secondary-entry-points' ;
10
-
6
+ import { compileEntryPoint , renamePrivateReExportsToBeUnique } from './compile-entry-point' ;
11
7
12
8
const { packagesDir, outputDir} = buildConfig ;
13
9
@@ -17,9 +13,6 @@ const buildTsconfigName = 'tsconfig-build.json';
17
13
/** Name of the tsconfig file that is responsible for building the tests. */
18
14
const testsTsconfigName = 'tsconfig-tests.json' ;
19
15
20
- /** Incrementing ID counter. */
21
- let nextId = 0 ;
22
-
23
16
export class BuildPackage {
24
17
/** Path to the package sources. */
25
18
sourceDir : string ;
@@ -30,6 +23,9 @@ export class BuildPackage {
30
23
/** Whether this package will re-export its secondary-entry points at the root module. */
31
24
exportsSecondaryEntryPointsAtRoot = false ;
32
25
26
+ /** Whether the secondary entry-point styles should be copied to the release output. */
27
+ copySecondaryEntryPointStylesToRoot = false ;
28
+
33
29
/** Path to the entry file of the package in the output directory. */
34
30
readonly entryFilePath : string ;
35
31
@@ -39,22 +35,22 @@ export class BuildPackage {
39
35
/** Path to the tsconfig file, which will be used to build the tests. */
40
36
private readonly tsconfigTests : string ;
41
37
42
- private _secondaryEntryPoints : string [ ] ;
43
- private _secondaryEntryPointsByDepth : string [ ] [ ] ;
38
+ /** Package bundler instance. */
39
+ private bundler = new PackageBundler ( this ) ;
44
40
45
41
/** Secondary entry-points partitioned by their build depth. */
46
42
private get secondaryEntryPointsByDepth ( ) : string [ ] [ ] {
47
43
this . cacheSecondaryEntryPoints ( ) ;
48
44
return this . _secondaryEntryPointsByDepth ;
49
45
}
50
-
51
- private readonly bundler = new PackageBundler ( this ) ;
46
+ private _secondaryEntryPointsByDepth : string [ ] [ ] ;
52
47
53
48
/** Secondary entry points for the package. */
54
49
get secondaryEntryPoints ( ) : string [ ] {
55
50
this . cacheSecondaryEntryPoints ( ) ;
56
51
return this . _secondaryEntryPoints ;
57
52
}
53
+ private _secondaryEntryPoints : string [ ] ;
58
54
59
55
constructor ( public readonly name : string , public readonly dependencies : BuildPackage [ ] = [ ] ) {
60
56
this . sourceDir = join ( packagesDir , name ) ;
@@ -74,11 +70,11 @@ export class BuildPackage {
74
70
// Depth 1: a11y, scrolling
75
71
// Depth 2: overlay
76
72
for ( const entryPointGroup of this . secondaryEntryPointsByDepth ) {
77
- await Promise . all ( entryPointGroup . map ( p => this . _compileEntryPoint ( buildTsconfigName , p ) ) ) ;
73
+ await Promise . all ( entryPointGroup . map ( p => compileEntryPoint ( this , buildTsconfigName , p ) ) ) ;
78
74
}
79
75
80
76
// Compile the primary entry-point.
81
- await this . _compileEntryPoint ( buildTsconfigName ) ;
77
+ await compileEntryPoint ( this , buildTsconfigName ) ;
82
78
}
83
79
84
80
/** Compiles the TypeScript test source files for the package. */
@@ -91,50 +87,13 @@ export class BuildPackage {
91
87
await this . bundler . createBundles ( ) ;
92
88
}
93
89
94
- /** Compiles the TypeScript sources of a primary or secondary entry point. */
95
- private async _compileEntryPoint ( tsconfigName : string , secondaryEntryPoint = '' ) {
96
- const entryPointPath = join ( this . sourceDir , secondaryEntryPoint ) ;
97
- const entryPointTsconfigPath = join ( entryPointPath , tsconfigName ) ;
98
-
99
- return new Promise ( ( resolve , reject ) => {
100
- const ngcPath = resolvePath ( './node_modules/.bin/ngc' ) ;
101
- const childProcess = spawn ( ngcPath , [ '-p' , entryPointTsconfigPath ] , { shell : true } ) ;
102
-
103
- // Pipe stdout and stderr from the child process.
104
- childProcess . stdout . on ( 'data' , ( data : any ) => console . log ( `${ data } ` ) ) ;
105
- childProcess . stderr . on ( 'data' , ( data : any ) => console . error ( red ( `${ data } ` ) ) ) ;
106
-
107
- childProcess . on ( 'exit' , ( exitCode : number ) => exitCode === 0 ? resolve ( ) : reject ( ) ) ;
108
- } )
109
- . catch ( ( ) => console . error ( red ( `Failed to compile ${ secondaryEntryPoint } ` ) ) )
110
- . then ( ( ) => this . renamePrivateReExportsToBeUnique ( secondaryEntryPoint ) ) ;
111
- }
112
-
113
90
/** Compiles the TypeScript sources of a primary or secondary entry point. */
114
91
private async _compileTestEntryPoint ( tsconfigName : string , secondaryEntryPoint = '' ) {
115
92
const entryPointPath = join ( this . sourceDir , secondaryEntryPoint ) ;
116
93
const entryPointTsconfigPath = join ( entryPointPath , tsconfigName ) ;
117
94
118
95
await ngc ( entryPointTsconfigPath , { basePath : entryPointPath } ) ;
119
- this . renamePrivateReExportsToBeUnique ( secondaryEntryPoint ) ;
120
- }
121
-
122
- /** Renames `ɵa`-style re-exports generated by Angular to be unique across compilation units. */
123
- private renamePrivateReExportsToBeUnique ( secondaryEntryPoint = '' ) {
124
- // When we compiled the typescript sources with ngc, we do entry-point individually.
125
- // If the root-level module re-exports multiple of these entry-points, the private-export
126
- // identifiers (e.g., `ɵa`) generated by ngc will collide. We work around this by suffixing
127
- // each of these identifiers with an ID specific to this entry point. We make this
128
- // replacement in the js, d.ts, and metadata output.
129
- if ( this . exportsSecondaryEntryPointsAtRoot && secondaryEntryPoint ) {
130
- const entryPointId = nextId ++ ;
131
- const outputPath = join ( this . outputDir , secondaryEntryPoint ) ;
132
- glob ( join ( outputPath , '**/*.+(js|d.ts|metadata.json)' ) ) . forEach ( filePath => {
133
- let fileContent = readFileSync ( filePath , 'utf-8' ) ;
134
- fileContent = fileContent . replace ( / ( ɵ [ a - z ] + ) / g, `$1${ entryPointId } ` ) ;
135
- writeFileSync ( filePath , fileContent , 'utf-8' ) ;
136
- } ) ;
137
- }
96
+ renamePrivateReExportsToBeUnique ( this , secondaryEntryPoint ) ;
138
97
}
139
98
140
99
/** Stores the secondary entry-points for this package if they haven't been computed already. */
0 commit comments