11import { task , watch } from 'gulp' ;
2- import { readdirSync , statSync , readFileSync } from 'fs' ;
32import * as path from 'path' ;
43
54import { SOURCE_ROOT , DIST_COMPONENTS_ROOT , PROJECT_ROOT } from '../constants' ;
@@ -10,31 +9,54 @@ import {writeFileSync} from 'fs';
109const inlineResources = require ( '../../../scripts/release/inline-resources' ) ;
1110const rollup = require ( 'rollup' ) . rollup ;
1211
12+
13+ // NOTE: there are two build "modes" in this file, based on which tsconfig is used.
14+ // When `tsconfig.json` is used, we are outputting ES6 modules and a UMD bundle. This is used
15+ // for serving and for release.
16+ //
17+ // When `tsconfig-spec.json` is used, we are outputting CommonJS modules. This is used
18+ // for unit tests (karma).
19+
20+ /** Path to the root of the Angular Material component library. */
1321const componentsDir = path . join ( SOURCE_ROOT , 'lib' ) ;
1422
23+ /** Path to the tsconfig used for ESM output. */
24+ const tsconfigPath = path . relative ( PROJECT_ROOT , path . join ( componentsDir , 'tsconfig.json' ) ) ;
25+
1526
27+ /** [Watch task] Rebuilds (ESM output) whenever ts, scss, or html sources change. */
1628task ( ':watch:components' , ( ) => {
1729 watch ( path . join ( componentsDir , '**/*.ts' ) , [ ':build:components:ts' ] ) ;
1830 watch ( path . join ( componentsDir , '**/*.scss' ) , [ ':build:components:scss' ] ) ;
1931 watch ( path . join ( componentsDir , '**/*.html' ) , [ ':build:components:assets' ] ) ;
2032} ) ;
2133
34+ /** [Watch task] Rebuilds for tests (CJS output) whenever ts, scss, or html sources change. */
2235task ( ':watch:components:spec' , ( ) => {
2336 watch ( path . join ( componentsDir , '**/*.ts' ) , [ ':build:components:spec' ] ) ;
2437 watch ( path . join ( componentsDir , '**/*.scss' ) , [ ':build:components:scss' ] ) ;
2538 watch ( path . join ( componentsDir , '**/*.html' ) , [ ':build:components:assets' ] ) ;
2639} ) ;
2740
2841
42+ /** Builds component typescript only (ESM output). */
2943task ( ':build:components:ts' , tsBuildTask ( componentsDir ) ) ;
44+
45+ /** Builds components typescript for tests (CJS output). */
3046task ( ':build:components:spec' , tsBuildTask ( path . join ( componentsDir , 'tsconfig-spec.json' ) ) ) ;
47+
48+ /** Copies assets (html, markdown) to build output. */
3149task ( ':build:components:assets' , copyTask ( [
3250 path . join ( componentsDir , '**/*.!(ts|spec.ts)' ) ,
3351 path . join ( PROJECT_ROOT , 'README.md' ) ,
3452] , DIST_COMPONENTS_ROOT ) ) ;
53+
54+ /** Builds scss into css. */
3555task ( ':build:components:scss' , sassBuildTask (
3656 DIST_COMPONENTS_ROOT , componentsDir , [ path . join ( componentsDir , 'core/style' ) ]
3757) ) ;
58+
59+ /** Builds the UMD bundle for all of Angular Material. */
3860task ( ':build:components:rollup' , [ ':build:components:inline' ] , ( ) => {
3961 const globals : { [ name : string ] : string } = {
4062 // Angular dependencies
@@ -85,18 +107,19 @@ task(':build:components:rollup', [':build:components:inline'], () => {
85107 } ) ;
86108} ) ;
87109
88- task ( ':build:components:inline' , [
89- ':build:components:ts' ,
90- ':build:components:scss' ,
91- ':build:components:assets'
92- ] , ( ) => {
93- return inlineResources ( DIST_COMPONENTS_ROOT ) ;
94- } ) ;
95-
96- task ( 'build:components' , sequenceTask (
97- ':build:components:rollup' ,
110+ /** Builds components with resources (html, css) inlined into the built JS (ESM output). */
111+ task ( ':build:components:inline' , sequenceTask (
112+ [ ':build:components:ts' , ':build:components:scss' , ':build:components:assets' ] ,
113+ ':inline-resources' ,
98114) ) ;
99115
116+ /** Inlines resources (html, css) into the JS output (for either ESM or CJS output). */
117+ task ( ':inline-resources' , ( ) => inlineResources ( DIST_COMPONENTS_ROOT ) ) ;
118+
119+ /** Builds components to ESM output and UMD bundle. */
120+ task ( 'build:components' , [ ':build:components:rollup' ] ) ;
121+
122+ /** Generates metadata.json files for all of the components. */
100123task ( ':build:components:ngc' , [ 'build:components' ] , execNodeTask (
101- '@angular/compiler-cli' , 'ngc' , [ '-p' , path . relative ( PROJECT_ROOT , path . join ( componentsDir , 'tsconfig.json' ) ) ]
124+ '@angular/compiler-cli' , 'ngc' , [ '-p' , tsconfigPath ]
102125) ) ;
0 commit comments