Generate relative paths in JS source maps
Also, add CLI options to manipulate prefixes of path See KT-4078
This commit is contained in:
@@ -19,7 +19,9 @@ package org.jetbrains.kotlin.cli.js;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.ExceptionUtil;
|
||||
import com.intellij.util.SmartList;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
@@ -34,6 +36,7 @@ import org.jetbrains.kotlin.cli.common.ExitCode;
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments;
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants;
|
||||
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport;
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity;
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
||||
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
||||
@@ -58,8 +61,8 @@ import org.jetbrains.kotlin.utils.PathUtil;
|
||||
import org.jetbrains.kotlin.utils.StringsKt;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static org.jetbrains.kotlin.cli.common.ExitCode.COMPILATION_ERROR;
|
||||
import static org.jetbrains.kotlin.cli.common.ExitCode.OK;
|
||||
@@ -261,6 +264,23 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
||||
|
||||
if (arguments.sourceMap) {
|
||||
configuration.put(JSConfigurationKeys.SOURCE_MAP, true);
|
||||
if (arguments.sourceMapPrefix != null) {
|
||||
configuration.put(JSConfigurationKeys.SOURCE_MAP_PREFIX, arguments.sourceMapPrefix);
|
||||
}
|
||||
|
||||
String sourceMapSourceRoots = arguments.sourceMapSourceRoots != null ?
|
||||
arguments.sourceMapSourceRoots :
|
||||
calculateSourceMapSourceRoot(messageCollector, arguments);
|
||||
List<String> sourceMapSourceRootList = StringUtil.split(sourceMapSourceRoots, File.pathSeparator);
|
||||
configuration.put(JSConfigurationKeys.SOURCE_MAP_SOURCE_ROOTS, sourceMapSourceRootList);
|
||||
}
|
||||
else {
|
||||
if (arguments.sourceMapPrefix != null) {
|
||||
messageCollector.report(WARNING, "source-map-prefix argument has no effect without source map", null);
|
||||
}
|
||||
if (arguments.sourceMapSourceRoots != null) {
|
||||
messageCollector.report(WARNING, "source-map-source-root argument has no effect without source map", null);
|
||||
}
|
||||
}
|
||||
if (arguments.metaInfo) {
|
||||
configuration.put(JSConfigurationKeys.META_INFO, true);
|
||||
@@ -299,6 +319,58 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
||||
configuration.put(JSConfigurationKeys.MODULE_KIND, moduleKind);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String calculateSourceMapSourceRoot(
|
||||
@NotNull MessageCollector messageCollector,
|
||||
@NotNull K2JSCompilerArguments arguments
|
||||
) {
|
||||
File commonPath = null;
|
||||
List<File> pathToRoot = new ArrayList<>();
|
||||
Map<File, Integer> pathToRootIndexes = new HashMap<>();
|
||||
|
||||
try {
|
||||
for (String path : arguments.freeArgs) {
|
||||
File file = new File(path).getCanonicalFile();
|
||||
if (commonPath == null) {
|
||||
commonPath = file;
|
||||
|
||||
while (file != null) {
|
||||
pathToRoot.add(file);
|
||||
file = file.getParentFile();
|
||||
}
|
||||
Collections.reverse(pathToRoot);
|
||||
|
||||
for (int i = 0; i < pathToRoot.size(); ++i) {
|
||||
pathToRootIndexes.put(pathToRoot.get(i), i);
|
||||
}
|
||||
}
|
||||
else {
|
||||
while (file != null) {
|
||||
Integer existingIndex = pathToRootIndexes.get(file);
|
||||
if (existingIndex != null) {
|
||||
existingIndex = Math.min(existingIndex, pathToRoot.size() - 1);
|
||||
pathToRoot.subList(existingIndex + 1, pathToRoot.size()).clear();
|
||||
commonPath = pathToRoot.get(pathToRoot.size() - 1);
|
||||
break;
|
||||
}
|
||||
file = file.getParentFile();
|
||||
}
|
||||
if (file == null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
String text = ExceptionUtil.getThrowableText(e);
|
||||
messageCollector.report(CompilerMessageSeverity.ERROR, "IO error occurred calculating source root:\n" + text, null);
|
||||
return ".";
|
||||
}
|
||||
|
||||
return commonPath != null ? commonPath.getPath() : ".";
|
||||
}
|
||||
|
||||
|
||||
private static MainCallParameters createMainCallParameters(String main) {
|
||||
if (K2JsArgumentConstants.NO_CALL.equals(main)) {
|
||||
return MainCallParameters.noCall();
|
||||
|
||||
Reference in New Issue
Block a user