# kotlinjs

Published articles for kotlinjs.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Measuring and analyzing the KotlinJS bundle size - Gustavo Fão Valvassori

DevFeed: [Measuring and analyzing the KotlinJS bundle size - Gustavo Fão Valvassori](<https://devfeed.tech/articles/measuring-and-analyzing-the-kotlinjs-bundle-size-gustavo-fao-valvassori-38293.md>)

Original publisher: [Read original article](<https://touchlab.co/measuring-kotlin-js-bundle>)

Published: 2024-02-16T00:00:00Z

Content type: tutorial

Language: en

Sources: [Touchlab | Enterprise Mobile Innovation & Development](<https://devfeed.tech/sources/touchlab-enterprise-mobile-innovation-development.md>)

Topics: [kotlinjs](<https://devfeed.tech/topics/kotlinjs.md>), [Webpack](<https://devfeed.tech/topics/webpack.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Web Development](<https://devfeed.tech/topics/web-development.md>)

Tags: [analyze](<https://devfeed.tech/tags/analyze.md>), [article](<https://devfeed.tech/tags/article.md>), [devtools](<https://devfeed.tech/tags/devtools.md>), [gradle](<https://devfeed.tech/tags/gradle.md>), [gradle-plugin](<https://devfeed.tech/tags/gradle-plugin.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [kotlin-js](<https://devfeed.tech/tags/kotlin-js.md>), [kotlinjs](<https://devfeed.tech/tags/kotlinjs.md>), [lighthouse](<https://devfeed.tech/tags/lighthouse.md>), [measuring](<https://devfeed.tech/tags/measuring.md>), [performance](<https://devfeed.tech/tags/performance.md>)

### AI overview

This tutorial explains how KotlinJS projects are compiled into JavaScript and bundled with Webpack. It presents two ways to measure and analyze bundle size: examining source maps with SourceMapExplorer or Chrome Lighthouse, and using WebpackBundleAnalyzer.

### Source excerpt

Smaller JavaScript bundle size helps improve a website's loading speed and performance. In this article, we present two different ways of analyzing and measuring KotlinJS bundle size.

## Different ways to distribute and integrate Kotlin/JS library

DevFeed: [Different ways to distribute and integrate Kotlin/JS library](<https://devfeed.tech/articles/different-ways-to-distribute-and-integrate-kotlin-js-library-24710.md>)

Original publisher: [Read original article](<https://dev.to/touchlab/different-ways-to-distribute-and-integrate-kotlinjs-library-1hg3>)

Author: Jigar Brahmbhatt

Published: 2022-07-11T20:24:11Z

Content type: tutorial

Language: en

Sources: [Touchlab](<https://devfeed.tech/sources/touchlab.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Library](<https://devfeed.tech/topics/library.md>), [Gradle](<https://devfeed.tech/topics/gradle.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Webpack](<https://devfeed.tech/topics/webpack.md>), [npm](<https://devfeed.tech/topics/npm.md>), [TypeScript](<https://devfeed.tech/topics/typescript.md>), [browser](<https://devfeed.tech/topics/browser.md>)

Tags: [browser](<https://devfeed.tech/tags/browser.md>), [coding](<https://devfeed.tech/tags/coding.md>), [community](<https://devfeed.tech/tags/community.md>), [development](<https://devfeed.tech/tags/development.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [gradle](<https://devfeed.tech/tags/gradle.md>), [inclusive](<https://devfeed.tech/tags/inclusive.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlinjs](<https://devfeed.tech/tags/kotlinjs.md>), [kotlinmultiplatform](<https://devfeed.tech/tags/kotlinmultiplatform.md>), [library](<https://devfeed.tech/tags/library.md>), [npm](<https://devfeed.tech/tags/npm.md>), [software](<https://devfeed.tech/tags/software.md>), [typescript](<https://devfeed.tech/tags/typescript.md>), [webpack](<https://devfeed.tech/tags/webpack.md>)

### AI overview

A tutorial on distributing and integrating Kotlin/JS libraries in a Kotlin Multiplatform setup. It explains browser and Node.js targets, library and executable binaries, Gradle distribution tasks, Webpack bundles, and publishing output as an npm module with JavaScript and TypeScript usage.

### Source excerpt

Note that examples in this post use Kotlin 1.7.0 In the previous post in the Kotlin/JS series, we learned about @JsExport to expose Kotlin code on the JS side. Now, we would look at various ways to distribute a JS library code through a KMP setup. Table Of Contents Gradle Setup Webpack Node Module npm-publish Using the library Use webpack executable HTML JavaScript TypeScript Use as node module HTML JavaScript TypeScript Gradle Setup If you don't already have a JS target in your KMP library project, then you should check out this blog post first. Let's look at common options to use for JS target, js(IR) { browser() nodejs() binaries.library() binaries.executable() } browser() It sets the JavaScript target execution environment as browser. It provides a Gradle task--jsBrowserTest that runs all js tests inside the browser using karma and webpack. nodejs() It sets the JavaScript target execution environment as nodejs. It provides a Gradle task--jsNodeTest that runs all js tests inside nodejs using the built-in test framework. binaries.library() It tells the Kotlin compiler to produce Kotlin/JS code as a distributable node library. Depending on which target you've used along with this, you would get Gradle tasks to generate library distribution files. Kotlin browser tasks -------------------- jsBrowserDevelopmentLibraryDistribution jsBrowserDevelopmentLibraryPrepare jsBrowserProductionLibraryDistribution jsBrowserProductionLibraryPrepare Kotlin node tasks ----------------- jsNodeDevelopmentLibraryDistribution jsNodeDevelopmentLibraryPrepare jsNodeDevelopmentRun jsNodeProductionLibraryDistribution jsNodeProductionLibraryPrepare jsNodeProductionRun jsNodeRun EitherjsBrowserProductionLibraryDistribution or jsNodeProductionLibraryDistribution task generates output files in <YourLibModule>/build/productionLibrary folder binaries.executable() It tells the Kotlin compiler to produce Kotlin/JS code as webpack executable .js files. Enabling this option generates the following Grad

## @JsExport guide for exposing Kotlin to JS

DevFeed: [@JsExport guide for exposing Kotlin to JS](<https://devfeed.tech/articles/jsexport-guide-for-exposing-kotlin-to-js-24714.md>)

Original publisher: [Read original article](<https://dev.to/touchlab/jsexport-guide-for-exposing-kotlin-to-js-20l9>)

Author: Jigar Brahmbhatt

Published: 2022-03-14T16:19:31Z

Content type: tutorial

Language: en

Sources: [Touchlab](<https://devfeed.tech/sources/touchlab.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>)

Tags: [coding](<https://devfeed.tech/tags/coding.md>), [community](<https://devfeed.tech/tags/community.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [development](<https://devfeed.tech/tags/development.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [experimental](<https://devfeed.tech/tags/experimental.md>), [guide](<https://devfeed.tech/tags/guide.md>), [inclusive](<https://devfeed.tech/tags/inclusive.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlinjs](<https://devfeed.tech/tags/kotlinjs.md>), [kotlinmultiplatform](<https://devfeed.tech/tags/kotlinmultiplatform.md>), [software](<https://devfeed.tech/tags/software.md>)

### AI overview

A guide to exposing Kotlin code to JavaScript through Kotlin/JS using the experimental @JsExport annotation. It explains that Kotlin declarations are hidden from JavaScript by default, demonstrates annotating a Greeting class, and notes that the generated .js and .d.ts files then include the class reference.

### Source excerpt

Note that this post focuses on JS output for Kotlin. There is also a Typescript output (.d.ts file) with some unique issues that this post doesn't cover in detail. In the previous post, we added Kotlin/JS support to an existing KMM library. Now, we would add code that works on the JS side. Table Of Contents Usage @ExperimentalJsExport vs @JsExport Limitations Collections Long Interface Solution - Using Implementation class Solution - Using Expect-Actual Enum Sealed classes Code mangling Suspended functions Usage It is critical to understand @JsExport annotation and all the issues around it if you expose Kotlin code through Kotlin/JS as an external JS library With the new IR compiler, Kotlin declarations do not get exposed to JavaScript by default. To make Kotlin declarations visible to JavaScript, they must be annotated with @JsExport. Note that @JsExport is experimental as of the posted date of this post (with Kotlin 1.6.10) Let's start with a very basic example, // commonMain - Greeting.kt class Greeting { fun greeting(): String { return "Hello World!" } } At this point, the generated .js library file would not have any reference to the Greeting class. The reason is that it is missing the @JsExport annotation. You can generate JS library code via ./gradlew jsBrowserDistribution. You would find the .js, .d.ts and map file in root/build/js/packages/<yourlibname>/kotlin folder. Now, add the annotation to generate JS code for it, import kotlin.js.ExperimentalJsExport import kotlin.js.JsExport @ExperimentalJsExport @JsExport class Greeting { fun greeting(): String { return "Hello World!" } } The .js and .d.ts files would now contain the Greeting reference. Generated .js file ```javascript function Greeting() { } Greeting.prototype.greeting = function () { return 'Hello World!'; }; Greeting.$metadata$ = { simpleName: 'Greeting', kind: 'class', interfaces: [] }; - **Generated .d.ts file** ```typescript export namespace jabbar.jigariyo.kmplibrary { class Greeting { constr