diff --git a/package.json b/package.json index e7aa8db..8890f41 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "tasks" ], "peerDependencies": { + "grunt": "^1.0.0", "webpack": "^2.1.0-beta || ^2.2.0-rc || 2" }, "license": "MIT", diff --git a/tasks/webpack.js b/tasks/webpack.js index b128968..495fadb 100644 --- a/tasks/webpack.js +++ b/tasks/webpack.js @@ -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); + } }); };