JS: add DCE devmode. Fix mapping paths in source maps
See KT-20210, KT-21307
This commit is contained in:
@@ -120,8 +120,12 @@ class DeadCodeElimination(private val logConsumer: (DCELogLevel, String) -> Unit
|
||||
val sourceMapFile = File(file.outputPath + ".map")
|
||||
val textOutput = TextOutputImpl()
|
||||
val sourceMapBuilder = SourceMap3Builder(File(file.outputPath), textOutput, "")
|
||||
val sourcePathResolver = SourceFilePathResolver(mutableListOf(), File(file.outputPath).parentFile)
|
||||
val consumer = SourceMapBuilderConsumer(sourceMapBuilder, sourcePathResolver, true, true)
|
||||
|
||||
val inputFile = File(file.resource.name)
|
||||
val sourceBaseDir = if (inputFile.exists()) inputFile.parentFile else File(".")
|
||||
|
||||
val sourcePathResolver = SourceFilePathResolver(emptyList(), File(file.outputPath).parentFile)
|
||||
val consumer = SourceMapBuilderConsumer(sourceBaseDir, sourceMapBuilder, sourcePathResolver, true, true)
|
||||
block.accept(JsToStringGenerationVisitor(textOutput, consumer))
|
||||
val sourceMapContent = sourceMapBuilder.build()
|
||||
sourceMapBuilder.addLink()
|
||||
|
||||
@@ -21,13 +21,19 @@ import java.io.FileInputStream
|
||||
import java.io.InputStream
|
||||
import java.util.zip.ZipFile
|
||||
|
||||
class InputResource(val name: String, val reader: () -> InputStream) {
|
||||
class InputResource(val name: String, val lastModified: () -> Long, val reader: () -> InputStream) {
|
||||
companion object {
|
||||
fun file(path: String): InputResource = InputResource(path) { FileInputStream(File(path)) }
|
||||
fun file(path: String): InputResource = InputResource(path, { File(path).lastModified() }) { FileInputStream(File(path)) }
|
||||
|
||||
fun zipFile(path: String, entryPath: String): InputResource = InputResource("$path!$entryPath") {
|
||||
val zipFile = ZipFile(path)
|
||||
zipFile.getInputStream(zipFile.getEntry(entryPath))
|
||||
fun zipFile(path: String, entryPath: String): InputResource =
|
||||
InputResource("$path!$entryPath", { getZipModificationTime(path, entryPath) }) {
|
||||
val zipFile = ZipFile(path)
|
||||
zipFile.getInputStream(zipFile.getEntry(entryPath))
|
||||
}
|
||||
|
||||
private fun getZipModificationTime(path: String, entryPath: String): Long {
|
||||
val result = ZipFile(path).getEntry(entryPath).time
|
||||
return if (result != -1L) result else File(path).lastModified()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -460,7 +460,8 @@ abstract class BasicBoxTest(
|
||||
val output = TextOutputImpl()
|
||||
val pathResolver = SourceFilePathResolver(mutableListOf(File(".")), null)
|
||||
val sourceMapBuilder = SourceMap3Builder(outputFile, output, "")
|
||||
generatedProgram.accept(JsToStringGenerationVisitor(output, SourceMapBuilderConsumer(sourceMapBuilder, pathResolver, false, false)))
|
||||
generatedProgram.accept(JsToStringGenerationVisitor(
|
||||
output, SourceMapBuilderConsumer(File("."), sourceMapBuilder, pathResolver, false, false)))
|
||||
val code = output.toString()
|
||||
val generatedSourceMap = sourceMapBuilder.build()
|
||||
|
||||
|
||||
@@ -34,6 +34,9 @@ import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class SourceMapBuilderConsumer implements SourceLocationConsumer {
|
||||
@NotNull
|
||||
private final File sourceBaseDir;
|
||||
|
||||
@NotNull
|
||||
private final SourceMapMappingConsumer mappingConsumer;
|
||||
|
||||
@@ -48,10 +51,12 @@ public class SourceMapBuilderConsumer implements SourceLocationConsumer {
|
||||
private final List<Object> sourceStack = new ArrayList<>();
|
||||
|
||||
public SourceMapBuilderConsumer(
|
||||
@NotNull File sourceBaseDir,
|
||||
@NotNull SourceMapMappingConsumer mappingConsumer,
|
||||
@NotNull SourceFilePathResolver pathResolver,
|
||||
boolean provideCurrentModuleContent, boolean provideExternalModuleContent
|
||||
) {
|
||||
this.sourceBaseDir = sourceBaseDir;
|
||||
this.mappingConsumer = mappingConsumer;
|
||||
this.pathResolver = pathResolver;
|
||||
this.provideCurrentModuleContent = provideCurrentModuleContent;
|
||||
@@ -109,7 +114,23 @@ public class SourceMapBuilderConsumer implements SourceLocationConsumer {
|
||||
else if (sourceInfo instanceof JsLocationWithSource) {
|
||||
JsLocationWithSource location = (JsLocationWithSource) sourceInfo;
|
||||
Supplier<Reader> contentSupplier = provideExternalModuleContent ? location.getSourceProvider()::invoke : () -> null;
|
||||
mappingConsumer.addMapping(location.getFile(), location.getIdentityObject(), contentSupplier,
|
||||
String path;
|
||||
|
||||
File absFile = new File(location.getFile()).isAbsolute() ?
|
||||
new File(location.getFile()) :
|
||||
new File(sourceBaseDir, location.getFile());
|
||||
if (absFile.isAbsolute()) {
|
||||
try {
|
||||
path = pathResolver.getPathRelativeToSourceRoots(absFile);
|
||||
}
|
||||
catch (IOException e) {
|
||||
path = location.getFile();
|
||||
}
|
||||
}
|
||||
else {
|
||||
path = location.getFile();
|
||||
}
|
||||
mappingConsumer.addMapping(path, location.getIdentityObject(), contentSupplier,
|
||||
location.getStartLine(), location.getStartChar());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,7 @@ abstract class TranslationResult protected constructor(val diagnostics: Diagnost
|
||||
val sourceMapContentEmbedding = config.sourceMapContentEmbedding
|
||||
val pathResolver = SourceFilePathResolver.create(config)
|
||||
SourceMapBuilderConsumer(
|
||||
File("."),
|
||||
sourceMapBuilder,
|
||||
pathResolver,
|
||||
sourceMapContentEmbedding == SourceMapSourceEmbedding.ALWAYS,
|
||||
|
||||
Reference in New Issue
Block a user