简体   繁体   中英

Source map created points to system paths instead of relative path

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?

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.

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

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