msbuilduglifyjs

uglifyjs command for content getting error


I am trying to use uglifyjs to minify and create a sourcemap when running msbuild. I'm getting an error, so I've cut MSBuild out of the loop and run it from the command line, but am still getting an error if I include content:

This works from the command line, creating the appropriate min file:

node_modules\.bin\uglifyjs.cmd wwwroot\lib\jquery.validate.unobtrusive.js 
   --source-map "url='jquery.validate.unobtrusive.min.js.map',includeSources" 
   -o wwwroot\lib\jquery.validate.unobtrusive.min.js -c -m

But this (which is basically the example I got from another project) throws an error

node_modules\.bin\uglifyjs.cmd wwwroot\lib\jquery.validate.unobtrusive.js 
   --source-map "url='jquery.validate.unobtrusive.min.js.map',content='wwwroot/lib/jquery.validate.unobtrusive.js.map',includeSources" 
   -o wwwroot\lib\jquery.validate.unobtrusive.min.js -c -m
ERROR: invalid input source map: wwwroot/lib/jquery.validate.unobtrusive.js.map
    at parse_source_map (eval at <anonymous> (C:\Users\xxx\source\temp\node_modules\uglify-js\tools\node.js:18:1), <anonymous>:22118:15)
    at Object.minify (eval at <anonymous> (C:\Users\xxx\source\temp\node_modules\uglify-js\tools\node.js:18:1), <anonymous>:22234:38)
    at run (C:\Users\xxx\source\temp\node_modules\uglify-js\bin\uglifyjs:379:27)
    at Object.<anonymous> (C:\Users\xxx\source\temp\node_modules\uglify-js\bin\uglifyjs:287:5)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

What am


Solution

  • The error is exactly as it states...

    invalid input source map

    --source-map has various options, in the example throwing the error you have

    content='wwwroot/lib/jquery.validate.unobtrusive.js.map'

    This is called a composed source map

    When you're compressing JS code that was output by a compiler such as CoffeeScript, mapping to the JS code won't be too helpful. Instead, you'd like to map back to the original code (i.e. CoffeeScript). UglifyJS has an option to take an input source map. Assuming you have a mapping from CoffeeScript → compiled JS, UglifyJS can generate a map from CoffeeScript → compressed JS by mapping every token in the compiled JS to its original location.

    To use this feature pass --source-map "content='/path/to/input/source.map'" or --source-map "content=inline" if the source map is included inline with the sources.

    see https://github.com/mishoo/UglifyJS#composed-source-map

    However that file wwwroot/lib/jquery.validate.unobtrusive.js.map is not well formed. You can see the error is thrown at parse_source_map

    If you take a look at that method you can see that all it is doing is attempting to parse the content as the JavaScript value. e.g.

    function parse_source_map(content) {
        try {
            return JSON.parse(content); // <- this is what is failing
        } catch (ex) {
            // ... and this is the error you are seeing
            throw new Error("invalid input source map: " + content);
        }
    }
    

    To fix, ensure that the file wwwroot/lib/jquery.validate.unobtrusive.js.map is acceptable to JSON.parse or else remove the content option as it seems to be redundant in your case.