diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSDceArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSDceArguments.kt index 3bb2131f413..1901f04d690 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSDceArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSDceArguments.kt @@ -26,6 +26,13 @@ class K2JSDceArguments : CommonToolArguments() { @JvmField var outputDirectory: String? = null + @field:Argument( + value = "-keep", + valueDescription = "", + description = "List of fully-qualified names of declarations that shouldn't be eliminated") + @JvmField + var declarationsToKeep: Array? = null + @field:GradleOption(DefaultValues.BooleanFalseDefault::class) @field:Argument(value = "-Xprint-reachability-info", description = "Print declarations marked as reachable") @JvmField diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/js/dce/K2JSDce.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/js/dce/K2JSDce.kt index 68b70da2070..6a1f1b3938b 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/js/dce/K2JSDce.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/js/dce/K2JSDce.kt @@ -49,7 +49,9 @@ class K2JSDce : CLITool() { return ExitCode.COMPILATION_ERROR } - val dceResult = DeadCodeElimination.run(files, emptySet()) { + val includedDeclarations = arguments.declarationsToKeep.orEmpty().toSet() + + val dceResult = DeadCodeElimination.run(files, includedDeclarations) { messageCollector.report(CompilerMessageSeverity.LOGGING, it) } val nodes = dceResult.reachableNodes diff --git a/compiler/testData/cli/js-dce/dceHelp.out b/compiler/testData/cli/js-dce/dceHelp.out index 8c4328da1ca..90733142797 100644 --- a/compiler/testData/cli/js-dce/dceHelp.out +++ b/compiler/testData/cli/js-dce/dceHelp.out @@ -1,6 +1,8 @@ Usage: kotlin-dce-js where possible options include: -output-dir Output directory + -keep + List of fully-qualified names of declarations that shouldn't be eliminated -help (-h) Print a synopsis of standard options -X Print a synopsis of advanced options -version Display compiler version diff --git a/compiler/testData/cli/js-dce/include.js b/compiler/testData/cli/js-dce/include.js new file mode 100644 index 00000000000..16a12fdbaae --- /dev/null +++ b/compiler/testData/cli/js-dce/include.js @@ -0,0 +1,20 @@ +define(["exports"], function(exports) { + function foo() { + } + + function bar() { + } + + function baz() { + } + + function ignore() { + } + + baz(); + + exports.foo = foo; + exports.bar = bar; + exports.baz = baz; + exports.ignore = ignore; +}); \ No newline at end of file diff --git a/compiler/testData/cli/js-dce/includeDeclarations.args b/compiler/testData/cli/js-dce/includeDeclarations.args new file mode 100644 index 00000000000..2223d60fa53 --- /dev/null +++ b/compiler/testData/cli/js-dce/includeDeclarations.args @@ -0,0 +1,6 @@ +$TESTDATA_DIR$/include.js +-output-dir +$TEMP_DIR$/min +-keep +global.foo,global.bar +-Xprint-reachability-info \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index f4bb2576814..0c540634ba4 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -560,6 +560,12 @@ public class CliTestGenerated extends AbstractCliTest { doJsDceTest(fileName); } + @TestMetadata("includeDeclarations.args") + public void testIncludeDeclarations() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js-dce/includeDeclarations.args"); + doJsDceTest(fileName); + } + @TestMetadata("jsExtraHelp.args") public void testJsExtraHelp() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js-dce/jsExtraHelp.args");