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