Coder Social home page Coder Social logo

winterbe / kotlin-frontend-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kotlin/kotlin-frontend-plugin

0.0 3.0 2.0 236 KB

Gradle Kotlin (http://kotlinlang.org) plugin for frontend development

License: Apache License 2.0

Kotlin 98.26% JavaScript 1.74%

kotlin-frontend-plugin's Introduction

kotlin-frontend-plugin Download

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.

Howto

Configure Gradle project

Fist of all you have to apply plugin org.jetbrains.kotlin.frontend and setup Kotlin:

buildscript {
    ext.kotlin_version = '1.1.1'

    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.17"
    }
}


// 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"
}

Setup npm dependencies

All frontend plugin settings are applied in kotlinFrontend section:

kotlinFrontend {
    npm {
        dependency "style-loader" // production dependency
        devDependency "karma"     // development dependency
    }
}

webpack bundler

To create a webpack bundle (for both packaging and running the dev server):

kotlinFrontend {
    webpackBundle {
        bundleName = "main"
    }
}

Complete example

See examples/frontend-only/build.gradle for a full example.

Building and running

To run dev server (that also will build kotlin sources):

gradlew run

To run tests:

  • run gradlew tests to build the tests and start the Karma daemon
  • open http://localhost:9876 to run the tests in your browser using Karma

To pack the bundle:

gradle bundle

To stop running webpack and Karma daemons:

gradle stop

webpack

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
        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

webpack configuration customization

To customize webpack coniguration, 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:

  • [DIR] webpack.config.d
    • css.js
    • minify.js
    • 10-apply-ealier.js
    • 20-apply-later.js

Karma

Karma configuration:

kotlinFrontend {
    karma {
        port = 9876
        runnerPort = 9100
        reporters = listOf("progress") 
        frameworks = listOf("qunit") // for now only qunit works as intended
        preprocessors = listOf("...")
    }
}

karma log is located at build/logs/karma.log

config file is generated at build/karma.conf.js

Hot module replacement

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

kotlin-frontend-plugin's People

Contributors

cy6ergn0m avatar orangy avatar yole avatar

Watchers

 avatar  avatar  avatar

Forkers

dalalsunil1986

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.