Skip to content

Commit

Permalink
fix multi target
Browse files Browse the repository at this point in the history
  • Loading branch information
ljqx committed May 26, 2017
1 parent 6f7ed27 commit 2091ff5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 50 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"tasks"
],
"peerDependencies": {
"grunt": "^1.0.0",
"webpack": "^2.1.0-beta || ^2.2.0-rc || 2"
},
"license": "MIT",
Expand Down
97 changes: 47 additions & 50 deletions tasks/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,66 @@ module.exports = (grunt) => {
const cachePluginFactory = new CachePluginFactory();
const processPluginFactory = new ProgressPluginFactory(grunt);

grunt.registerTask('webpack', 'Run webpack.', function webpackTask(cliTarget) {
grunt.registerMultiTask('webpack', 'Run webpack.', function webpackTask() {
const done = this.async();

const targets = cliTarget ? [cliTarget] : Object.keys(grunt.config([this.name]));
targets.forEach((target) => {
if (target === 'options') return;
const target = this.target;

const optionHelper = new OptionHelper(grunt, this.name, target);
const optionHelper = new OptionHelper(grunt, this.name, target);

const watch = optionHelper.get('watch');
const opts = {
cache: watch ? false : optionHelper.get('cache'),
failOnError: optionHelper.get('failOnError'),
keepalive: optionHelper.get('keepalive'),
progress: optionHelper.get('progress'),
stats: optionHelper.get('stats'),
storeStatsTo: optionHelper.get('storeStatsTo'),
watch: watch,
};
const watch = optionHelper.get('watch');
const opts = {
cache: watch ? false : optionHelper.get('cache'),
failOnError: optionHelper.get('failOnError'),
keepalive: optionHelper.get('keepalive'),
progress: optionHelper.get('progress'),
stats: optionHelper.get('stats'),
storeStatsTo: optionHelper.get('storeStatsTo'),
watch: watch,
};

const webpackOptions = optionHelper.getWebpackOptions();
const webpackOptions = optionHelper.getWebpackOptions();

const compiler = webpack(webpackOptions);
const compiler = webpack(webpackOptions);

if (opts.cache) cachePluginFactory.addPlugin(target, compiler);
if (opts.progress) processPluginFactory.addPlugin(compiler, webpackOptions);
if (opts.cache) cachePluginFactory.addPlugin(target, compiler);
if (opts.progress) processPluginFactory.addPlugin(compiler, webpackOptions);

const handler = (err, stats) => {
if (opts.cache) cachePluginFactory.updateDependencies(target, compiler);
if (err) return done(err);
const handler = (err, stats) => {
if (opts.cache) cachePluginFactory.updateDependencies(target, compiler);
if (err) return done(err);

if (opts.stats && !stats.hasErrors()) {
grunt.log.writeln(
stats
.toString(opts.stats)
// add plugin version with and without colors
.replace(/(\n(.*)Version: webpack (.*)\d+\.\d+\.\d+(.*))\n/, `$1$2 / grunt-webpack $3${pkg.version}$4\n`)
);
}
if (opts.stats && !stats.hasErrors()) {
grunt.log.writeln(
stats
.toString(opts.stats)
// add plugin version with and without colors
.replace(/(\n(.*)Version: webpack (.*)\d+\.\d+\.\d+(.*))\n/, `$1$2 / grunt-webpack $3${pkg.version}$4\n`)
);
}

if (typeof opts.storeStatsTo === 'string') {
grunt.config.set(opts.storeStatsTo, stats.toJson(opts.stats));
}
if (typeof opts.storeStatsTo === 'string') {
grunt.config.set(opts.storeStatsTo, stats.toJson(opts.stats));
}

if (stats.hasErrors()) {
// in case opts.stats === false we still want to display errors.
grunt.log.writeln(stats.toString(opts.stats || 'errors-only'));
if (opts.failOnError) {
// construct error without stacktrace, as the stack is not relevant here
const error = new Error();
error.stack = null;
return done(error);
}
if (stats.hasErrors()) {
// in case opts.stats === false we still want to display errors.
grunt.log.writeln(stats.toString(opts.stats || 'errors-only'));
if (opts.failOnError) {
// construct error without stacktrace, as the stack is not relevant here
const error = new Error();
error.stack = null;
return done(error);
}
}

if (!opts.keepalive) done();
};
if (!opts.keepalive) done();
};

if (opts.watch) {
compiler.watch(webpackOptions.watchOptions || {}, handler);
} else {
compiler.run(handler);
}
});
if (opts.watch) {
compiler.watch(webpackOptions.watchOptions || {}, handler);
} else {
compiler.run(handler);
}
});
};

0 comments on commit 2091ff5

Please sign in to comment.