Search code examples
angularjsgruntjsuglifyjsgrunt-contrib-uglify

Error trying to create sourcemap with grunt-contrib-uglify


I'm attempting to add sourcemaps for an AngularJS application derived from the ngBoilerplate template, which uses grunt to build and grunt-contrib-uglify to minify the JavaScript sources into one app.js file.

The problem is that grunt can't actually write the sourcemap to disk, with this cryptic error (used grunt --verbose, edited only slightly):

Running "uglify" task

Running "uglify:compile" (uglify) task
Verifying property uglify.compile exists in config...OK
Files: release/assets/MyApp-1.0.0.debug.js -> release/assets/MyApp-1.0.0.js
Options: banner="", footer="", compress={"warnings":false}, mangle={}, beautify=false, report="min", expression=false, sourceMap
Minifying with UglifyJS...Reading release/assets/MyApp-1.0.0.debug.js...OK
OK
Writing release/assets/MyApp-1.0.0.js...OK
Writing release/assets/MyApp-1.0.0.js.map...ERROR
Warning: Unable to write "release/assets/MyApp-1.0.0.map" file (Error code: undefined). Use --force to continue.

Aborted due to warnings.

The critical section being:

Warning: Unable to write "release/assets/MyApp-1.0.0.map" file (Error code: undefined). Use --force to continue.

Now you may be tempted to say this is a duplicate of the question Grunt Uglify source map “unable to write” except that case was a simple gotcha where the asker was using a new configuration style with an older version of the package, resulting in the error Unable to write "true" file. Not so in my case. It's pretty clear that the file name it is attempting to write is correct (the JS file plus the ".map" extension) but that the system can't do it and gives a colossally unhelpful "Error code: undefined" back.

Versions from the packages.json file - and I cleaned out the node_modules folder and re-ran npm install to make sure this is what was in use:

"devDependencies": {
  "grunt": "0.4.5",
  "grunt-recess": "0.6.1",
  "grunt-contrib-jshint": "0.10.0",
  "grunt-contrib-concat": "0.4.0",
  "grunt-contrib-watch": "0.6.1",
  "grunt-contrib-uglify": "0.5.0",
  "grunt-karma": "0.6.2",
  "grunt-ngmin-with-comments": "0.0.3",
  "grunt-html2js": "0.2.3",
  "grunt-groundskeeper": "0.1.9",
  "lodash": "2.4.1",
  "grunt-contrib-clean": "0.5.0",
  "grunt-contrib-copy": "0.5.0",
  "grunt-contrib-connect": "^0.7.1"
}

Inside node_modules, the uglify-js version is 2.4.14.

My Gruntfile config isn't exactly boilerplate ngBoilerplate - I've modified it some so that I can get a .debug.js that is unminified and a .js that is minified. Here it is:

uglify: {
    compile: {
        options: {
            sourceMap: true
        },
        files: {
            '<%= minJs %>': '<%= debugJs %>'
        }
    }
}

I have also tried:

  • specifying the sourceMapName property
    • As a string
    • As a function that takes the jsName and appends '.map' to it
    • As a function that does not try to write in the release/assets subdirectory
  • Not using the grunt templates and specifying the filenames for minJs and debugJs explicitly.

Nothing has worked. Any ideas?


Solution

  • I'm getting the same error with a different setup. Everything goes fine if I disable sourcemaps. I spent a couple hours debugging the error and gave up.

    Just switch to an older version of grunt-contrib-uglify such as ~0.4 until they sort out what is wrong.