Skip to content

Source map generation creates invalid paths when source root is specified in input map file #748

@diegope

Description

@diegope

Ran into a problem when uglifying a js file and passing the map file into Uglify. It seems that uglify will prepend the sourceRoot into the individual paths, but also expand the paths to their absolute form.

So, with sourceRoot = /one/two/, and a file /three.coffee, you end up with /one/two//one/two/three.coffee. I've modified the add function in lib/sourcemap.js to account for this behavior (with an albeit slightly dirty fix):

function add(source, gen_line, gen_col, orig_line, orig_col, name) {
        if (orig_map) {
            var info = orig_map.originalPositionFor({
                line: orig_line,
                column: orig_col
            });
            if (info.source === null) {
                return;
            }
            source = info.source;
            orig_line = info.line;
            orig_col = info.column;
            name = info.name || name;
        }

        var srcRoot = options.root || options.orig.sourceRoot || orig_map.sourceRoot;

        if (srcRoot && srcRoot.length > 0) {
             // This prevents duplication of the source root path in individual file paths
            if(source.indexOf(srcRoot) === 0) {
                source = source.slice(srcRoot.length);
            }

            // This prevents duplication of slashes between source root and individual files paths - with the source root taking precedence
            if(srcRoot.slice(-1) === '/' && source.charAt(0) === '/') {
                source = source.slice(1);
            }
        }

        generator.addMapping({
            generated : { line: gen_line + options.dest_line_diff, column: gen_col },
            original  : { line: orig_line + options.orig_line_diff, column: orig_col },
            source    : source,
            name      : name
        });
    }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions