Allow to embed source files into JS source maps

This commit is contained in:
Alexey Andreev
2017-06-14 14:03:37 +03:00
parent 73c37ecd25
commit a0e1bde594
40 changed files with 491 additions and 81 deletions
@@ -48,6 +48,15 @@ open class DefaultValues(val defaultValue: String, val possibleValues: List<Stri
listOf("\"plain\"", "\"amd\"", "\"commonjs\"", "\"umd\"")
)
object JsSourceMapContentModes : DefaultValues(
"\"${K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_INLINING}\"",
listOf(
K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_NEVER,
K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_ALWAYS,
K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_INLINING
).map { "\"$it\""}
)
object JsMain : DefaultValues(
"\"" + CALL + "\"",
listOf("\"" + CALL + "\"", "\"" + NO_CALL + "\"")
@@ -52,6 +52,14 @@ public class K2JSCompilerArguments extends CommonCompilerArguments {
)
public String sourceMapSourceRoots;
@GradleOption(DefaultValues.JsSourceMapContentModes.class)
@Argument(
value = "-source-map-embed-sources",
valueDescription = "{ always, never, inlining }",
description = "Embed source files into source map"
)
public String sourceMapEmbedSources;
@GradleOption(DefaultValues.BooleanTrueDefault.class)
@Argument(value = "-meta-info", description = "Generate .meta.js and .kjsm files with metadata. Use to create a library")
public boolean metaInfo;
@@ -24,4 +24,8 @@ public interface K2JsArgumentConstants {
String MODULE_AMD = "amd";
String MODULE_COMMONJS = "commonjs";
String MODULE_UMD = "umd";
String SOURCE_MAP_SOURCE_CONTENT_ALWAYS = "always";
String SOURCE_MAP_SOURCE_CONTENT_NEVER = "never";
String SOURCE_MAP_SOURCE_CONTENT_INLINING = "inlining";
}
@@ -51,6 +51,7 @@ import org.jetbrains.kotlin.js.analyzer.JsAnalysisResult;
import org.jetbrains.kotlin.js.config.EcmaVersion;
import org.jetbrains.kotlin.js.config.JSConfigurationKeys;
import org.jetbrains.kotlin.js.config.JsConfig;
import org.jetbrains.kotlin.js.config.SourceMapSourceEmbedding;
import org.jetbrains.kotlin.js.facade.K2JSTranslator;
import org.jetbrains.kotlin.js.facade.MainCallParameters;
import org.jetbrains.kotlin.js.facade.TranslationResult;
@@ -74,12 +75,17 @@ import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
private static final Map<String, ModuleKind> moduleKindMap = new HashMap<>();
private static final Map<String, SourceMapSourceEmbedding> sourceMapContentEmbeddingMap = new LinkedHashMap<>();
static {
moduleKindMap.put(K2JsArgumentConstants.MODULE_PLAIN, ModuleKind.PLAIN);
moduleKindMap.put(K2JsArgumentConstants.MODULE_COMMONJS, ModuleKind.COMMON_JS);
moduleKindMap.put(K2JsArgumentConstants.MODULE_AMD, ModuleKind.AMD);
moduleKindMap.put(K2JsArgumentConstants.MODULE_UMD, ModuleKind.UMD);
sourceMapContentEmbeddingMap.put(K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_ALWAYS, SourceMapSourceEmbedding.ALWAYS);
sourceMapContentEmbeddingMap.put(K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_NEVER, SourceMapSourceEmbedding.NEVER);
sourceMapContentEmbeddingMap.put(K2JsArgumentConstants.SOURCE_MAP_SOURCE_CONTENT_INLINING, SourceMapSourceEmbedding.INLINING);
}
public static void main(String... args) {
@@ -357,8 +363,25 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
messageCollector.report(
ERROR, "Unknown module kind: " + moduleKindName + ". Valid values are: plain, amd, commonjs, umd", null
);
moduleKind = ModuleKind.PLAIN;
}
configuration.put(JSConfigurationKeys.MODULE_KIND, moduleKind);
String sourceMapEmbedContentString = arguments.sourceMapEmbedSources;
SourceMapSourceEmbedding sourceMapContentEmbedding = sourceMapEmbedContentString != null ?
sourceMapContentEmbeddingMap.get(sourceMapEmbedContentString) :
SourceMapSourceEmbedding.INLINING;
if (sourceMapContentEmbedding == null) {
String message = "Unknown source map source embedding mode: " + sourceMapEmbedContentString + ". Valid values are: " +
StringUtil.join(sourceMapContentEmbeddingMap.keySet(), ", ");
messageCollector.report(ERROR, message, null);
sourceMapContentEmbedding = SourceMapSourceEmbedding.INLINING;
}
configuration.put(JSConfigurationKeys.SOURCE_MAP_EMBED_SOURCES, sourceMapContentEmbedding);
if (!arguments.sourceMap && sourceMapEmbedContentString != null) {
messageCollector.report(WARNING, "source-map-embed-sources argument has no effect without source map", null);
}
}
@NotNull