diff --git a/tasks/webpack-dev-server.js b/tasks/webpack-dev-server.js index fb443b4..9cccd28 100644 --- a/tasks/webpack-dev-server.js +++ b/tasks/webpack-dev-server.js @@ -57,10 +57,21 @@ npm install --save-dev webpack-dev-server grunt.registerTask('webpack-dev-server', 'Start a webpack-dev-server.', function webpackDevServerTask(cliTarget) { const done = this.async(); - const targets = cliTarget ? [cliTarget] : Object.keys(grunt.config([this.name])); + const config = grunt.config([this.name]); + const targets = cliTarget + ? [cliTarget] + : config + ? Object.keys(config) + : []; + let runningTargetCount = targets.length; let keepalive = false; + if (runningTargetCount === 0) { + done(new Error('No configuration was found for webpack-dev-server. For further assistance on how to create the config refer to https://github.com/webpack-contrib/grunt-webpack/blob/master/README.md#grunt-webpack')); + return; + } + targets.forEach((target) => { if (target === 'options') { runningTargetCount--; diff --git a/tasks/webpack.js b/tasks/webpack.js index ef87c9a..8d84152 100644 --- a/tasks/webpack.js +++ b/tasks/webpack.js @@ -12,10 +12,20 @@ module.exports = (grunt) => { grunt.registerTask('webpack', 'Run webpack.', function webpackTask(cliTarget) { const done = this.async(); - const targets = cliTarget ? [cliTarget] : Object.keys(grunt.config([this.name])); + const config = grunt.config([this.name]); + const targets = cliTarget + ? [cliTarget] + : config + ? Object.keys(config) + : []; let runningTargetCount = targets.length; let keepalive = false; + if (runningTargetCount === 0) { + done(new Error('No configuration was found for webpack. For further assistance on how to create the config refer to https://github.com/webpack-contrib/grunt-webpack/blob/master/README.md#grunt-webpack')); + return; + } + targets.forEach((target) => { if (target === 'options') { runningTargetCount--; diff --git a/tests/fixtures/webpack/no-config/Gruntfile.js b/tests/fixtures/webpack/no-config/Gruntfile.js new file mode 100644 index 0000000..3289a58 --- /dev/null +++ b/tests/fixtures/webpack/no-config/Gruntfile.js @@ -0,0 +1,8 @@ +const path = require('path'); +const loadGruntWebpackTasks = require('../../../utils/loadGruntWebpackTasks'); + +module.exports = function (grunt) { + grunt.initConfig({}); + + loadGruntWebpackTasks(grunt); +}; diff --git a/tests/fixtures/webpack/no-config/entry.js b/tests/fixtures/webpack/no-config/entry.js new file mode 100644 index 0000000..547d32a --- /dev/null +++ b/tests/fixtures/webpack/no-config/entry.js @@ -0,0 +1 @@ +console.log(okey); diff --git a/tests/fixtures/webpack/no-config/exec.js b/tests/fixtures/webpack/no-config/exec.js new file mode 100644 index 0000000..67e54b8 --- /dev/null +++ b/tests/fixtures/webpack/no-config/exec.js @@ -0,0 +1,3 @@ +assertGrunt.failed(); + +t.regex(stdout, /No configuration was found for webpack./);