|
| 1 | +import { GulpTask } from 'gulp-core-build'; |
| 2 | + |
| 3 | +export interface IWebpackConfig { |
| 4 | + configPaths: string[]; |
| 5 | +} |
| 6 | + |
| 7 | +export class WebpackTask extends GulpTask<IWebpackConfig> { |
| 8 | + public name = 'webpack'; |
| 9 | + |
| 10 | + public taskConfig: IWebpackConfig = { |
| 11 | + configPaths: ['./webpack.config.js'] |
| 12 | + }; |
| 13 | + |
| 14 | + public executeTask(gulp, completeCallback): any { |
| 15 | + // let isProduction = (process.argv.indexOf('--production') > -1); |
| 16 | + // let streams = []; |
| 17 | + let completeEntries = 0; |
| 18 | + |
| 19 | + if (completeEntries === this.taskConfig.configPaths.length) { |
| 20 | + completeCallback(); |
| 21 | + } else { |
| 22 | + |
| 23 | + for (let configPath of this.taskConfig.configPaths) { |
| 24 | + configPath = this.resolvePath(configPath); |
| 25 | + |
| 26 | + if (!this.fileExists(configPath)) { |
| 27 | + let path = require('path'); |
| 28 | + let relativeConfigPath = path.relative(this.buildConfig.rootPath, configPath); |
| 29 | + |
| 30 | + this.logWarning(`The webpack config location '${relativeConfigPath}' doesn't exist.`); |
| 31 | + completeEntries++; |
| 32 | + } else { |
| 33 | + let webpack = require('webpack'); |
| 34 | + let path = require('path'); |
| 35 | + let gutil = require('gulp-util'); |
| 36 | + |
| 37 | + let webpackConfig = require(configPath); |
| 38 | + let startTime = new Date().getTime(); |
| 39 | + let outputDir = this.buildConfig.distFolder; |
| 40 | + |
| 41 | + webpack( |
| 42 | + webpackConfig, |
| 43 | + (error, stats) => { |
| 44 | + let statsResult = stats.toJson({ |
| 45 | + hash: false, |
| 46 | + source: false |
| 47 | + }); |
| 48 | + |
| 49 | + if (statsResult.errors && statsResult.errors.length) { |
| 50 | + this.logError(`'${outputDir}':` + '\n' + statsResult.errors.join('\n') + '\n'); |
| 51 | + } |
| 52 | + |
| 53 | + if (statsResult.warnings && statsResult.warnings.length) { |
| 54 | + this.logWarning(`'${outputDir}':` + '\n' + statsResult.warnings.join('\n') + '\n'); |
| 55 | + } |
| 56 | + |
| 57 | + completeEntries++; |
| 58 | + |
| 59 | + let duration = (new Date().getTime() - startTime); |
| 60 | + |
| 61 | + statsResult.chunks.forEach(chunk => chunk.files.forEach(file => ( |
| 62 | + this.log(`Bundled: '${gutil.colors.cyan(path.basename(file))}', ` + |
| 63 | + `size: ${gutil.colors.magenta(chunk.size)} bytes, ` + |
| 64 | + `took ${gutil.colors.magenta(duration)} ms.`) |
| 65 | + ))); |
| 66 | + |
| 67 | + let chunk; |
| 68 | + |
| 69 | + for (let i = 0; i < statsResult.chunks.length; i++) { |
| 70 | + let chunkStats = { |
| 71 | + chunk: null, |
| 72 | + modules: null |
| 73 | + }; |
| 74 | + |
| 75 | + chunkStats.chunk = chunk = statsResult.chunks[i]; |
| 76 | + |
| 77 | + let statsPath = path.join(outputDir, chunk.files[0]) + '.stats.json'; |
| 78 | + |
| 79 | + if (statsResult.modules) { |
| 80 | + chunkStats.modules = statsResult.modules |
| 81 | + .filter(mod => (mod.chunks && mod.chunks.indexOf(chunk.id) > -1)) |
| 82 | + .map(mod => ({ name: mod.name, size: mod.size })) |
| 83 | + .sort((a, b) => (a.size < b.size ? 1 : -1)); |
| 84 | + } |
| 85 | + |
| 86 | + let fs = require('fs'); |
| 87 | + |
| 88 | + fs.writeFileSync( |
| 89 | + statsPath, |
| 90 | + JSON.stringify(chunkStats, null, 2), |
| 91 | + 'utf8' |
| 92 | + ); |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + if (completeEntries === this.taskConfig.configPaths.length) { |
| 98 | + completeCallback(); |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments