Source map created points to system paths instead of relative path

852 Views Asked by At

I am using UglifyJS2 to minify my javascript source files into one large minified file. Here is the relevant bit of code that I use:

var options = options || {};
options.outSourceMap = 'minfile.js.map';

try {
    // "scripts" is an array of absolute paths to the javascript files
    var minified = uglifyjs.minify(scripts, options);

    // minified.map contains the minfile
} catch (err) {
    // handle errs here
}

The minfile itself contains absolute paths to the source files:

{
    "version": 3,
    "file": "nodebb.min.js.map",
    "sources": [
        "/path/to/folder/jquery/js/jquery.js",
        "/path/to/folder/another/lib.js",
        ...
    ],
    ...
}

The problem is, the source files I'm passing in are not accessible publically, they are only compiled into the minfile and only the minfile is accessible. Therefore, my source map seems to be pointless, as Chrome (in my case) tries to load http://mydomain.com/path/to/folder/jquery/js/jquery.js instead of reading the file on my local fs.

What am I doing wrong?

2

There are 2 best solutions below

0
On

Looks like the UglifyJS2 repo is missing a bunch of options for the node API, but this PR fixed it up a bit: https://github.com/mishoo/UglifyJS2/pull/192

So I'm using that in my package.json now:

"dependencies": {
    ...
    "uglify-js": "git+https://github.com/julianlam/UglifyJS2.git",
    ...
}

Then prefix can be passed into the minify method to lop off parts of the path that aren't needed.

0
On

Another option is to use the "Hard Way"

And in "source map generating" instead of source_map.toString() use

var json = source_map.get().toJSON();
json.sources = [ /* override sources */ ];
JSON.stringify(json);