|
| 1 | +'use strict'; |
| 2 | +/** |
| 3 | + * This file needs to be JavaScript and is read by gulp. |
| 4 | + */ |
| 5 | +// Global imports. |
| 6 | +const fs = require('fs'); |
| 7 | +const gulp = require('gulp'); |
| 8 | +const path = require('path'); |
| 9 | + |
| 10 | +// Other imports. |
| 11 | +const inlineResources = require('./scripts/release/inline-resources'); |
| 12 | + |
| 13 | +// Gulp plugins. |
| 14 | +const gulpClean = require('gulp-clean'); |
| 15 | +const gulpTs = require('gulp-typescript'); |
| 16 | +const gulpMerge = require('merge2'); |
| 17 | +const gulpSass = require('gulp-sass'); |
| 18 | +const gulpSourcemaps = require('gulp-sourcemaps'); |
| 19 | +const gulpLiveServer = require('gulp-live-server'); |
| 20 | + |
| 21 | + |
| 22 | +// Directories. |
| 23 | +const srcDir = path.join(__dirname, 'src'); |
| 24 | +const componentsDir = path.join(srcDir, 'lib'); |
| 25 | +const devAppDir = path.join(srcDir, 'demo-app'); |
| 26 | + |
| 27 | +const outDir = 'dist'; |
| 28 | +const outLibDir = path.join(outDir, '@angular2-material'); |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * Create a TS Build Task, based on the options. |
| 33 | + */ |
| 34 | +function makeTsBuildTask(options) { |
| 35 | + const tsConfigDir = options.tsConfigPath; |
| 36 | + const tsConfigPath = path.join(tsConfigDir, 'tsconfig.json'); |
| 37 | + const tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf-8')); |
| 38 | + const dest = path.join(tsConfigDir, tsConfig.compilerOptions.outDir); |
| 39 | + |
| 40 | + return function() { |
| 41 | + const tsProject = gulpTs.createProject(tsConfigPath, { |
| 42 | + typescript: require('typescript') |
| 43 | + }); |
| 44 | + |
| 45 | + let pipe = tsProject.src() |
| 46 | + .pipe(gulpSourcemaps.init()) |
| 47 | + .pipe(gulpTs(tsProject)); |
| 48 | + let dts = pipe.dts.pipe(gulp.dest(dest)); |
| 49 | + |
| 50 | + if (tsConfig.compilerOptions.sourceMap) { |
| 51 | + if (!tsConfig.compilerOptions.inlineSources) { |
| 52 | + pipe = pipe.pipe(gulpSourcemaps.write(dest)); |
| 53 | + } else { |
| 54 | + pipe = pipe.pipe(gulpSourcemaps.write()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return gulpMerge([dts, pipe.pipe(gulp.dest(dest))]); |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Create a SASS Build Task. |
| 64 | + */ |
| 65 | +function makeSassBuildTask(options) { |
| 66 | + const dest = options.dest; |
| 67 | + const glob = path.join(options.root, '**/*.scss'); |
| 68 | + const sassOptions = { |
| 69 | + includePaths: options.includePaths |
| 70 | + }; |
| 71 | + |
| 72 | + return function() { |
| 73 | + return gulp.src(glob) |
| 74 | + .pipe(gulpSourcemaps.init()) |
| 75 | + .pipe(gulpSass(sassOptions).on('error', gulpSass.logError)) |
| 76 | + .pipe(gulpSourcemaps.write(dest)) |
| 77 | + .pipe(gulp.dest(dest)); |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +/*************************************************************************************************** |
| 83 | + * Components Build Tasks. |
| 84 | + */ |
| 85 | +gulp.task('build:components:ts', makeTsBuildTask({ tsConfigPath: componentsDir })); |
| 86 | +gulp.task('build:components:assets', function() { |
| 87 | + return gulp.src(path.join(componentsDir, '*/**/*.!(ts|spec.ts)')) |
| 88 | + .pipe(gulp.dest(outLibDir)); |
| 89 | +}); |
| 90 | +gulp.task('build:components:scss', function() { |
| 91 | + const cssTask = makeSassBuildTask({ |
| 92 | + dest: outLibDir, |
| 93 | + root: componentsDir, |
| 94 | + includePaths: path.join(componentsDir, 'core/style') |
| 95 | + }); |
| 96 | + // Also copy over the SCSS for the components. |
| 97 | + return gulpMerge([ |
| 98 | + cssTask(), |
| 99 | + gulp.src(path.join(componentsDir, '**/*.scss')) |
| 100 | + .pipe(gulp.dest(outLibDir)) |
| 101 | + ]); |
| 102 | +}); |
| 103 | +gulp.task('build:components', [ |
| 104 | + 'build:components:ts', |
| 105 | + 'build:components:assets', |
| 106 | + 'build:components:scss' |
| 107 | +], function() { |
| 108 | + inlineResources([outLibDir]); |
| 109 | +}); |
| 110 | + |
| 111 | +/*************************************************************************************************** |
| 112 | + * DevApp Build Tasks. |
| 113 | + */ |
| 114 | +gulp.task('build:devapp:ts', ['build:components:ts'], makeTsBuildTask({ tsConfigPath: devAppDir })); |
| 115 | +gulp.task('build:devapp:scss', ['build:components:scss'], makeSassBuildTask({ |
| 116 | + dest: outDir, |
| 117 | + root: devAppDir, |
| 118 | + // Change this once we have a better strategy for releasing SCSS files. |
| 119 | + includePaths: [ |
| 120 | + path.join(componentsDir, 'core/style'), |
| 121 | + componentsDir |
| 122 | + ] |
| 123 | +})); |
| 124 | +gulp.task('build:devapp:assets', function() { |
| 125 | + return gulp.src(path.join(devAppDir, '**/*')) |
| 126 | + .pipe(gulp.dest(outDir)); |
| 127 | +}); |
| 128 | +gulp.task('build:devapp:vendor', function() { |
| 129 | + const npmVendorFiles = [ |
| 130 | + 'core-js/client', 'zone.js/dist', 'hammerjs', 'systemjs/dist', 'rxjs', '@angular', 'hammerjs' |
| 131 | + ]; |
| 132 | + |
| 133 | + return gulpMerge( |
| 134 | + npmVendorFiles.map(function(root) { |
| 135 | + const glob = path.join(root, '**/*.+(js|js.map)'); |
| 136 | + return gulp.src(path.join('node_modules', glob)) |
| 137 | + .pipe(gulp.dest(path.join('dist/vendor', root))); |
| 138 | + })); |
| 139 | +}); |
| 140 | + |
| 141 | +gulp.task('build:devapp', [ |
| 142 | + 'build:components', |
| 143 | + 'build:devapp:vendor', |
| 144 | + 'build:devapp:ts', |
| 145 | + 'build:devapp:scss', |
| 146 | + 'build:devapp:assets' |
| 147 | +]); |
| 148 | + |
| 149 | +/*************************************************************************************************** |
| 150 | + * DevApp Build Tasks. |
| 151 | + */ |
| 152 | +gulp.task('build:e2eapp:ts', ['build:components:ts'], makeTsBuildTask({ tsConfigPath: devAppDir })); |
| 153 | +gulp.task('build:e2eapp:scss', ['build:components:scss'], makeSassBuildTask({ |
| 154 | + dest: outDir, |
| 155 | + root: devAppDir, |
| 156 | + // Change this once we have a better strategy for releasing SCSS files. |
| 157 | + includePaths: [ |
| 158 | + path.join(componentsDir, 'core/style'), |
| 159 | + componentsDir |
| 160 | + ] |
| 161 | +})); |
| 162 | +gulp.task('build:e2eapp:assets', function() { |
| 163 | + return gulp.src(path.join(devAppDir, '**/*')) |
| 164 | + .pipe(gulp.dest(outDir)); |
| 165 | +}); |
| 166 | +gulp.task('build:e2eapp:vendor', function() { |
| 167 | + const npmVendorFiles = [ |
| 168 | + 'core-js/client', 'zone.js/dist', 'hammerjs', 'systemjs/dist', 'rxjs', '@angular', 'hammerjs' |
| 169 | + ]; |
| 170 | + |
| 171 | + return gulpMerge( |
| 172 | + npmVendorFiles.map(function(root) { |
| 173 | + const glob = path.join(root, '**/*.+(js|js.map)'); |
| 174 | + return gulp.src(path.join('node_modules', glob)) |
| 175 | + .pipe(gulp.dest(path.join('dist/vendor', root))); |
| 176 | + })); |
| 177 | +}); |
| 178 | + |
| 179 | +gulp.task('build:e2eapp', [ |
| 180 | + 'build:components', |
| 181 | + 'build:e2eapp:vendor', |
| 182 | + 'build:e2eapp:ts', |
| 183 | + 'build:e2eapp:scss', |
| 184 | + 'build:e2eapp:assets' |
| 185 | +]); |
| 186 | + |
| 187 | + |
| 188 | +/*************************************************************************************************** |
| 189 | + * Global tasks. |
| 190 | + */ |
| 191 | +gulp.task('build', ['build:devapp']); |
| 192 | + |
| 193 | +gulp.task('clean', function () { |
| 194 | + return gulp.src('dist', { read: false }) |
| 195 | + .pipe(gulpClean()); |
| 196 | +}); |
| 197 | + |
| 198 | + |
| 199 | +/*************************************************************************************************** |
| 200 | + * Watch Tasks. |
| 201 | + */ |
| 202 | +gulp.task('watch:components', ['build:components'], function() { |
| 203 | + gulp.watch(path.join(componentsDir, '**/*'), ['build:components']); |
| 204 | +}); |
| 205 | + |
| 206 | +gulp.task('watch:devapp', ['watch:components', 'build:devapp'], function() { |
| 207 | + gulp.watch(path.join(devAppDir, '**/*'), ['build:devapp']); |
| 208 | +}); |
| 209 | + |
| 210 | + |
| 211 | +/*************************************************************************************************** |
| 212 | + * Serve Tasks. |
| 213 | + */ |
| 214 | +gulp.task('serve:devapp', ['build:devapp'], function() { |
| 215 | + const server = gulpLiveServer.static('dist', 4200); |
| 216 | + |
| 217 | + server.start(); |
| 218 | + function reload(file) { |
| 219 | + server.notify.apply(server, [file]); |
| 220 | + } |
| 221 | + |
| 222 | + gulp.watch(path.join(componentsDir, '**/*.ts'), ['build:components:ts'], reload); |
| 223 | + gulp.watch(path.join(componentsDir, '**/*.scss'), ['build:components:scss'], reload); |
| 224 | + gulp.watch(path.join(componentsDir, '**/*.html'), ['build:components:html'], reload); |
| 225 | + gulp.watch(path.join(devAppDir, '**/*.ts'), ['build:devapp:ts'], reload); |
| 226 | + gulp.watch(path.join(devAppDir, '**/*.scss'), ['build:devapp:scss'], reload); |
| 227 | + gulp.watch(path.join(devAppDir, '**/*.html'), ['build:devapp:html'], reload); |
| 228 | +}); |
| 229 | + |
| 230 | +gulp.task('serve:e2eapp', ['build:e2eapp'], function() { |
| 231 | + const server = gulpLiveServer.static('dist', 4200); |
| 232 | + |
| 233 | + server.start(); |
| 234 | + function reload(file) { |
| 235 | + server.notify.apply(server, [file]); |
| 236 | + } |
| 237 | + |
| 238 | + gulp.watch(path.join(componentsDir, '**/*.ts'), ['build:components:ts'], reload); |
| 239 | + gulp.watch(path.join(componentsDir, '**/*.scss'), ['build:components:scss'], reload); |
| 240 | + gulp.watch(path.join(componentsDir, '**/*.html'), ['build:components:html'], reload); |
| 241 | + gulp.watch(path.join(devAppDir, '**/*.ts'), ['build:e2eapp:ts'], reload); |
| 242 | + gulp.watch(path.join(devAppDir, '**/*.scss'), ['build:e2eapp:scss'], reload); |
| 243 | + gulp.watch(path.join(devAppDir, '**/*.html'), ['build:e2eapp:html'], reload); |
| 244 | +}); |
| 245 | + |
0 commit comments