diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/GradleOption.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/GradleOption.kt index a97b8dbb38d..52aeee76ae6 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/GradleOption.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/GradleOption.kt @@ -33,6 +33,7 @@ annotation class GradleOption( enum class DefaultValue { BOOLEAN_FALSE_DEFAULT, BOOLEAN_TRUE_DEFAULT, + BOOLEAN_NULL_DEFAULT, STRING_NULL_DEFAULT, EMPTY_STRING_LIST_DEFAULT, EMPTY_STRING_ARRAY_DEFAULT, diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt index affa21e59df..98fc3bd8331 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt @@ -166,7 +166,7 @@ class K2JSCompilerArguments : CommonCompilerArguments() { gradleInputType = GradleInputTypes.INPUT, shouldGenerateDeprecatedKotlinOptions = true, ) - @Argument(value = "-target", valueDescription = "{ v5 }", description = "Generate JS files for the specified ECMA version.") + @Argument(value = "-target", valueDescription = "{ es5, es2015 }", description = "Generate JS files for the specified ECMA version.") var target: String? = null set(value) { checkFrozen() @@ -192,12 +192,12 @@ class K2JSCompilerArguments : CommonCompilerArguments() { @Argument( value = "-module-kind", valueDescription = "{plain|amd|commonjs|umd|es}", - description = "The kind of JS module generated by the compiler." + description = "The kind of JS module generated by the compiler. ES modules are enabled by default in case of ES2015 target usage" ) - var moduleKind: String? = MODULE_PLAIN + var moduleKind: String? = null set(value) { checkFrozen() - field = if (value.isNullOrEmpty()) MODULE_PLAIN else value + field = value } @GradleOption( @@ -460,15 +460,15 @@ In combination with '-meta-info', this generates both IR and pre-IR versions of } @GradleOption( - value = DefaultValue.BOOLEAN_FALSE_DEFAULT, + value = DefaultValue.BOOLEAN_NULL_DEFAULT, gradleInputType = GradleInputTypes.INPUT, shouldGenerateDeprecatedKotlinOptions = true, ) @Argument( value = "-Xes-classes", - description = "Let generated JavaScript code use ES2015 classes." + description = "Let generated JavaScript code use ES2015 classes. Enabled by default in case of ES2015 target usage" ) - var useEsClasses = false + var useEsClasses: Boolean? = null set(value) { checkFrozen() field = value @@ -486,9 +486,9 @@ In combination with '-meta-info', this generates both IR and pre-IR versions of @Argument( value = "-Xes-generators", - description = "Enable ES2015 generator functions usage inside the compiled code" + description = "Enable ES2015 generator functions usage inside the compiled code. Enabled by default in case of ES2015 target usage" ) - var useEsGenerators = false + var useEsGenerators: Boolean? = null set(value) { checkFrozen() field = value @@ -657,7 +657,7 @@ In combination with '-meta-info', this generates both IR and pre-IR versions of collector.deprecationWarn(irBaseClassInMetadata, false, "-Xir-base-class-in-metadata") collector.deprecationWarn(irNewIr2Js, true, "-Xir-new-ir2js") - if (irPerFile && moduleKind != MODULE_ES) { + if (irPerFile && (moduleKind != MODULE_ES && target != ES_2015)) { collector.report( CompilerMessageSeverity.ERROR, "Per-file compilation can't be used with any `moduleKind` except `es` (ECMAScript Modules)" diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JsArgumentConstants.java b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JsArgumentConstants.java index 770d438a5f8..5b3af749f76 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JsArgumentConstants.java +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JsArgumentConstants.java @@ -26,6 +26,8 @@ public interface K2JsArgumentConstants { String MODULE_UMD = "umd"; String MODULE_ES = "es"; + String ES_2015 = "es2015"; + String GRANULARITY_WHOLE_PROGRAM = "whole-program"; String GRANULARITY_PER_MODULE = "per-module"; String GRANULARITY_PER_FILE = "per-file"; diff --git a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt index c16dd8a1c30..8c391ffd703 100644 --- a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt +++ b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt @@ -145,6 +145,15 @@ class K2JsIrCompiler : CLICompiler() { } + private val K2JSCompilerArguments.targetVersion: EcmaVersion? + get() { + val targetString = target + return when { + targetString != null -> EcmaVersion.entries.firstOrNull { it.name == targetString } + else -> EcmaVersion.defaultVersion() + } + } + override fun doExecute( arguments: K2JSCompilerArguments, configuration: CompilerConfiguration, @@ -153,6 +162,15 @@ class K2JsIrCompiler : CLICompiler() { ): ExitCode { val messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) + val targetVersion = arguments.targetVersion?.also { + configuration.put(JSConfigurationKeys.TARGET, it) + } + + if (targetVersion == null) { + messageCollector.report(ERROR, "Unsupported ECMA version: ${arguments.target}") + return COMPILATION_ERROR + } + val pluginLoadResult = loadPlugins(paths, arguments, configuration) if (pluginLoadResult != OK) return pluginLoadResult @@ -182,7 +200,6 @@ class K2JsIrCompiler : CLICompiler() { configuration.put(JSConfigurationKeys.WASM_USE_TRAPS_INSTEAD_OF_EXCEPTIONS, arguments.wasmUseTrapsInsteadOfExceptions) configuration.putIfNotNull(JSConfigurationKeys.WASM_TARGET, arguments.wasmTarget?.let(WasmTarget::fromName)) - configuration.put(JSConfigurationKeys.USE_ES6_CLASSES, arguments.useEsClasses) configuration.put(JSConfigurationKeys.OPTIMIZE_GENERATED_JS, arguments.optimizeGeneratedJs) val commonSourcesArray = arguments.commonSources @@ -206,14 +223,21 @@ class K2JsIrCompiler : CLICompiler() { val projectJs = environmentForJS.project val configurationJs = environmentForJS.configuration val sourcesFiles = environmentForJS.getSourceFiles() + val isES2015 = targetVersion == EcmaVersion.es2015 + val moduleKind = configuration[JSConfigurationKeys.MODULE_KIND] + ?: moduleKindMap[arguments.moduleKind] + ?: ModuleKind.ES.takeIf { isES2015 } + ?: ModuleKind.UMD + configurationJs.put(JSConfigurationKeys.MODULE_KIND, moduleKind) configurationJs.put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage) configurationJs.put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.renderInternalDiagnosticNames) configurationJs.put(JSConfigurationKeys.PROPERTY_LAZY_INITIALIZATION, arguments.irPropertyLazyInitialization) configurationJs.put(JSConfigurationKeys.GENERATE_POLYFILLS, arguments.generatePolyfills) configurationJs.put(JSConfigurationKeys.GENERATE_DTS, arguments.generateDts) - configurationJs.put(JSConfigurationKeys.COMPILE_SUSPEND_AS_JS_GENERATOR, arguments.useEsGenerators) configurationJs.put(JSConfigurationKeys.GENERATE_INLINE_ANONYMOUS_FUNCTIONS, arguments.irGenerateInlineAnonymousFunctions) + configurationJs.put(JSConfigurationKeys.USE_ES6_CLASSES, arguments.useEsClasses ?: isES2015) + configurationJs.put(JSConfigurationKeys.COMPILE_SUSPEND_AS_JS_GENERATOR, arguments.useEsGenerators ?: isES2015) arguments.platformArgumentsProviderJsExpression?.let { configurationJs.put(JSConfigurationKeys.DEFINE_PLATFORM_MAIN_FUNCTION_ARGUMENTS, it) @@ -294,8 +318,6 @@ class K2JsIrCompiler : CLICompiler() { } if (arguments.irProduceJs) { - val moduleKind = configurationJs[JSConfigurationKeys.MODULE_KIND] ?: error("cannot get 'module kind' from configuration") - messageCollector.report(INFO, "Produce executable: $outputDirPath") messageCollector.report(INFO, "Cache directory: ${arguments.cacheDirectory}") @@ -686,11 +708,6 @@ class K2JsIrCompiler : CLICompiler() { ) { val messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) - if (arguments.target != null) { - assert("v5" == arguments.target) { "Unsupported ECMA version: " + arguments.target!! } - } - configuration.put(JSConfigurationKeys.TARGET, EcmaVersion.defaultVersion()) - if (arguments.sourceMap) { configuration.put(JSConfigurationKeys.SOURCE_MAP, true) if (arguments.sourceMapPrefix != null) { @@ -733,19 +750,10 @@ class K2JsIrCompiler : CLICompiler() { configuration.put(JSConfigurationKeys.FRIEND_PATHS, friendPaths) } - val moduleKindName = arguments.moduleKind - var moduleKind: ModuleKind? = if (moduleKindName != null) moduleKindMap[moduleKindName] else ModuleKind.PLAIN - if (moduleKind == null) { - messageCollector.report( - ERROR, "Unknown module kind: $moduleKindName. Valid values are: plain, amd, commonjs, umd, es", null - ) - moduleKind = ModuleKind.PLAIN - } if (arguments.wasm) { // K/Wasm support ES modules only. - moduleKind = ModuleKind.ES + configuration.put(JSConfigurationKeys.MODULE_KIND, ModuleKind.ES) } - configuration.put(JSConfigurationKeys.MODULE_KIND, moduleKind) configuration.putIfNotNull(JSConfigurationKeys.INCREMENTAL_DATA_PROVIDER, services[IncrementalDataProvider::class.java]) configuration.putIfNotNull(JSConfigurationKeys.INCREMENTAL_RESULTS_CONSUMER, services[IncrementalResultsConsumer::class.java]) diff --git a/compiler/testData/cli/js/jsExtraHelp.out b/compiler/testData/cli/js/jsExtraHelp.out index 6ff22440cbd..38a9919aa49 100644 --- a/compiler/testData/cli/js/jsExtraHelp.out +++ b/compiler/testData/cli/js/jsExtraHelp.out @@ -50,8 +50,8 @@ where advanced options include: JS expression that will be executed in runtime and be put as an Array parameter of the main function -Xstrict-implicit-export-types Generate strict types for implicitly exported entities inside d.ts files. This is available in the IR backend only. -Xtyped-arrays Translate primitive arrays into JS typed arrays. - -Xes-classes Let generated JavaScript code use ES2015 classes. - -Xes-generators Enable ES2015 generator functions usage inside the compiled code + -Xes-classes Let generated JavaScript code use ES2015 classes. Enabled by default in case of ES2015 target usage + -Xes-generators Enable ES2015 generator functions usage inside the compiled code. Enabled by default in case of ES2015 target usage -Xwasm Use the experimental WebAssembly compiler backend. -Xwasm-debug-info Add debug info to the compiled WebAssembly module. -Xwasm-enable-array-range-checks diff --git a/compiler/testData/cli/js/jsHelp.out b/compiler/testData/cli/js/jsHelp.out index 6c3c63d9c43..7d0c02b8db9 100644 --- a/compiler/testData/cli/js/jsHelp.out +++ b/compiler/testData/cli/js/jsHelp.out @@ -4,7 +4,7 @@ where possible options include: -main {call|noCall} Specify whether the 'main' function should be called upon execution. -meta-info Generate .meta.js and .kjsm files with metadata. Use this to create a library. -module-kind {plain|amd|commonjs|umd|es} - The kind of JS module generated by the compiler. + The kind of JS module generated by the compiler. ES modules are enabled by default in case of ES2015 target usage -ir-output-name Base name of generated files. -no-stdlib Don't automatically include the default Kotlin/JS stdlib in compilation dependencies. -ir-output-dir Destination for generated files. @@ -16,7 +16,7 @@ where possible options include: -source-map-names-policy {no|simple-names|fully-qualified-names} Mode for mapping generated names to original names (IR backend only). -source-map-prefix Add the specified prefix to the paths in the source map. - -target { v5 } Generate JS files for the specified ECMA version. + -target { es5, es2015 } Generate JS files for the specified ECMA version. -Werror Report an error if there are any warnings. -api-version Allow using declarations from only the specified version of bundled libraries. -X Print a synopsis of advanced options. diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt index c8b75ba697f..d123539f0da 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt @@ -217,7 +217,7 @@ class JsEnvironmentConfigurator(testServices: TestServices) : EnvironmentConfigu configuration.put(JSConfigurationKeys.FRIEND_PATHS, friends) configuration.put(CommonConfigurationKeys.MODULE_NAME, module.name.removeSuffix(OLD_MODULE_SUFFIX)) - configuration.put(JSConfigurationKeys.TARGET, EcmaVersion.v5) + configuration.put(JSConfigurationKeys.TARGET, EcmaVersion.es5) val errorIgnorancePolicy = registeredDirectives[ERROR_POLICY].singleOrNull() ?: ErrorTolerancePolicy.DEFAULT configuration.put(JSConfigurationKeys.ERROR_TOLERANCE_POLICY, errorIgnorancePolicy) diff --git a/generators/tests/org/jetbrains/kotlin/generators/arguments/DefaultValues.kt b/generators/tests/org/jetbrains/kotlin/generators/arguments/DefaultValues.kt index 961ff1aec92..b7904602454 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/arguments/DefaultValues.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/arguments/DefaultValues.kt @@ -35,6 +35,8 @@ open class DefaultValues( object BooleanTrueDefault : DefaultBoolean(true) + object BooleanNullDefault : DefaultValues("null", typeOf(), typeOf()) + object StringNullDefault : DefaultValues("null", typeOf(), typeOf()) object EmptyStringListDefault : DefaultValues("emptyList()", typeOf>(), typeOf>()) @@ -91,22 +93,22 @@ open class DefaultValues( ) object JsEcmaVersions : DefaultValues( - "\"v5\"", + "\"es5\"", typeOf(), typeOf(), - possibleValues = listOf("\"v5\"") + possibleValues = listOf("\"es5\"", "\"es2015\"") ) object JsModuleKinds : DefaultValues( - "${typeOf()}.${JsModuleKind.MODULE_PLAIN.name}", - typeOf(), - typeOf(), + "null", + typeOf(), + typeOf(), possibleValues = listOf("\"plain\"", "\"amd\"", "\"commonjs\"", "\"umd\""), fromKotlinOptionConverterProp = """ - ${typeOf()}.fromKind(this) + this?.let { ${typeOf()}.fromKind(it) } """.trimIndent(), toKotlinOptionConverterProp = """ - this.kind + this?.kind """.trimIndent() ) diff --git a/generators/tests/org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt b/generators/tests/org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt index cd2c4512280..ecacbd69468 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt @@ -952,6 +952,7 @@ private val KProperty1<*, *>.gradleValues: DefaultValues when (this) { DefaultValue.BOOLEAN_FALSE_DEFAULT -> DefaultValues.BooleanFalseDefault DefaultValue.BOOLEAN_TRUE_DEFAULT -> DefaultValues.BooleanTrueDefault + DefaultValue.BOOLEAN_NULL_DEFAULT -> DefaultValues.BooleanNullDefault DefaultValue.STRING_NULL_DEFAULT -> DefaultValues.StringNullDefault DefaultValue.EMPTY_STRING_LIST_DEFAULT -> DefaultValues.EmptyStringListDefault DefaultValue.EMPTY_STRING_ARRAY_DEFAULT -> DefaultValues.EmptyStringArrayDefault diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/config/EcmaVersion.java b/js/js.frontend/src/org/jetbrains/kotlin/js/config/EcmaVersion.java index 9739df54bec..fcc6cd5c988 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/config/EcmaVersion.java +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/config/EcmaVersion.java @@ -19,10 +19,10 @@ package org.jetbrains.kotlin.js.config; import org.jetbrains.annotations.NotNull; public enum EcmaVersion { - v3, v5; + es5, es2015; @NotNull public static EcmaVersion defaultVersion() { - return v5; + return es5; } } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/testOld/utils/JsTestUtils.java b/js/js.tests/test/org/jetbrains/kotlin/js/testOld/utils/JsTestUtils.java index 3def87c4551..1ce7ceef30d 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/testOld/utils/JsTestUtils.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/testOld/utils/JsTestUtils.java @@ -36,7 +36,7 @@ public final class JsTestUtils { @NotNull public static EnumSet successOnEcmaV5() { - return EnumSet.of(EcmaVersion.v5); + return EnumSet.of(EcmaVersion.es5); } @NotNull diff --git a/libraries/tools/kotlin-gradle-plugin-api/api/kotlin-gradle-plugin-api.api b/libraries/tools/kotlin-gradle-plugin-api/api/kotlin-gradle-plugin-api.api index bd898fa19b7..0de71159ee3 100644 --- a/libraries/tools/kotlin-gradle-plugin-api/api/kotlin-gradle-plugin-api.api +++ b/libraries/tools/kotlin-gradle-plugin-api/api/kotlin-gradle-plugin-api.api @@ -336,7 +336,7 @@ public abstract interface class org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions public abstract fun getSourceMapPrefix ()Ljava/lang/String; public abstract fun getTarget ()Ljava/lang/String; public abstract fun getTypedArrays ()Z - public abstract fun getUseEsClasses ()Z + public abstract fun getUseEsClasses ()Ljava/lang/Boolean; public abstract fun setFriendModulesDisabled (Z)V public abstract fun setMain (Ljava/lang/String;)V public abstract fun setMetaInfo (Z)V @@ -349,7 +349,7 @@ public abstract interface class org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions public abstract fun setSourceMapPrefix (Ljava/lang/String;)V public abstract fun setTarget (Ljava/lang/String;)V public abstract fun setTypedArrays (Z)V - public abstract fun setUseEsClasses (Z)V + public abstract fun setUseEsClasses (Ljava/lang/Boolean;)V } public final class org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions$DefaultImpls { @@ -370,7 +370,7 @@ public final class org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions$DefaultImpls public static fun getSuppressWarnings (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;)Z public static fun getTarget (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;)Ljava/lang/String; public static fun getTypedArrays (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;)Z - public static fun getUseEsClasses (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;)Z + public static fun getUseEsClasses (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;)Ljava/lang/Boolean; public static fun getUseK2 (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;)Z public static fun getVerbose (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;)Z public static fun setAllWarningsAsErrors (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;Z)V @@ -390,7 +390,7 @@ public final class org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions$DefaultImpls public static fun setSuppressWarnings (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;Z)V public static fun setTarget (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;Ljava/lang/String;)V public static fun setTypedArrays (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;Z)V - public static fun setUseEsClasses (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;Z)V + public static fun setUseEsClasses (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;Ljava/lang/Boolean;)V public static fun setUseK2 (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;Z)V public static fun setVerbose (Lorg/jetbrains/kotlin/gradle/dsl/KotlinJsOptions;Z)V } diff --git a/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptions.kt b/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptions.kt index 9a5b6085d22..1bac7b958f4 100644 --- a/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptions.kt +++ b/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptions.kt @@ -38,12 +38,13 @@ interface KotlinJsCompilerOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommon val metaInfo: org.gradle.api.provider.Property /** - * The kind of JS module generated by the compiler. + * The kind of JS module generated by the compiler. ES modules are enabled by default in case of ES2015 target usage * * Possible values: "plain", "amd", "commonjs", "umd" * - * Default value: JsModuleKind.MODULE_PLAIN + * Default value: null */ + @get:org.gradle.api.tasks.Optional @get:org.gradle.api.tasks.Input val moduleKind: org.gradle.api.provider.Property @@ -107,9 +108,9 @@ interface KotlinJsCompilerOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommon /** * Generate JS files for the specified ECMA version. * - * Possible values: "v5" + * Possible values: "es5", "es2015" * - * Default value: "v5" + * Default value: "es5" */ @get:org.gradle.api.tasks.Input val target: org.gradle.api.provider.Property @@ -123,10 +124,11 @@ interface KotlinJsCompilerOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommon val typedArrays: org.gradle.api.provider.Property /** - * Let generated JavaScript code use ES2015 classes. + * Let generated JavaScript code use ES2015 classes. Enabled by default in case of ES2015 target usage * - * Default value: false + * Default value: null */ + @get:org.gradle.api.tasks.Optional @get:org.gradle.api.tasks.Input val useEsClasses: org.gradle.api.provider.Property } diff --git a/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions.kt b/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions.kt index 63bf2338998..dd59d7ace53 100644 --- a/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions.kt +++ b/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions.kt @@ -51,19 +51,19 @@ interface KotlinJsOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions get() = options.metaInfo.get() set(value) = options.metaInfo.set(value) - private val kotlin.String.moduleKindCompilerOption get() = org.jetbrains.kotlin.gradle.dsl.JsModuleKind.fromKind(this) + private val kotlin.String?.moduleKindCompilerOption get() = this?.let { org.jetbrains.kotlin.gradle.dsl.JsModuleKind.fromKind(it) } - private val org.jetbrains.kotlin.gradle.dsl.JsModuleKind.moduleKindKotlinOption get() = this.kind + private val org.jetbrains.kotlin.gradle.dsl.JsModuleKind?.moduleKindKotlinOption get() = this?.kind /** - * The kind of JS module generated by the compiler. + * The kind of JS module generated by the compiler. ES modules are enabled by default in case of ES2015 target usage * * Possible values: "plain", "amd", "commonjs", "umd" * - * Default value: JsModuleKind.MODULE_PLAIN + * Default value: null */ - var moduleKind: kotlin.String - get() = options.moduleKind.get().moduleKindKotlinOption + var moduleKind: kotlin.String? + get() = options.moduleKind.orNull.moduleKindKotlinOption set(value) = options.moduleKind.set(value.moduleKindCompilerOption) /** @@ -136,9 +136,9 @@ interface KotlinJsOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions /** * Generate JS files for the specified ECMA version. * - * Possible values: "v5" + * Possible values: "es5", "es2015" * - * Default value: "v5" + * Default value: "es5" */ var target: kotlin.String get() = options.target.get() @@ -154,11 +154,11 @@ interface KotlinJsOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions set(value) = options.typedArrays.set(value) /** - * Let generated JavaScript code use ES2015 classes. + * Let generated JavaScript code use ES2015 classes. Enabled by default in case of ES2015 target usage * - * Default value: false + * Default value: null */ - var useEsClasses: kotlin.Boolean - get() = options.useEsClasses.get() + var useEsClasses: kotlin.Boolean? + get() = options.useEsClasses.orNull set(value) = options.useEsClasses.set(value) } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptionsDefault.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptionsDefault.kt index d6c765053df..03c6929aa1b 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptionsDefault.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptionsDefault.kt @@ -20,7 +20,7 @@ internal abstract class KotlinJsCompilerOptionsDefault @javax.inject.Inject cons objectFactory.property(kotlin.Boolean::class.java).convention(true) override val moduleKind: org.gradle.api.provider.Property = - objectFactory.property(org.jetbrains.kotlin.gradle.dsl.JsModuleKind::class.java).convention(org.jetbrains.kotlin.gradle.dsl.JsModuleKind.MODULE_PLAIN) + objectFactory.property(org.jetbrains.kotlin.gradle.dsl.JsModuleKind::class.java) override val moduleName: org.gradle.api.provider.Property = objectFactory.property(kotlin.String::class.java) @@ -42,11 +42,11 @@ internal abstract class KotlinJsCompilerOptionsDefault @javax.inject.Inject cons objectFactory.property(kotlin.String::class.java) override val target: org.gradle.api.provider.Property = - objectFactory.property(kotlin.String::class.java).convention("v5") + objectFactory.property(kotlin.String::class.java).convention("es5") override val typedArrays: org.gradle.api.provider.Property = objectFactory.property(kotlin.Boolean::class.java).convention(true) override val useEsClasses: org.gradle.api.provider.Property = - objectFactory.property(kotlin.Boolean::class.java).convention(false) + objectFactory.property(kotlin.Boolean::class.java) } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptionsHelper.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptionsHelper.kt index 789196c9c39..67b570569ff 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptionsHelper.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsCompilerOptionsHelper.kt @@ -15,7 +15,7 @@ internal object KotlinJsCompilerOptionsHelper { args.friendModulesDisabled = from.friendModulesDisabled.get() args.main = from.main.get().mode args.metaInfo = from.metaInfo.get() - args.moduleKind = from.moduleKind.get().kind + args.moduleKind = from.moduleKind.orNull?.kind args.moduleName = from.moduleName.orNull args.noStdlib = from.noStdlib.get() args.sourceMap = from.sourceMap.get() @@ -24,7 +24,7 @@ internal object KotlinJsCompilerOptionsHelper { args.sourceMapPrefix = from.sourceMapPrefix.orNull args.target = from.target.get() args.typedArrays = from.typedArrays.get() - args.useEsClasses = from.useEsClasses.get() + args.useEsClasses = from.useEsClasses.orNull } internal fun syncOptionsAsConvention( diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/KotlinJsCompilation.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/KotlinJsCompilation.kt index dee3ad131ae..b5d2ba0c3af 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/KotlinJsCompilation.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/KotlinJsCompilation.kt @@ -3,7 +3,7 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -@file:Suppress("PackageDirectoryMismatch") +@file:Suppress("PackageDirectoryMismatch", "DEPRECATION", "TYPEALIAS_EXPANSION_DEPRECATION") // Old package for compatibility package org.jetbrains.kotlin.gradle.plugin.mpp @@ -13,8 +13,12 @@ import org.gradle.api.Action import org.gradle.api.attributes.AttributeContainer import org.gradle.api.provider.Provider import org.gradle.api.tasks.TaskProvider -import org.jetbrains.kotlin.gradle.dsl.* -import org.jetbrains.kotlin.gradle.plugin.* +import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants +import org.jetbrains.kotlin.gradle.dsl.JsModuleKind +import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompilerOptions +import org.jetbrains.kotlin.gradle.dsl.KotlinJsOptions +import org.jetbrains.kotlin.gradle.plugin.DeprecatedHasCompilerOptions +import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType import org.jetbrains.kotlin.gradle.plugin.mpp.compilationImpl.KotlinCompilationImpl import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsSubTargetContainerDsl import org.jetbrains.kotlin.gradle.targets.js.ir.JsBinary @@ -23,7 +27,6 @@ import org.jetbrains.kotlin.gradle.targets.js.npm.PackageJson import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile import javax.inject.Inject -@Suppress("TYPEALIAS_EXPANSION_DEPRECATION", "DEPRECATION") open class KotlinJsCompilation @Inject internal constructor( compilation: KotlinCompilationImpl, ) : DeprecatedAbstractKotlinCompilationToRunnableFiles(compilation), @@ -102,11 +105,19 @@ val KotlinJsCompilation.fileExtension: Provider get() { val isWasm = platformType == KotlinPlatformType.wasm @Suppress("DEPRECATION") - return compilerOptions.options.moduleKind.map { moduleKind -> - if (isWasm || moduleKind == JsModuleKind.MODULE_ES) { - "mjs" - } else { - "js" + return compilerOptions.options.moduleKind + .orElse( + compilerOptions.options.target.map { + if (it == K2JsArgumentConstants.ES_2015) { + JsModuleKind.MODULE_ES + } else JsModuleKind.MODULE_UMD + } + ) + .map { moduleKind -> + if (isWasm || moduleKind == JsModuleKind.MODULE_ES) { + "mjs" + } else { + "js" + } } - } } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinBrowserJsIr.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinBrowserJsIr.kt index 857efc5e5ed..25f2b4f3847 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinBrowserJsIr.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinBrowserJsIr.kt @@ -13,6 +13,7 @@ import org.gradle.api.provider.Property import org.gradle.api.provider.Provider import org.gradle.api.tasks.Copy import org.gradle.language.base.plugins.LifecycleBasePlugin +import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants.ES_2015 import org.jetbrains.kotlin.gradle.dsl.JsModuleKind import org.jetbrains.kotlin.gradle.dsl.KotlinJsDce import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType @@ -34,6 +35,7 @@ import org.jetbrains.kotlin.gradle.utils.doNotTrackStateCompat import org.jetbrains.kotlin.gradle.utils.domainObjectSet import org.jetbrains.kotlin.gradle.utils.relativeOrAbsolute import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly +import org.jetbrains.kotlin.utils.addToStdlib.runIf import javax.inject.Inject abstract class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) : @@ -274,7 +276,11 @@ abstract class KotlinBrowserJsIr @Inject constructor(target: KotlinJsIrTarget) : this.inputFilesDirectory.set(inputFilesDirectory) val platformType = binary.compilation.platformType - val moduleKind = binary.linkTask.flatMap { it.compilerOptions.moduleKind } + val moduleKind = binary.linkTask.flatMap { task -> + task.compilerOptions.moduleKind.orElse(task.compilerOptions.target.map { + if (it == ES_2015) JsModuleKind.MODULE_ES else JsModuleKind.MODULE_UMD + }) + } this.entryModuleName.set(entryModuleName) this.esModules.convention( diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrTargetConfigurator.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrTargetConfigurator.kt index f32f6c2ac68..4f44ed7cb2b 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrTargetConfigurator.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrTargetConfigurator.kt @@ -5,7 +5,6 @@ package org.jetbrains.kotlin.gradle.targets.js.ir -import org.jetbrains.kotlin.gradle.dsl.JsModuleKind import org.jetbrains.kotlin.gradle.dsl.JsSourceMapEmbedMode import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompilerOptions import org.jetbrains.kotlin.gradle.plugin.KotlinOnlyTargetConfigurator @@ -15,7 +14,6 @@ open class KotlinJsIrTargetConfigurator : internal companion object { internal fun KotlinJsCompilerOptions.configureJsDefaultOptions() { - moduleKind.convention(JsModuleKind.MODULE_UMD) sourceMap.convention(true) sourceMapEmbedSources.convention(JsSourceMapEmbedMode.SOURCE_MAP_SOURCE_CONTENT_NEVER) }