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
+2
View File
@@ -7,6 +7,8 @@ where possible options include:
-source-map-prefix Prefix for paths in a source map
-source-map-source-roots <path>
Base directories which are used to calculate relative paths to source files in source map
-source-map-embed-sources { always, never, inlining }
Embed source files into source map
-meta-info Generate .meta.js and .kjsm files with metadata. Use to create a library
-target { v5 } Generate JS files for specific ECMA version
-module-kind { plain, amd, commonjs, umd }
+7
View File
@@ -0,0 +1,7 @@
$TESTDATA_DIR$/sourceMap.kt
-no-stdlib
-source-map
-source-map-embed-sources
always
-output
$TEMP_DIR$/out.js
+1
View File
@@ -0,0 +1 @@
OK
+3
View File
@@ -0,0 +1,3 @@
// EXISTS: out.js
// CONTAINS: out.js.map, "sourceMap.kt"
// CONTAINS: out.js.map, "var log = \"\"\n\nfun foo(x: String) {
@@ -539,6 +539,12 @@ public class CliTestGenerated extends AbstractCliTest {
doJsTest(fileName);
}
@TestMetadata("sourceMapEmbedSources.args")
public void testSourceMapEmbedSources() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/sourceMapEmbedSources.args");
doJsTest(fileName);
}
@TestMetadata("sourceMapPrefix.args")
public void testSourceMapPrefix() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/sourceMapPrefix.args");
@@ -124,7 +124,9 @@ object JsLibraryUtils {
librariesWithoutSourceMaps += JsLibrary(content, relativePath, null)
}
else if (entryName.endsWith(KotlinJavascriptMetadataUtils.JS_MAP_EXT)) {
possibleMapFiles[entryName.removeSuffix(KotlinJavascriptMetadataUtils.JS_MAP_EXT)] = entry
val correspondingJsPath = entryName.removeSuffix(KotlinJavascriptMetadataUtils.JS_MAP_EXT) +
KotlinJavascriptMetadataUtils.JS_EXT
possibleMapFiles[correspondingJsPath] = entry
}
}
}