gruntjs / grunt-contrib-watch

Run tasks whenever watched files change.
http://gruntjs.com/
MIT License
1.98k stars 356 forks source link

Using ES6 with grunt-contrib-watch; arrow functions break grunt #460

Open andrew-luhring opened 9 years ago

andrew-luhring commented 9 years ago

Not completely sure whether this is a grunt-contrib-watch issue or a grunt issue so i'm submitting the issue in both places, but here's my setup and what's happening:

Setup

Gruntfile.js

module.exports = function(grunt) {
    var request = require('./request.js')
 ,  config = {
            pkg: grunt.file.readJSON('package.json')
            , watch: {
                  files: ['./request.js']
                , tasks: ['req']
                , options: {
                    spawn: false
                }
            }
        };

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.initConfig(config);

    grunt.registerTask('req', function() {
        console.log('\n\n\n\n\n');
        request();
        console.log('\n\n\n\n\n');
    });

    grunt.registerTask('default', ['watch']);
};

Request.js

'use strict';
var http = require('http');
module.exports = ()=> {
    console.log('requesting...');
};

To reproduce issue:

  1. On the command line run grunt --verbose --debug
  2. Then add a space or something to request.js and hit save.

    Result:

Grunt will the grunt watch task, the grunt-watch task will run request.js, it will break.

Initializing
Command-line options: --verbose, --debug=1

Reading "Gruntfile.js" Gruntfile...OK

Registering Gruntfile tasks.
Loading "Gruntfile.js" tasks...ERROR
>> /Users/the_future/Google Drive/Sites/lol/request.js:4
>> module.exports = ()=> {
>>                   ^
>> SyntaxError: Unexpected token )
>>     at exports.runInThisContext (vm.js:73:16)
>>     at Module._compile (module.js:443:25)
>>     at Object.Module._extensions..js (module.js:478:10)
>>     at Module.load (module.js:355:32)
>>     at Function.Module._load (module.js:310:12)
>>     at Module.require (module.js:365:17)
>>     at require (module.js:384:17)
>>     at Object.module.exports (/Users/the_future/Google Drive/Sites/lol/Gruntfile.js:4:19)
>>     at loadTask (/Users/the_future/Google Drive/Sites/lol/node_modules/grunt/lib/grunt/task.js:325:10)
>>     at Task.task.init (/Users/the_future/Google Drive/Sites/lol/node_modules/grunt/lib/grunt/task.js:437:5)

No tasks specified, running default tasks.
Running tasks: default
Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.

^ Grunt fails.

BUT

Now, change request.js to the following.

'use strict';
var http = require('http');
module.exports = function () {
    console.log('requesting...');
};
  1. Run grunt --verbose --debug
  2. Add a space or something to request.js and save.

It's fine. No es6, no error.

BUT

NOW- without stopping grunt- change request.js back to:

'use strict';
const http = require('http');

module.exports = ()=> {
    console.log('requesting...');
};

Result:

IT WORKS FINE.

The first time you run grunt, it doesn't respect es6 (arrow functions at least). But if you run grunt without any es6 at first, THEN ADD ES6 CODE grunt works as expected.

Hence, i'm not sure if this is an issue with grunt or grunt-contrib-watch.

cspotcode commented 8 years ago

This is a node issue, not a grunt or grunt-contrib-watch issue. Your version of node doesn't support arrow functions.

request.js is only parsed once when you start grunt. That's why changing it while grunt is running doesn't cause an error. To prove this, change the message being logged within request.js while grunt is running. You'll see the new message does not get logged.

andrew-luhring commented 8 years ago

I was using the latest version of node at the time... so I don't think that's the case.

shiraze commented 6 years ago

Was this ever resolved, as I'm hitting the same issue? My version of node.js is v6.9.5, and arrows support has been available since v6.4.0 (according to https://node.green/)