Kotlin/JS plugin contains features of kotlin-frontend-plugin
.
For setting up project with Kotlin/JS Gradle Plugin, please follow
https://kotlinlang.org/docs/reference/js-project-setup.html
Gradle plugin for Kotlin frontend development
The plugin provides an easy way to gather Maven and npm dependencies, pack bundles (via webpack) and test a frontend application using Karma. By default the plugin generates all required configs for webpack, karma and manages the corresponding daemons.
By using Gradle continuous build, you also can get hot module replacement feature (apply code changes in browser on the fly). See corresponding section below.
First of all you have to apply plugin org.jetbrains.kotlin.frontend
and setup Kotlin:
buildscript {
ext.kotlin_version = '1.3.21'
repositories {
jcenter()
maven {
url "https://dl.bintray.com/kotlin/kotlin-eap"
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.45"
}
}
// apply plugin
apply plugin: 'org.jetbrains.kotlin.frontend'
// apply kotlin2js
apply plugin: 'kotlin2js'
// configure kotlin compiler
compileKotlin2Js {
kotlinOptions.metaInfo = true
kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js"
kotlinOptions.sourceMap = true
kotlinOptions.moduleKind = 'commonjs'
kotlinOptions.main = "call"
}
All frontend plugin settings are applied in kotlinFrontend
section:
kotlinFrontend {
npm {
dependency "style-loader" // production dependency
devDependency "karma" // development dependency
}
}
To create a webpack bundle (for both packaging and running the dev server):
kotlinFrontend {
webpackBundle {
bundleName = "main"
}
}
See examples/frontend-only/build.gradle for a full example.
To run dev server (that also will build kotlin sources):
gradlew run
To run tests:
gradlew tests
to build the tests and start the Karma daemonhttp://localhost:9876
to run the tests in your browser using KarmaTo pack the bundle:
gradle bundle
To stop running webpack and Karma daemons:
gradle stop
webpack configuration:
kotlinFrontend {
webpackBundle {
bundleName = "main"
sourceMapEnabled = true | false // enable/disable source maps
contentPath = file(...) // a file that represents a directory to be served by dev server)
publicPath = "/" // web prefix
host = "localhost" // dev server host
port = 8088 // dev server port
proxyUrl = "" | "http://...." // URL to be proxied, useful to proxy backend webserver
stats = "errors-only" // log level
}
}
dev server log is located at build/logs/webpack-dev-server.log
config file is generated at build/webpack.config.js
To customize webpack configuration, you can apply additional scripts by placing them in the directory webpack.config.d
. The scripts will be appended to the end of config script. Use number prefix to change order (it is very similar to UNIX rc.d config directories)
Sample structure:
Karma configuration:
kotlinFrontend {
karma {
port = 9876
runnerPort = 9100
reporters = listOf("progress")
frameworks = listOf("qunit") // for now only qunit works as intended
preprocessors = listOf("...")
}
}
This will generate a config file located at build/karma.conf.js
.
Note that for your tests to run correctly with webpack their module type must be defined as well:
compileTestKotlin2Js {
kotlinOptions.metaInfo = true
kotlinOptions.moduleKind = 'commonjs'
}
If you would like to use a custom karma.config.js
then specify it using customConfigFile
:
kotlinFrontend {
karma {
customConfigFile = "myKarma.conf.js"
}
}
Your custom config file will be copied to the build folder and renamed to karma.config.js
.
karma log is located at build/logs/karma.log
Webpack provides ability to apply code changes on the fly with no page reload (if possible). For reference see Webpack Hot Module Replacement documentation
Webpack does a lot of work for you however to get it working well most likely you have to implement state save and restore functionality via webpack's API. See HMR.kt for corresponding Kotlin external declarations for webpack API and main.kt for sample save/load.
Briefly at module load accept HMR feature and listen for disposal
module.hot?.let { hot ->
hot.accept() // accept hot reload
hot.dispose { data -> // listen for disposal events
data.my-fields = [your application state] // put your state in the 'data' object
}
}
To get previously saved state at module load use module.hot?.data
module.hot?.data?.let { data -> // if we have previous state then we are in the middle of HMR
myRestoreFunction(data) // so get state from the 'data' object
}
Finally use Gradle continuous build with run task to get live replacement every time you change your code.
gradlew -t run
by Adam Arold 亚当·阿罗德(Adam Arold) 如何超越Android并在前端使用Kotlin (How to go beyond Android and use Kotlin on the frontend) This article is part of a series. Check out the previous part here. 本文是系列文章的一部分。 在这里查
by Adam Arold 亚当·阿罗德(Adam Arold) 超越Android:探索Kotlin的应用领域 (Going beyond Android: exploring Kotlin’s areas of application) If you have written something in Kotlin, chances are that you wrote it for Andr
Ilios Frontend Web interface for accessing and managing Ilios Platform data. Open Source Heroes Every day these for-profit companies make developing Ilios possible. Without the free tier of service th
This page describes how to make changes to Flarum's user interface. How to add buttons, marquees, and blinking text. Remember, Flarum's frontend is a single-page JavaScript application. There's no Twi
基于 jQuery + bootstrap 的前端框架。 技术栈 express jade bootstrap bootstraptable jquery gulp nginx 前后端分离实践 前端:moa-frontend public下面的采用nginx做反向代理 其他的采用express+jade精简代码(ajax与后端交互) 后端:moa-api Install npm install n
puewue-frontend 是一个简单的前端应用用来显示 Facebook 数据中心电源消耗和其他信息的工具。与其配合的后端工具请看 puewue-backend
TipXMR - The Livestream Tipping Service for OBS TipXMR is a web based application that let’s you accept donations in your livestream. �� Our CCS Funding Campaign The Goal We want to provide an open so
FRONTEND-TUTORIAL 一个后端程序员的前端技术总结。 �� 项目同步维护:Github | Gitee �� 电子书阅读:Github Pages | Gitee Pages �� 内容 Html, Css, JavaScript 前端基础 - Html, Css, JavaScript Html 入门 Css 入门 JavaScript 入门 Nodejs Node.js 入门 N