简体   繁体   中英

Why doesn't GruntJS break execution if exception is thrown?

I'm exploring grunt-inline module and faced with the following problem. I have the following task's config:

grunt.initConfig({
  inline: {
    options: {
       uglify: true
    },
    dist: {
      src: 'src/index.html',
      dest: 'dist/index.html'
    }
  }
});

From the config above uglify: true option means executing UglifyJS.minify on this line :

var c = options.uglify ? UglifyJS.minify(inlineFilePath).code : grunt.file.read( inlineFilePath );

The problem is that if UglifyJS.minify throws exception (which I'm just faced with) the grunt inline command will not fails but exits quite.

So, either the problem with this module of lacking some grunt's flags from my side?

It would be great if you could explain me how to break grunt execution on any exceptions.

EDIT: Also I've noticed that if I catch exception and re-throw it myself it breaks grunt well, like the following:

try {
    var c = options.uglify ? UglifyJS.minify(inlineFilePath).code : grunt.file.read( inlineFilePath );
} catch(e) {
    throw new Error(e.message);
}

So why does't exception from UglifyJS.minify(inlineFilePath).code break gruntjs task?

Looks like a problem with the module. I can't seem to find a way to flag and return these errors. You could try replace that line with:

var c;
if(options.uglify){
    try{
        c = UglifyJS.minify(inlineFilePath).code
    }catch(err){
        grunt.log.error(err);//catch error and send to grunt
    }
}else{
        c=grunt.file.read( inlineFilePath );
}

UPDATE: So why does't exception from UglifyJS.minify(inlineFilePath).code break gruntjs task?

Looks like this is an issue with the error itself: JS_Parse_Error This is not a properly constructed error and is not caught by grunt. Looks like you have to catch it yourself and wrap it in a new Error() as you have done above until a fix goes in. See link below:

https://github.com/mishoo/UglifyJS2/issues/348

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM