Skip to content

Commit 82ea2ad

Browse files
committed
Simplifying webpack task config.
1 parent bbe6aaf commit 82ea2ad

File tree

2 files changed

+65
-76
lines changed

2 files changed

+65
-76
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-core-build-webpack",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",

src/WebpackTask.ts

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,107 @@
11
import { GulpTask } from 'gulp-core-build';
22

33
export interface IWebpackConfig {
4-
configPaths: string[];
4+
configPath: string;
55
}
66

77
export class WebpackTask extends GulpTask<IWebpackConfig> {
88
public name = 'webpack';
99

1010
public taskConfig: IWebpackConfig = {
11-
configPaths: ['./webpack.config.js']
11+
configPath: './webpack.config.js'
1212
};
1313

1414
public executeTask(gulp, completeCallback): any {
1515
// let isProduction = (process.argv.indexOf('--production') > -1);
1616
// let streams = [];
17-
let completeEntries = 0;
1817
let shouldInitWebpack = (process.argv.indexOf('--initwebpack') > -1);
1918
let path = require('path');
2019

2120
if (shouldInitWebpack) {
2221
this.log('Initializing a webpack.config.js, which bundles lib/index.js into dist/packagename.js into a UMD module.');
2322
this.copyFile(path.resolve(__dirname, '../webpack.config.js'));
24-
} else if (completeEntries === this.taskConfig.configPaths.length) {
23+
completeCallback();
24+
} else if (!this.taskConfig.configPath) {
2525
completeCallback();
2626
} else {
27+
let configPath = this.taskConfig.configPath;
2728

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);
3531

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 }).`);
3935

40-
let webpack = require('webpack');
41-
let gutil = require('gulp-util');
36+
completeCallback();
37+
} else {
38+
configPath = this.resolvePath(configPath);
4239

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');
4642

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;
5446

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+
});
5854

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+
}
6258

63-
completeEntries++;
59+
if (statsResult.warnings && statsResult.warnings.length) {
60+
this.logWarning(`'${outputDir}':` + '\n' + statsResult.warnings.join('\n') + '\n');
61+
}
6462

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 ];
6765

68-
statsResultChildren.forEach(child => {
69-
child.chunks.forEach(chunk => {
66+
statsResultChildren.forEach(child => {
67+
child.chunks.forEach(chunk => {
7068

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
7674

7775
/*
7876
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+
);
9997
*/
10098

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
111100

112-
if (completeEntries === this.taskConfig.configPaths.length) {
113-
completeCallback();
114-
}
101+
}); // end child
115102

103+
completeCallback();
104+
}); // endwebpack callback
116105
}
117106
}
118107
}

0 commit comments

Comments
 (0)