google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.39k stars 1.15k forks source link

Source map files generated by command line and Compiler are different #951

Closed ova2 closed 9 years ago

ova2 commented 9 years ago

You fixed the issue for ANT task https://github.com/google/closure-compiler/issues/588 But if you use Maven, the issue with absolute path in "sources" is still here. I use CompilerOptions and com.google.javascript.jscomp.Compiler

CompilerOptions options = new CompilerOptions();
 ...
options.setSourceMapDetailLevel(SourceMap.DetailLevel.ALL);
options.setSourceMapFormat(SourceMap.Format.V3);
ptions.setSourceMapOutputPath(...);
...
Compiler compiler = new Compiler();
Result result = compiler.compile(..., ..., options);

I get something like in the source map

"sources":["C:/Projects/primefaces-extensions/core/target/classes/META-INF/resources/primefaces-extensions/primefaces-extensions.source.js"]

but it should be

"sources":["primefaces-extensions.source.js"]

because the source map and the source JS file are located in the same folder.

How to fix this issue? Setting options.setSourceMapLocationMappings(...) didn't help as well.

concavelenz commented 9 years ago

Why didn't setSourceMapLocationMappings work for you?

ova2 commented 9 years ago

I don't know why. It only appends prefix. Right? I tried options.setSourceMapLocationMappings(", ""),options.setSourceMapLocationMappings(".", ""), options.setSourceMapLocationMappings("", ".")`, etc. Nothing works. The patch in "sources" is absolute.

concavelenz commented 9 years ago

It should be a prefix something like

options.setSourceMapLocationMappings(ImmutableList.of(new LocationMapping("C:/Projects/primefaces-extensions/core/target/classes/META-INF/resources/primefaces-extensions/","")))

Perhaps with the slashes the other way.

ova2 commented 9 years ago

I tried your suggestion. It doesn't work. I assume it is the same bug as here https://github.com/google/closure-compiler/issues/588

ova2 commented 9 years ago

Huh, I could fix this problem. The SourceMap.java has

// Replace backslashes (the file separator used on Windows systems).
if (File.separatorChar == '\\') {
    sourceFile = sourceFile.replace('\\', '/');
}

And this was the problem. I didn't know why you do this guys. I fixed the problem by doing the same:

File sourceMapFile = new File(outputFilePath + ".map");
options.setSourceMapOutputPath(sourceMapFile.getCanonicalPath());

String prefix = outputFilePath.substring(0, outputFilePath.lastIndexOf(File.separator) + 1);
// Replace backslashes (the file separator used on Windows systems).
// This is needed due to the same code in SourceMap.java
if (File.separatorChar == '\\') {
    prefix = prefix.replace('\\', '/');
}

List<SourceMap.LocationMapping> sourceMapLocationMappings =
        ImmutableList.of(new SourceMap.LocationMapping(prefix, ""));
options.setSourceMapLocationMappings(sourceMapLocationMappings);