A new 1.1.4 EAP provides a new cool feature for JavaScript: a dead code elimination (DCE) tool. This tool allows to strip out unused pieces of code from generated JS. When you are writing an application, you, hopefully, don’t write unused code. However, you might use libraries, which may be huge and of which you use only small parts. Examples are: kotlin.js (~1.3 mb), kotlinx-html-js.js (~550 kb). DCE tool is good at removing unused code from these libraries. Currently, DCE reduces size of kotlin.js file down to ~65 kb for a simple “Hello, World” application.
How to use
DCE tool is currently available from Gradle and as a command-line tool. We will describe how to use it from CLI later. For now, we are going to show how to use it from Gradle.
Simply add the following line to your build.gradle
:
apply plugin: 'kotlin-dce-js'
Note that if you are using multi-project build, you should apply plugin to the main project that is an entry point to your application.
For example as for 1.1.4 EAP, the build script may look like this:
group 'org.jetbrains.kotlin'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.4-eap-11'
repositories {
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap-1.1' }
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
apply plugin: 'kotlin-dce-js'
repositories {
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1' }
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}
By default, the resulting set of JavaScript files (your application together with all dependencies) can be found at build/classes/main/min/
.
Configuring
To configure DCE on the main source set, you can use the runDceKotlinJs
task (and corresponding runDce<sourceSetName>KotlinJs
for other source sets).
Sometimes you are going to use a Kotlin declaration directly from JavaScript, and it’s being stripped out by DCE. You may want to keep this declaration. To do so, you can use the following syntax in build.gradle
:
runDceKotlinJs.keep "declarationToKeep"[, "declarationToKeep", ...]
Where declarationToKeep
has the following syntax:
moduleName.dot.separated.package.name.declarationName
For example, if our module is named kotlin-js-example
and we have a function named toKeep
in package org.jetbrains.kotlin.examples
, we should use the following line:
runDceKotlinJs.keep "kotlin-js-example_main.org.jetbrains.kotlin.examples.toKeep"
Note that if your function has parameters, its name will be mangled, so the mangled name should be used in the keep directive.
Notes
- DCE tool is an experimental feature. This does not mean we are going to remove it, or that it’s unusable for production. This means that we can change names of configuration parameters, default settings, etc.
- Currently you should not use DCE tool if your project is a shared library. It’s only applicable when you are developing an application (which may use shared libraries). The reason is: DCE does not know which parts of the library are going to be used by the user’s application.
- DCE does not perform minification (uglification) of your code by removing unnecessary whitespaces and shortening identifiers. You should use existing tools, like UglifyJS (GitHub - mishoo/UglifyJS: JavaScript parser / mangler / compressor / beautifier toolkit) or Google Closure Compiler (Closure Compiler | Google Developers) for this purpose.
- Full documentation and examples will be available later.