The Kotlin/JS Gradle plugin includes a dead code elimination (DCE) tool. Dead code elimination is often also called tree shaking. It reduces the size or the resulting JavaScript code by removing unused properties, functions, and classes.
Unused declarations can appear in cases like:
The Kotlin/JS Gradle plugin handles DCE automatically when you build a production bundle, for example by using the browserProductionWebpack
task. Development bundling tasks (like browserDevelopmentWebpack
) don't include DCE.
Sometimes you may need to keep a function or a class in the resulting JavaScript code even if you don't use it in your module, for example, if you're going to use it in the client JavaScript code.
To keep certain declarations from elimination, add the dceTask
block to your Gradle build script and list the declarations as arguments of the keep
function. An argument must be the declaration's fully qualified name with the module name as a prefix: moduleName.dot.separated.package.name.declarationName
Unless specified otherwise, the names of functions and modules can be mangled in the generated JavaScript code. To keep such functions from elimination, use the mangled names in the
keep
arguments as they appear in the generated JavaScript code.
kotlin { js { browser { dceTask { keep("myKotlinJSModule.org.example.getName", "myKotlinJSModule.org.example.User" ) } binaries.executable() } } }
If you want to keep a whole package or module from elimination, you can use its fully qualified name as it appears in the generated JavaScript code.
Keeping whole packages or modules from elimination can prevent DCE from removing many unused declarations. Because of this, it is preferable to select individual declarations which should be excluded from DCE one by one.
To turn off DCE completely, use the devMode
option in the dceTask
:
kotlin { js { browser { dceTask { dceOptions.devMode = true } } binaries.executable() } }
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/reference/javascript-dce.html