|
| 1 | +// Copyright 2015 Google Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @fileoverview Gulp tasks for building the project. |
| 17 | + */ |
| 18 | +import del from 'del'; |
| 19 | +import gulp from 'gulp'; |
| 20 | +import gulpEslint from 'gulp-eslint'; |
| 21 | +import gulpFilter from 'gulp-filter'; |
| 22 | +import gulpInject from 'gulp-inject'; |
| 23 | +import gulpMinifyCss from 'gulp-minify-css'; |
| 24 | +import gulpMinifyHtml from 'gulp-minify-html'; |
| 25 | +import gulpUglify from 'gulp-uglify'; |
| 26 | +import gulpUseref from 'gulp-useref'; |
| 27 | +import gulpRev from 'gulp-rev'; |
| 28 | +import gulpRevReplace from 'gulp-rev-replace'; |
| 29 | +import gulpSize from 'gulp-size'; |
| 30 | +import gulpSourcemaps from 'gulp-sourcemaps'; |
| 31 | +import uglifySaveLicense from 'uglify-save-license'; |
| 32 | +import path from 'path'; |
| 33 | + |
| 34 | +import conf from './conf'; |
| 35 | + |
| 36 | + |
| 37 | +/** |
| 38 | + * Builds production package and places it in the dist directory. |
| 39 | + * |
| 40 | + * Following steps are done here: |
| 41 | + * 1. Vendor CSS and JS files are concatenated and minified. |
| 42 | + * 2. index.html is minified. |
| 43 | + * 3. CSS and JS assets are suffixed with version hash. |
| 44 | + * 4. Everything is saved in the dist directory. |
| 45 | + */ |
| 46 | +gulp.task('build', ['index:prod', 'assets'], function () { |
| 47 | + let htmlFilter = gulpFilter('*.html', {restore: true}); |
| 48 | + let vendorCssFilter = gulpFilter('**/vendor.css', {restore: true}); |
| 49 | + let vendorJsFilter = gulpFilter('**/vendor.js', {restore: true}); |
| 50 | + let assets; |
| 51 | + |
| 52 | + return gulp.src(path.join(conf.paths.prodTmp, '*.html')) |
| 53 | + .pipe(assets = gulpUseref.assets({ |
| 54 | + searchPath: [ |
| 55 | + // To resolve local paths. |
| 56 | + conf.paths.prodTmp, |
| 57 | + // To resolve bower_components/... paths. |
| 58 | + conf.paths.base |
| 59 | + ] |
| 60 | + })) |
| 61 | + .pipe(vendorCssFilter) |
| 62 | + .pipe(gulpMinifyCss()) |
| 63 | + .pipe(vendorCssFilter.restore) |
| 64 | + .pipe(vendorJsFilter) |
| 65 | + .pipe(gulpUglify({preserveComments: uglifySaveLicense})) |
| 66 | + .pipe(vendorJsFilter.restore) |
| 67 | + .pipe(gulpRev()) |
| 68 | + .pipe(assets.restore()) |
| 69 | + .pipe(gulpUseref({searchPath: [conf.paths.prodTmp]})) |
| 70 | + .pipe(gulpRevReplace()) |
| 71 | + .pipe(htmlFilter) |
| 72 | + .pipe(gulpMinifyHtml({ |
| 73 | + empty: true, |
| 74 | + spare: true, |
| 75 | + quotes: true |
| 76 | + })) |
| 77 | + .pipe(htmlFilter.restore) |
| 78 | + .pipe(gulp.dest(conf.paths.dist)) |
| 79 | + .pipe(gulpSize({showFiles: true})); |
| 80 | +}); |
| 81 | + |
| 82 | + |
| 83 | +/** |
| 84 | + * Copies assets to the dist directory. |
| 85 | + */ |
| 86 | +gulp.task('assets', function () { |
| 87 | + return gulp.src(path.join(conf.paths.assets, '/**/*'), {base: conf.paths.app}) |
| 88 | + .pipe(gulp.dest(conf.paths.dist)); |
| 89 | +}); |
| 90 | + |
| 91 | + |
| 92 | +/** |
| 93 | + * Cleans all build artifacts. |
| 94 | + */ |
| 95 | +gulp.task('clean', function () { |
| 96 | + return del([path.join(conf.paths.dist, '/'), path.join(conf.paths.tmp, '/')]); |
| 97 | +}); |
0 commit comments