Skip to main content

Modules

A module is a single .kscript file. Modules expose symbols through export and consume symbols from other modules through import.

Exporting

Any top-level declaration can be exported by prefixing it with export:

export var version: String = "1.0"
export class Model {}
export enum Page { main, settings }
export component Header {}
export modifier Highlight {}
export type PlayerID = Int

Re-exporting

Use export { … } from to re-export symbols from another module as part of the current module:

export { Rectangle, Text } from ui

Use export * as to re-export an entire module as a single named namespace symbol:

export * as util from util

Consumers of this module can then access the namespace directly:

import { util } from my_module

util.some_function()

Importing

Named Imports

Import specific symbols by name:

import { Text, HStack } from ui

Renaming Imports

Use as to rename a symbol or type at the import site. This is useful to avoid name collisions or to give a symbol a more convenient local name.

import { Text as Label, HStack as Row } from ui

Row {
Label("Hello") // same as Text("Hello")
}

To import a symbol from a nested namespace within a module, use dot-separated names. A rename with as is always required in this case:

import { controls.knob.Knob as Knob, controls.fader.Fader as Fader } from my_module

Wildcard Imports

Import all exported symbols from a module:

import * from my_module

Namespace Imports

Import all symbols under a namespace to avoid name collisions or to make the origin of a symbol explicit:

import * as ui from ui

ui.Text("Hello")

Module Naming

Module names must start with a lowercase letter and may only contain lowercase letters, digits (09), and underscores (_).

Directory Structure

Modules can be organized into subdirectories. Use dot-separated paths to import from nested subdirectories.

Given this file layout:

Resources/komplete_scripts/
main.kscript
components/
tabbar.kscript
kontakt_components/
button.kscript
base/
button_base.kscript

Import from subdirectories as follows:

import * from components.tabbar
import * from kontakt_components.button
import * from kontakt_components.base.button_base
info

Import paths are always resolved from the project root (the komplete_scripts directory), never relative to the file in which you are importing. So no matter from where in the project you import a module, provide the path from the root:

tabbar.kscript
import * from kontakt_components.base.button_base

Folder Modules

A folder can act as a module by placing an init.kscript file inside it. Importing the folder path then resolves to that file, allowing a directory to present a single unified public interface while its internal files remain an implementation detail.

Resources/komplete_scripts/
main.kscript
controls/
init.kscript ← imported when using "controls"
knob.kscript
fader.kscript

init.kscript typically re-exports from the internal modules:

init.kscript
export { Knob } from controls.knob
export { Fader } from controls.fader

Consumers of the folder module only need to know about the folder:

import { Knob, Fader } from controls  // resolves to controls/init.kscript