Skip to content

Commit

Permalink
feat(builtin): add Kotlin example
Browse files Browse the repository at this point in the history
This uses the Kotlin JS output to build a web application
  • Loading branch information
alexeagle committed Sep 6, 2019
1 parent 38814aa commit 0912014
Show file tree
Hide file tree
Showing 18 changed files with 1,372 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ jobs:
# to build the release package with this configuration.
- run: bazel build --build_tag_filters=examples ...
- run:
command: bazel test --test_tag_filters=examples --local_resources=792,1.0,1.0 --test_arg=--local_resources=13288,7.0,1.0 --test_arg=--test_tag_filters=-no-circleci,-manual ...
# We don't have java installed on our circleci docker image
command: bazel test --test_tag_filters=examples,-requires-java --local_resources=792,1.0,1.0 --test_arg=--local_resources=13288,7.0,1.0 --test_arg=--test_tag_filters=-no-circleci,-manual ...
no_output_timeout: 20m
# The following examples only works on linux as it downloads the linux node distribution
# It is tagged "manual" so we run it explicitly here
Expand Down
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ k8s_defaults(
# Setup bazel_integration_test repositories
#

# Don't need to build kotlin in the root workspace.
# See tools/mock_rules_kotlin/README.md
local_repository(
name = "io_bazel_rules_kotlin",
path = "tools/mock_rules_kotlin",
)

load("//e2e:index.bzl", "ALL_E2E")

[local_repository(
Expand Down
15 changes: 15 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ example_integration_test(
},
)

example_integration_test(
name = "examples_kotlin",
npm_packages = {
"//packages/jasmine:npm_package": "@bazel/jasmine",
},
# Kotlin's kt_js_import relies on a helper written in Python
# https://github.com/bazelbuild/rules_kotlin/blob/master/kotlin/internal/js/importer.py
# but python isn't installed on BazelCI windows machines.
# Failure looks like
# [0 / 57] [Prepa] Expanding template external/io_bazel_rules_kotlin/kotlin/internal/js/importer.temp [for host]
# ERROR: D:/temp/tmp-67520fayqgbwmbgd/BUILD.bazel:10:1: Action kotlinx-html-js.js failed (Exit 2)
# LAUNCHER ERROR: Cannot launch process: "python.exe"
tags = ["no-bazelci-windows"],
)

example_integration_test(
name = "examples_web_testing",
npm_packages = {
Expand Down
1 change: 1 addition & 0 deletions examples/index.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"Used to reference the nested workspaces for examples in /WORKSPACE"
ALL_EXAMPLES = [
"app",
"kotlin",
"nestjs",
"parcel",
"protocol_buffers",
Expand Down
1 change: 1 addition & 0 deletions examples/kotlin/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import %workspace%/../../common.bazelrc
74 changes: 74 additions & 0 deletions examples/kotlin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Add rules here to build your software
# See https://docs.bazel.build/versions/master/build-ref.html#BUILD_files

load("@build_bazel_rules_nodejs//:defs.bzl", "http_server", "rollup_bundle")
load("@build_bazel_rules_nodejs//internal/web_package:web_package.bzl", "web_package")
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_js_import", "kt_js_library")
load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")

# Grab a Maven dependency
kt_js_import(
name = "kotlinx-html-js",
jars = ["@maven//:v1/http/dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12.jar"],
srcjar = "@maven//:v1/http/dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12-sources.jar",
)

# This will create hello.js
kt_js_library(
name = "hello",
srcs = [":HelloWorld.kt"],
deps = [":kotlinx-html-js"],
)

rollup_bundle(
name = "bundle",
srcs = ["hello.js"],
entry_point = "bootstrap.js",
deps = [
"@npm//kotlin",
"@npm//kotlinx-html-js",
],
)

web_package(
name = "package",
assets = [
# For differential loading, we supply both an ESModule entry point and an es5 entry point
# The injector will put two complimentary script tags in the index.html
":bundle.min.js",
":bundle.min.es2015.js",
],
data = [":bundle"],
index_html = "index.html",
)

http_server(
name = "server",
data = [":package"],
templated_args = ["package"],
)

jasmine_node_test(
name = "test",
srcs = ["spec.js"],
data = [
":bundle",
":bundle.js",
"@npm//domino",
],
templated_args = ["--node_options=--experimental-modules"],
)

# For testing from the root workspace of this repository with bazel_integration_test.
filegroup(
name = "all_files",
srcs = glob(
include = ["**/*"],
exclude = [
"bazel-out/**/*",
"dist/**/*",
"node_modules/**/*",
],
),
visibility = ["//visibility:public"],
)
11 changes: 11 additions & 0 deletions examples/kotlin/HelloWorld.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import kotlin.browser.*
import kotlinx.html.*
import kotlinx.html.dom.*

fun printHello() {
document.body!!.append.div {
span {
+"Hello from Kotlin!"
}
}
}
74 changes: 74 additions & 0 deletions examples/kotlin/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
workspace(
name = "kotlin_example",
managed_directories = {"@npm": ["node_modules"]},
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "da72ea53fa1cb8ab5ef7781ba06b97259b7d579a431ce480476266bc81bdf21d",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.36.2/rules_nodejs-0.36.2.tar.gz"],
)

# Install external npm dependencies
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_install")

npm_install(
# Name this npm so that Bazel Label references look like @npm//package
name = "npm",
package_json = "//:package.json",
package_lock_json = "//:package-lock.json",
)

# Install any Bazel rules which were extracted earlier by the yarn_install rule.
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")

install_bazel_dependencies()

# Install external Kotlin/Java dependencies
http_archive(
name = "rules_jvm_external",
sha256 = "f04b1466a00a2845106801e0c5cec96841f49ea4e7d1df88dc8e4bf31523df74",
strip_prefix = "rules_jvm_external-2.7",
url = "https://github.com/bazelbuild/rules_jvm_external/archive/2.7.zip",
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
name = "maven",
artifacts = [
"org.jetbrains.kotlinx:kotlinx-html-js:0.6.12",
"org.jetbrains.kotlin:kotlin-stdlib-js:1.2.71",
],
fetch_sources = True, # Fetch source jars. Defaults to False.
maven_install_json = "@kotlin_example//:maven_install.json",
repositories = [
"http://dl.bintray.com/kotlin/kotlinx.html/",
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
)

load("@maven//:defs.bzl", "pinned_maven_install")

pinned_maven_install()

# To test local edits, add this to .bazelrc.user:
# build --override_repository=io_bazel_rules_kotlin=/my/path/to/rules_kotlin
rules_kotlin_version = "40efd46789a06f06655e867dcaec2a84815461df"

http_archive(
name = "io_bazel_rules_kotlin",
#sha256 = "fea280c0c3fbb40712c5e7994c36c1a7ab0be5c8d26c90d47ac55860c5d43b2d",
strip_prefix = "rules_kotlin-%s" % rules_kotlin_version,
type = "zip",
urls = ["https://github.com/bazelbuild/rules_kotlin/archive/%s.zip" % rules_kotlin_version],
)

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")

kotlin_repositories()

kt_register_toolchains()
8 changes: 8 additions & 0 deletions examples/kotlin/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// hello.js is created by the kt_js_library rule
import('./hello.js')
// we dynamic load the Kotlin code because it brings in a Kotlin runtime dependency
// In a real app we'd want to avoid doing that until the user visits a part of the application that requires it
.then(m =>
// Kotlin's JS emit will create a default export object
// it has the function printHello that we defined in HelloWorld.kt
m.default.printHello());
8 changes: 8 additions & 0 deletions examples/kotlin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>rules_kotlin webapp example</title>
<link rel="icon" href="/favicon.png">
<!-- CSS will be linked here -->
</head>
<!-- JS scripts will be included here -->
</html>
143 changes: 143 additions & 0 deletions examples/kotlin/maven_install.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"dependency_tree": {
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": 1276653619,
"conflict_resolution": {},
"dependencies": [
{
"coord": "org.jetbrains.kotlin:kotlin-stdlib-common:1.2.71",
"dependencies": [],
"directDependencies": [],
"file": "v1/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71.jar",
"mirror_urls": [
"http://dl.bintray.com/kotlin/kotlinx.html//org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71.jar",
"https://maven.google.com/org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71.jar",
"https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71.jar"
],
"sha256": "63999687ff2fce8a592dd180ffbbf8f1d21c26b4044c55cdc74ff3cf3b3cf328",
"url": "https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71.jar"
},
{
"coord": "org.jetbrains.kotlin:kotlin-stdlib-common:jar:sources:1.2.71",
"dependencies": [],
"directDependencies": [],
"file": "v1/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71-sources.jar",
"mirror_urls": [
"http://dl.bintray.com/kotlin/kotlinx.html//org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71-sources.jar",
"https://maven.google.com/org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71-sources.jar",
"https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71-sources.jar"
],
"sha256": "e947bbdd1f6d3afd69ce01598327a9923b11b725f1bfc140340aca0fcfcf173a",
"url": "https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.2.71/kotlin-stdlib-common-1.2.71-sources.jar"
},
{
"coord": "org.jetbrains.kotlin:kotlin-stdlib-js:1.2.71",
"dependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-common:1.2.71"
],
"directDependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-common:1.2.71"
],
"file": "v1/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71.jar",
"mirror_urls": [
"http://dl.bintray.com/kotlin/kotlinx.html//org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71.jar",
"https://maven.google.com/org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71.jar",
"https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71.jar"
],
"sha256": "1c8b11025a9bbc22d27d9b8abf0d6f7c91a9e5460979ec37b686d3da0efcf79b",
"url": "https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71.jar"
},
{
"coord": "org.jetbrains.kotlin:kotlin-stdlib-js:jar:sources:1.2.71",
"dependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-common:jar:sources:1.2.71"
],
"directDependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-common:jar:sources:1.2.71"
],
"file": "v1/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71-sources.jar",
"mirror_urls": [
"http://dl.bintray.com/kotlin/kotlinx.html//org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71-sources.jar",
"https://maven.google.com/org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71-sources.jar",
"https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71-sources.jar"
],
"sha256": "5a126625080701aca06f5d5d675a37566afbc375cacdfd6968b54fe1f849dc5f",
"url": "https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-js/1.2.71/kotlin-stdlib-js-1.2.71-sources.jar"
},
{
"coord": "org.jetbrains.kotlinx:kotlinx-html-common:0.6.12",
"dependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-common:1.2.71"
],
"directDependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-common:1.2.71"
],
"file": "v1/http/dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12.jar",
"mirror_urls": [
"http://dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12.jar",
"https://maven.google.comorg/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12.jar",
"https://repo1.maven.org/maven2org/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12.jar"
],
"sha256": "ecf6d7595bdd762c4e835cd9196df7b014f51542d8266575343e8acbf0beb825",
"url": "http://dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12.jar"
},
{
"coord": "org.jetbrains.kotlinx:kotlinx-html-common:jar:sources:0.6.12",
"dependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-common:jar:sources:1.2.71"
],
"directDependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-common:jar:sources:1.2.71"
],
"file": "v1/http/dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12-sources.jar",
"mirror_urls": [
"http://dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12-sources.jar",
"https://maven.google.comorg/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12-sources.jar",
"https://repo1.maven.org/maven2org/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12-sources.jar"
],
"sha256": "1ee01561bd105933e282e024ced9220a520f8f42d7e80eada3bd522cc04fb35b",
"url": "http://dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-common/0.6.12/kotlinx-html-common-0.6.12-sources.jar"
},
{
"coord": "org.jetbrains.kotlinx:kotlinx-html-js:0.6.12",
"dependencies": [
"org.jetbrains.kotlinx:kotlinx-html-common:0.6.12",
"org.jetbrains.kotlin:kotlin-stdlib-common:1.2.71",
"org.jetbrains.kotlin:kotlin-stdlib-js:1.2.71"
],
"directDependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-js:1.2.71",
"org.jetbrains.kotlinx:kotlinx-html-common:0.6.12"
],
"file": "v1/http/dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12.jar",
"mirror_urls": [
"http://dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12.jar",
"https://maven.google.comorg/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12.jar",
"https://repo1.maven.org/maven2org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12.jar"
],
"sha256": "c62f5577ecfb6452c950f7e4d6faed9c016049eb2e6744099579fd888b071999",
"url": "http://dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12.jar"
},
{
"coord": "org.jetbrains.kotlinx:kotlinx-html-js:jar:sources:0.6.12",
"dependencies": [
"org.jetbrains.kotlinx:kotlinx-html-common:jar:sources:0.6.12",
"org.jetbrains.kotlin:kotlin-stdlib-common:jar:sources:1.2.71",
"org.jetbrains.kotlin:kotlin-stdlib-js:jar:sources:1.2.71"
],
"directDependencies": [
"org.jetbrains.kotlin:kotlin-stdlib-js:jar:sources:1.2.71",
"org.jetbrains.kotlinx:kotlinx-html-common:jar:sources:0.6.12"
],
"file": "v1/http/dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12-sources.jar",
"mirror_urls": [
"http://dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12-sources.jar",
"https://maven.google.comorg/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12-sources.jar",
"https://repo1.maven.org/maven2org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12-sources.jar"
],
"sha256": "58dd80d73af41e0e2bc0c50df66302411bca3b0cb69c071b5b135015285accb7",
"url": "http://dl.bintray.com/kotlin/kotlinx.html/org/jetbrains/kotlinx/kotlinx-html-js/0.6.12/kotlinx-html-js-0.6.12-sources.jar"
}
],
"version": "0.1.0"
}
}
Loading

0 comments on commit 0912014

Please sign in to comment.