|
1 | 1 | import {task, watch, src, dest} from 'gulp'; |
| 2 | +import {ScriptTarget, ModuleKind} from 'typescript'; |
2 | 3 | import * as path from 'path'; |
3 | 4 |
|
4 | 5 | import { |
5 | 6 | DIST_COMPONENTS_ROOT, PROJECT_ROOT, COMPONENTS_DIR, HTML_MINIFIER_OPTIONS, LICENSE_BANNER |
6 | 7 | } from '../constants'; |
7 | 8 | import { |
8 | | - sassBuildTask, tsBuildTask, execNodeTask, copyTask, sequenceTask, |
| 9 | + sassBuildTask, tsBuildTask, execNodeTask, sequenceTask, |
9 | 10 | triggerLivereload |
10 | 11 | } from '../task_helpers'; |
11 | 12 |
|
12 | | -// No typings for these. |
| 13 | +// These imports lack of types. |
13 | 14 | const inlineResources = require('../../../scripts/release/inline-resources'); |
14 | 15 | const gulpRollup = require('gulp-better-rollup'); |
15 | | -const gulpMinifyCss = require('gulp-clean-css'); |
16 | 16 | const gulpMinifyHtml = require('gulp-htmlmin'); |
17 | 17 | const gulpIf = require('gulp-if'); |
18 | | -const merge2 = require('merge2'); |
19 | 18 |
|
| 19 | +/** Path to tsconfig file for the components. */ |
| 20 | +const tsconfigPath = path.join(COMPONENTS_DIR, 'tsconfig-build.json'); |
20 | 21 |
|
21 | | -// NOTE: there are two build "modes" in this file, based on which tsconfig is used. |
22 | | -// When `tsconfig.json` is used, we are outputting ES6 modules and a UMD bundle. This is used |
23 | | -// for serving and for release. |
24 | | -// |
25 | | -// When `tsconfig-spec.json` is used, we are outputting CommonJS modules. This is used |
26 | | -// for unit tests (karma). |
| 22 | +/** Asset files to be added to the components output. */ |
| 23 | +const assetFiles = [ |
| 24 | + path.join(COMPONENTS_DIR, '**/*.html'), |
| 25 | + path.join(COMPONENTS_DIR, 'package.json'), |
| 26 | + path.join(PROJECT_ROOT, 'README.md'), |
| 27 | + path.join(PROJECT_ROOT, 'LICENSE'), |
| 28 | +]; |
27 | 29 |
|
28 | | -/** Path to the different tsconfig files. */ |
29 | | -const tsconfigES6 = path.join(COMPONENTS_DIR, 'tsconfig-es6.json'); |
30 | | -const tsconfigES5 = path.join(COMPONENTS_DIR, 'tsconfig-es5.json'); |
31 | | - |
32 | | -/** [Watch task] Rebuilds (ESM output) whenever ts, scss, or html sources change. */ |
33 | | -task(':watch:components', () => { |
34 | | - watch(path.join(COMPONENTS_DIR, '**/*.ts'), ['build:components', triggerLivereload]); |
35 | | - watch(path.join(COMPONENTS_DIR, '**/*.scss'), ['build:components', triggerLivereload]); |
36 | | - watch(path.join(COMPONENTS_DIR, '**/*.html'), ['build:components', triggerLivereload]); |
37 | | -}); |
| 30 | +/** Builds components to UMD bundle. */ |
| 31 | +task('build:components', [':build:components:bundle:umd']); |
38 | 32 |
|
| 33 | +/** Builds components for Angular Material releases */ |
| 34 | +task(':build:components:release', sequenceTask( |
| 35 | + ':build:components:bundle:umd', |
| 36 | + ':build:components:bundle:es', |
| 37 | + ':build:components:ngc' |
| 38 | +)); |
39 | 39 |
|
40 | | -/** Builds component typescript only (ES6 output). */ |
41 | | -task(':build:components:ts', tsBuildTask(tsconfigES6)); |
| 40 | +/** Builds components typescript in ES5, ES6 target. For specs Karma needs CJS output. */ |
| 41 | +task(':build:components:ts:es5', tsBuildTask(tsconfigPath, { target: ScriptTarget.ES5 })); |
| 42 | +task(':build:components:ts:es6', tsBuildTask(tsconfigPath, { target: ScriptTarget.ES6 })); |
| 43 | +task(':build:components:ts:spec', tsBuildTask(tsconfigPath, { |
| 44 | + target: ScriptTarget.ES5, module: ModuleKind.CommonJS |
| 45 | +})); |
42 | 46 |
|
43 | | -/** Builds components typescript for tests (ES5 output). */ |
44 | | -task(':build:components:spec', tsBuildTask(tsconfigES5)); |
| 47 | +/** Tasks to create a UMD or ES bundle */ |
| 48 | +task(':build:components:bundle:umd', sequenceTask( |
| 49 | + ':build:components:ts:es5', ':build:components:inline', ':build:components:rollup:umd' |
| 50 | +)); |
45 | 51 |
|
46 | | -/** Copies assets (html, markdown) to build output. */ |
47 | | -task(':build:components:assets', copyTask([ |
48 | | - path.join(COMPONENTS_DIR, '**/*.!(ts|spec.ts)'), |
49 | | - path.join(PROJECT_ROOT, 'README.md'), |
50 | | - path.join(PROJECT_ROOT, 'LICENSE'), |
51 | | -], DIST_COMPONENTS_ROOT)); |
| 52 | +task(':build:components:bundle:es', sequenceTask( |
| 53 | + ':build:components:ts:es6', ':build:components:inline', ':build:components:rollup:es' |
| 54 | +)); |
52 | 55 |
|
53 | | -/** Minifies the HTML and CSS assets in the distribution folder. */ |
54 | | -task(':build:components:assets:minify', () => { |
55 | | - return src('**/*.+(html|css)', { cwd: DIST_COMPONENTS_ROOT}) |
56 | | - .pipe(gulpIf(/.css$/, gulpMinifyCss(), gulpMinifyHtml(HTML_MINIFIER_OPTIONS))) |
| 56 | +/** Copies all component assets to the build output. */ |
| 57 | +task(':build:components:assets', () => { |
| 58 | + return src(assetFiles) |
| 59 | + .pipe(gulpIf(/.html$/, gulpMinifyHtml(HTML_MINIFIER_OPTIONS))) |
57 | 60 | .pipe(dest(DIST_COMPONENTS_ROOT)); |
58 | 61 | }); |
59 | 62 |
|
60 | | -/** Builds scss into css. */ |
61 | | -task(':build:components:scss', sassBuildTask(DIST_COMPONENTS_ROOT, COMPONENTS_DIR)); |
62 | | - |
63 | | -/** Builds the UMD bundle for all of Angular Material. */ |
64 | | -task(':build:components:rollup', () => { |
65 | | - let esBundle = src(path.join(DIST_COMPONENTS_ROOT, 'index.js')) |
66 | | - .pipe(createRollupBundle('es', 'material.js')); |
| 63 | +/** Compiles the components SCSS into minified CSS. */ |
| 64 | +task(':build:components:scss', sassBuildTask(DIST_COMPONENTS_ROOT, COMPONENTS_DIR, true)); |
67 | 65 |
|
68 | | - let umdBundle = src(path.join(DIST_COMPONENTS_ROOT, 'index.js')) |
69 | | - .pipe(createRollupBundle('umd', 'material.umd.js')); |
70 | | - |
71 | | - return merge2(esBundle, umdBundle) |
| 66 | +/** Builds a ES6 bundle for all components. */ |
| 67 | +task(':build:components:rollup:es', () => { |
| 68 | + return src(path.join(DIST_COMPONENTS_ROOT, 'index.js')) |
| 69 | + .pipe(createRollupBundle('es', 'material.js')) |
72 | 70 | .pipe(dest(path.join(DIST_COMPONENTS_ROOT, 'bundles'))); |
73 | 71 | }); |
74 | 72 |
|
75 | | -/** Builds components with resources (html, css) inlined into the built JS (ESM output). */ |
| 73 | +/** Builds a UMD bundle (ES5) for all components. */ |
| 74 | +task(':build:components:rollup:umd', () => { |
| 75 | + return src(path.join(DIST_COMPONENTS_ROOT, 'index.js')) |
| 76 | + .pipe(createRollupBundle('umd', 'material.umd.js')) |
| 77 | + .pipe(dest(path.join(DIST_COMPONENTS_ROOT, 'bundles'))); |
| 78 | +}); |
| 79 | + |
| 80 | + |
| 81 | +/** Builds components with resources (html, css) inlined into the built JS. */ |
76 | 82 | task(':build:components:inline', sequenceTask( |
77 | | - [':build:components:ts', ':build:components:scss', ':build:components:assets'], |
| 83 | + [':build:components:scss', ':build:components:assets'], |
78 | 84 | ':inline-resources', |
79 | 85 | )); |
80 | 86 |
|
81 | | -/** Builds components with minified HTML and CSS inlined into the built JS. */ |
82 | | -task(':build:components:inline:release', sequenceTask( |
83 | | - [':build:components:ts', ':build:components:scss', ':build:components:assets'], |
84 | | - ':build:components:assets:minify', |
85 | | - ':inline-resources' |
86 | | -)); |
87 | | - |
88 | | -/** Inlines resources (html, css) into the JS output (for either ESM or CJS output). */ |
| 87 | +/** Inlines resources (html, css) into the JS output. */ |
89 | 88 | task(':inline-resources', () => inlineResources(DIST_COMPONENTS_ROOT)); |
90 | 89 |
|
91 | | -/** Builds components to ESM output and UMD bundle. */ |
92 | | -task('build:components', sequenceTask(':build:components:inline', ':build:components:rollup')); |
93 | | -task('build:components:release', sequenceTask( |
94 | | - ':build:components:inline:release', ':build:components:rollup' |
95 | | -)); |
96 | | - |
97 | 90 | /** Generates metadata.json files for all of the components. */ |
98 | | -task(':build:components:ngc', ['build:components:release'], execNodeTask( |
99 | | - '@angular/compiler-cli', 'ngc', ['-p', tsconfigES6] |
| 91 | +task(':build:components:ngc', ['build:components'], execNodeTask( |
| 92 | + '@angular/compiler-cli', 'ngc', ['-p', tsconfigPath] |
100 | 93 | )); |
101 | 94 |
|
| 95 | +/** [Watch task] Rebuilds (ESM output) whenever ts, scss, or html sources change. */ |
| 96 | +task(':watch:components', () => { |
| 97 | + watch(path.join(COMPONENTS_DIR, '**/*.ts'), ['build:components', triggerLivereload]); |
| 98 | + watch(path.join(COMPONENTS_DIR, '**/*.scss'), ['build:components', triggerLivereload]); |
| 99 | + watch(path.join(COMPONENTS_DIR, '**/*.html'), ['build:components', triggerLivereload]); |
| 100 | +}); |
| 101 | + |
102 | 102 | const ROLLUP_GLOBALS = { |
103 | 103 | // Angular dependencies |
104 | 104 | '@angular/core': 'ng.core', |
|
0 commit comments