55 * Use of this source code is governed by an MIT-style license that can be
66 * found in the LICENSE file at https://angular.io/license
77 */
8- import * as path from 'canonical-path' ;
9- import * as yargs from 'yargs' ;
108
119import { checkMarkerFile , writeMarkerFile } from './packages/build_marker' ;
1210import { DependencyHost } from './packages/dependency_host' ;
@@ -16,72 +14,61 @@ import {makeEntryPointBundle} from './packages/entry_point_bundle';
1614import { EntryPointFinder } from './packages/entry_point_finder' ;
1715import { Transformer } from './packages/transformer' ;
1816
19- export function mainNgcc ( args : string [ ] ) : number {
20- const options =
21- yargs
22- . option ( 's' , {
23- alias : 'source' ,
24- describe : 'A path to the root folder to compile.' ,
25- default : './node_modules'
26- } )
27- . option ( 'f' , {
28- alias : 'formats' ,
29- array : true ,
30- describe : 'An array of formats to compile.' ,
31- default : [ 'fesm2015' , 'esm2015' , 'fesm5' , 'esm5' ]
32- } )
33- . option ( 't' , {
34- alias : 'target' ,
35- describe : 'A path to a root folder where the compiled files will be written.' ,
36- defaultDescription : 'The `source` folder.'
37- } )
38- . help ( )
39- . parse ( args ) ;
40-
41- const sourcePath : string = path . resolve ( options [ 's' ] ) ;
42- const formats : EntryPointFormat [ ] = options [ 'f' ] ;
43- const targetPath : string = options [ 't' ] || sourcePath ;
17+ /**
18+ * The options to configure the ngcc compiler.
19+ */
20+ export interface NgccOptions {
21+ /** The path to the node_modules folder that contains the packages to compile. */
22+ baseSourcePath : string ;
23+ /** A list of JavaScript bundle formats that should be compiled. */
24+ formats : EntryPointFormat [ ] ;
25+ /** The path to the node_modules folder where modified files should be written. */
26+ baseTargetPath ?: string ;
27+ }
4428
45- const transformer = new Transformer ( sourcePath , targetPath ) ;
29+ /**
30+ * This is the main entry-point into ngcc (aNGular Compatibility Compiler).
31+ *
32+ * You can call this function to process one or more npm packages, to ensure
33+ * that they are compatible with the ivy compiler (ngtsc).
34+ *
35+ * @param options The options telling ngcc what to compile and how.
36+ */
37+ export function mainNgcc ( { baseSourcePath, formats, baseTargetPath = baseSourcePath } : NgccOptions ) :
38+ void {
39+ const transformer = new Transformer ( baseSourcePath , baseTargetPath ) ;
4640 const host = new DependencyHost ( ) ;
4741 const resolver = new DependencyResolver ( host ) ;
4842 const finder = new EntryPointFinder ( resolver ) ;
4943
50- try {
51- const { entryPoints} = finder . findEntryPoints ( sourcePath ) ;
52- entryPoints . forEach ( entryPoint => {
44+ const { entryPoints} = finder . findEntryPoints ( baseSourcePath ) ;
45+ entryPoints . forEach ( entryPoint => {
5346
54- // Are we compiling the Angular core?
55- const isCore = entryPoint . name === '@angular/core' ;
47+ // Are we compiling the Angular core?
48+ const isCore = entryPoint . name === '@angular/core' ;
5649
57- // We transform the d.ts typings files while transforming one of the formats.
58- // This variable decides with which of the available formats to do this transform.
59- // It is marginally faster to process via the flat file if available.
60- const dtsTransformFormat : EntryPointFormat = entryPoint . fesm2015 ? 'fesm2015' : 'esm2015' ;
50+ // We transform the d.ts typings files while transforming one of the formats.
51+ // This variable decides with which of the available formats to do this transform.
52+ // It is marginally faster to process via the flat file if available.
53+ const dtsTransformFormat : EntryPointFormat = entryPoint . fesm2015 ? 'fesm2015' : 'esm2015' ;
6154
62- formats . forEach ( format => {
63- if ( checkMarkerFile ( entryPoint , format ) ) {
64- console . warn ( `Skipping ${ entryPoint . name } : ${ format } (already built).` ) ;
65- return ;
66- }
55+ formats . forEach ( format => {
56+ if ( checkMarkerFile ( entryPoint , format ) ) {
57+ console . warn ( `Skipping ${ entryPoint . name } : ${ format } (already built).` ) ;
58+ return ;
59+ }
6760
68- const bundle =
69- makeEntryPointBundle ( entryPoint , isCore , format , format === dtsTransformFormat ) ;
70- if ( bundle === null ) {
71- console . warn (
72- `Skipping ${ entryPoint . name } : ${ format } (no entry point file for this format).` ) ;
73- } else {
74- transformer . transform ( entryPoint , isCore , bundle ) ;
75- }
61+ const bundle =
62+ makeEntryPointBundle ( entryPoint , isCore , format , format === dtsTransformFormat ) ;
63+ if ( bundle === null ) {
64+ console . warn (
65+ `Skipping ${ entryPoint . name } : ${ format } (no entry point file for this format).` ) ;
66+ } else {
67+ transformer . transform ( entryPoint , isCore , bundle ) ;
68+ }
7669
77- // Write the built-with-ngcc marker
78- writeMarkerFile ( entryPoint , format ) ;
79- } ) ;
70+ // Write the built-with-ngcc marker
71+ writeMarkerFile ( entryPoint , format ) ;
8072 } ) ;
81- } catch ( e ) {
82- console . error ( e . stack || e . message ) ;
83- return 1 ;
84- }
85-
86- return 0 ;
73+ } ) ;
8774}
0 commit comments