Skip to content

Commit 28f1f80

Browse files
devversionjelbourn
authored andcommitted
chore: minify html and css for releases (#2262)
* chore: minify html and css for releases * For releases, the HTML and CSS assets will be minified before being inlined into the built JS * Remove comments in production html
1 parent 2be9ec4 commit 28f1f80

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@
6363
"gulp": "^3.9.1",
6464
"gulp-autoprefixer": "^3.1.1",
6565
"gulp-clean": "^0.3.2",
66+
"gulp-clean-css": "^2.3.0",
6667
"gulp-cli": "^1.2.2",
68+
"gulp-htmlmin": "^3.0.0",
69+
"gulp-if": "^2.0.2",
6770
"gulp-markdown": "^1.2.0",
6871
"gulp-sass": "^2.3.2",
6972
"gulp-server-livereload": "^1.8.2",

tools/gulp/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export const SASS_AUTOPREFIXER_OPTIONS = {
1515
cascade: false,
1616
};
1717

18+
export const HTML_MINIFIER_OPTIONS = {
19+
collapseWhitespace: true,
20+
removeComments: true
21+
};
22+
1823
export const NPM_VENDOR_FILES = [
1924
'@angular', 'core-js/client', 'hammerjs', 'rxjs', 'systemjs/dist', 'zone.js/dist'
2025
];

tools/gulp/tasks/components.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import {task, watch} from 'gulp';
1+
import {task, watch, src, dest} from 'gulp';
22
import * as path from 'path';
33

4-
import {DIST_COMPONENTS_ROOT, PROJECT_ROOT, COMPONENTS_DIR} from '../constants';
4+
import {
5+
DIST_COMPONENTS_ROOT, PROJECT_ROOT, COMPONENTS_DIR, HTML_MINIFIER_OPTIONS
6+
} from '../constants';
57
import {sassBuildTask, tsBuildTask, execNodeTask, copyTask, sequenceTask} from '../task_helpers';
68
import {writeFileSync} from 'fs';
79

810
// No typings for these.
911
const inlineResources = require('../../../scripts/release/inline-resources');
1012
const rollup = require('rollup').rollup;
13+
const gulpMinifyCss = require('gulp-clean-css');
14+
const gulpMinifyHtml = require('gulp-htmlmin');
15+
const gulpIf = require('gulp-if');
1116

1217

1318
// NOTE: there are two build "modes" in this file, based on which tsconfig is used.
@@ -23,9 +28,9 @@ const tsconfigPath = path.relative(PROJECT_ROOT, path.join(COMPONENTS_DIR, 'tsco
2328

2429
/** [Watch task] Rebuilds (ESM output) whenever ts, scss, or html sources change. */
2530
task(':watch:components', () => {
26-
watch(path.join(COMPONENTS_DIR, '**/*.ts'), [':build:components:rollup']);
27-
watch(path.join(COMPONENTS_DIR, '**/*.scss'), [':build:components:rollup']);
28-
watch(path.join(COMPONENTS_DIR, '**/*.html'), [':build:components:rollup']);
31+
watch(path.join(COMPONENTS_DIR, '**/*.ts'), ['build:components']);
32+
watch(path.join(COMPONENTS_DIR, '**/*.scss'), ['build:components']);
33+
watch(path.join(COMPONENTS_DIR, '**/*.html'), ['build:components']);
2934
});
3035

3136

@@ -41,11 +46,18 @@ task(':build:components:assets', copyTask([
4146
path.join(PROJECT_ROOT, 'README.md'),
4247
], DIST_COMPONENTS_ROOT));
4348

49+
/** Minifies the HTML and CSS assets in the distribution folder. */
50+
task(':build:components:assets:minify', () => {
51+
return src('**/*.+(html|css)', { cwd: DIST_COMPONENTS_ROOT})
52+
.pipe(gulpIf(/.css$/, gulpMinifyCss(), gulpMinifyHtml(HTML_MINIFIER_OPTIONS)))
53+
.pipe(dest(DIST_COMPONENTS_ROOT));
54+
});
55+
4456
/** Builds scss into css. */
4557
task(':build:components:scss', sassBuildTask(DIST_COMPONENTS_ROOT, COMPONENTS_DIR));
4658

4759
/** Builds the UMD bundle for all of Angular Material. */
48-
task(':build:components:rollup', [':build:components:inline'], () => {
60+
task(':build:components:rollup', () => {
4961
const globals: {[name: string]: string} = {
5062
// Angular dependencies
5163
'@angular/core': 'ng.core',
@@ -102,13 +114,23 @@ task(':build:components:inline', sequenceTask(
102114
':inline-resources',
103115
));
104116

117+
/** Builds components with minified HTML and CSS inlined into the built JS. */
118+
task(':build:components:inline:release', sequenceTask(
119+
[':build:components:ts', ':build:components:scss', ':build:components:assets'],
120+
':build:components:assets:minify',
121+
':inline-resources'
122+
));
123+
105124
/** Inlines resources (html, css) into the JS output (for either ESM or CJS output). */
106125
task(':inline-resources', () => inlineResources(DIST_COMPONENTS_ROOT));
107126

108127
/** Builds components to ESM output and UMD bundle. */
109-
task('build:components', [':build:components:rollup']);
128+
task('build:components', sequenceTask(':build:components:inline', ':build:components:rollup'));
129+
task('build:components:release', sequenceTask(
130+
':build:components:inline:release', ':build:components:rollup'
131+
));
110132

111133
/** Generates metadata.json files for all of the components. */
112-
task(':build:components:ngc', ['build:components'], execNodeTask(
134+
task(':build:components:ngc', ['build:components:release'], execNodeTask(
113135
'@angular/compiler-cli', 'ngc', ['-p', tsconfigPath]
114136
));

tools/gulp/tasks/development.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ task(':watch:devapp', () => {
2424

2525

2626
task(':build:devapp:vendor', vendorTask());
27-
task(':build:devapp:ts', [':build:components:rollup'], tsBuildTask(appDir));
27+
task(':build:devapp:ts', ['build:components'], tsBuildTask(appDir));
2828
task(':build:devapp:scss', [':build:components:scss'], sassBuildTask(outDir, appDir));
2929
task(':build:devapp:assets', copyTask(appDir, outDir));
3030
task('build:devapp', buildAppTask('devapp'));

0 commit comments

Comments
 (0)