[JS IR] Add per-module output module name

This commit is contained in:
Ilya Goncharov
2021-05-04 15:37:24 +03:00
committed by TeamCityServer
parent 4d7f7fc50f
commit 228c6879f5
7 changed files with 43 additions and 35 deletions
@@ -157,8 +157,8 @@ class K2JSCompilerArguments : CommonCompilerArguments() {
@Argument(value = "-Xir-per-module", description = "Splits generated .js per-module")
var irPerModule: Boolean by FreezableVar(false)
@Argument(value = "-Xir-per-module-prefix", description = "Adds a custom prefix to the splitted js files")
var irPerModulePrefix: String? by NullableStringFreezableVar(null)
@Argument(value = "-Xir-per-module-output-name", description = "Adds a custom output name to the splitted js files")
var irPerModuleOutputName: String? by NullableStringFreezableVar(null)
@Argument(
value = "-Xinclude",
@@ -207,8 +207,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
irFactory = PersistentIrFactory(), // TODO IrFactoryImpl?
outputKlibPath = outputFile.path,
nopack = arguments.irProduceKlibDir,
jsOutputName = moduleName
.substringAfterLast(":"),
jsOutputName = arguments.irPerModuleOutputName,
)
}
@@ -277,7 +276,6 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
relativeRequirePath = true,
propertyLazyInitialization = arguments.irPropertyLazyInitialization,
legacyPropertyAccess = arguments.irLegacyPropertyAccess,
irPerModulePrefix = arguments.irPerModulePrefix
)
val jsCode = if (arguments.irDce && !arguments.irDceDriven) compiledModule.dceJsCode!! else compiledModule.jsCode!!
@@ -53,7 +53,6 @@ fun compile(
relativeRequirePath: Boolean = false,
propertyLazyInitialization: Boolean,
legacyPropertyAccess: Boolean = false,
irPerModulePrefix: String? = null,
): CompilerResult {
val (moduleFragment: IrModuleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer, moduleToName) =
loadIr(project, mainModule, analyzer, configuration, allDependencies, friendDependencies, irFactory, forceAllJs)
@@ -112,7 +111,6 @@ fun compile(
multiModule = multiModule,
relativeRequirePath = relativeRequirePath,
moduleToName = moduleToName,
irPerModulePrefix = irPerModulePrefix,
)
return transformer.generateModule(allModules)
} else {
@@ -125,7 +123,6 @@ fun compile(
multiModule = multiModule,
relativeRequirePath = relativeRequirePath,
moduleToName = moduleToName,
irPerModulePrefix = irPerModulePrefix,
)
return transformer.generateModule(allModules)
}
@@ -33,7 +33,6 @@ class IrModuleToJsTransformer(
private val multiModule: Boolean = false,
private val relativeRequirePath: Boolean = false,
private val moduleToName: Map<IrModuleFragment, String> = emptyMap(),
private val irPerModulePrefix: String? = null,
) {
private val generateRegionComments = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_REGION_COMMENTS)
@@ -193,7 +192,7 @@ class IrModuleToJsTransformer(
}
private fun IrModuleFragment.externalModuleName(): String {
return (irPerModulePrefix ?: "") + (moduleToName[this] ?: sanitizeName(safeName))
return moduleToName[this] ?: sanitizeName(safeName)
}
private fun generateCrossModuleImports(
@@ -70,7 +70,6 @@ import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.utils.DFS
import java.io.File
import java.util.*
import org.jetbrains.kotlin.konan.file.File as KFile
val KotlinLibrary.moduleName: String
@@ -243,7 +242,8 @@ fun loadIr(
JsIrLinker.JsFePluginContext(moduleDescriptor, symbolTable, typeTranslator, irBuiltIns)
}
val moduleFragmentToUniqueName = mutableMapOf<IrModuleFragment, String>()
val irLinker = JsIrLinker(psi2IrContext.moduleDescriptor, messageLogger, irBuiltIns, symbolTable, functionFactory, feContext, null)
val irLinker =
JsIrLinker(psi2IrContext.moduleDescriptor, messageLogger, irBuiltIns, symbolTable, functionFactory, feContext, null)
val deserializedModuleFragments = sortDependencies(allDependencies.getFullList(), depsDescriptors.descriptors).map { klib ->
irLinker.deserializeIrModuleHeader(depsDescriptors.getModuleDescriptor(klib), klib).also { moduleFragment ->
klib.manifestProperties.getProperty(KLIB_PROPERTY_JS_OUTPUT_NAME)?.let {
@@ -319,7 +319,11 @@ private fun runAnalysisAndPreparePsi2Ir(
Psi2IrConfiguration(errorIgnorancePolicy.allowErrors)
)
val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc), irFactory)
return psi2Ir.createGeneratorContext(analysisResult.moduleDescriptor, analysisResult.bindingContext, symbolTable) to analysisResult.hasErrors
return psi2Ir.createGeneratorContext(
analysisResult.moduleDescriptor,
analysisResult.bindingContext,
symbolTable
) to analysisResult.hasErrors
}
fun GeneratorContext.generateModuleFragmentWithPlugins(
@@ -571,20 +575,20 @@ fun serializeModuleIntoKlib(
}
}
buildKotlinLibrary(
linkDependencies = dependencies,
ir = fullSerializedIr,
metadata = serializedMetadata,
dataFlowGraph = null,
manifestProperties = properties,
moduleName = moduleName,
nopack = nopack,
perFile = perFile,
output = klibPath,
versions = versions,
builtInsPlatform = BuiltInsPlatform.JS
)
}
buildKotlinLibrary(
linkDependencies = dependencies,
ir = fullSerializedIr,
metadata = serializedMetadata,
dataFlowGraph = null,
manifestProperties = properties,
moduleName = moduleName,
nopack = nopack,
perFile = perFile,
output = klibPath,
versions = versions,
builtInsPlatform = BuiltInsPlatform.JS
)
}
const val KLIB_PROPERTY_JS_OUTPUT_NAME = "jsOutputName"
@@ -617,11 +621,12 @@ private fun compareMetadataAndGoToNextICRoundIfNeeded(
if (nextRoundChecker.shouldGoToNextRound()) throw IncrementalNextRoundException()
}
private fun KlibMetadataIncrementalSerializer(configuration: CompilerConfiguration, project: Project, allowErrors: Boolean) = KlibMetadataIncrementalSerializer(
languageVersionSettings = configuration.languageVersionSettings,
metadataVersion = configuration.metadataVersion,
project = project,
exportKDoc = false,
skipExpects = !configuration.expectActualLinker,
allowErrorTypes = allowErrors
)
private fun KlibMetadataIncrementalSerializer(configuration: CompilerConfiguration, project: Project, allowErrors: Boolean) =
KlibMetadataIncrementalSerializer(
languageVersionSettings = configuration.languageVersionSettings,
metadataVersion = configuration.metadataVersion,
project = project,
exportKDoc = false,
skipExpects = !configuration.expectActualLinker,
allowErrorTypes = allowErrors
)
@@ -24,5 +24,7 @@ internal const val PRODUCE_ZIPPED_KLIB = "-Xir-produce-klib-file"
internal const val MODULE_NAME = "-Xir-module-name"
internal const val PER_MODULE_OUTPUT_NAME = "-Xir-per-module-output-name"
fun KotlinJsOptions.isProduceUnzippedKlib() = PRODUCE_UNZIPPED_KLIB in freeCompilerArgs
fun KotlinJsOptions.isProduceZippedKlib() = PRODUCE_ZIPPED_KLIB in freeCompilerArgs
@@ -58,6 +58,13 @@ abstract class KotlinJsIrSubTarget(
internal fun configure() {
NpmResolverPlugin.apply(project)
target.compilations.all {
val npmProject = it.npmProject
it.kotlinOptions {
freeCompilerArgs += "$PER_MODULE_OUTPUT_NAME=${npmProject.name}"
}
}
configureTests()
}