Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andrew-luhring opened this issue Sep 12, 2015 · 3 comments
Open

Comments

@andrew-luhring
Copy link

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
Copy link

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
Copy link
Author

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

@shiraze
Copy link

shiraze commented Jun 25, 2018

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/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants