Skip to content
This repository has been archived by the owner on Oct 23, 2018. It is now read-only.

Modules

Onoshko Dan edited this page Jan 10, 2015 · 5 revisions

ColaScript supports exits CommonJS modules and have syntax to create them.

With @import command you can import modules:

@import 'sugar' // import all items in current scope
@import 'fs' as fs
@import dirname from 'path'
@import print as echo, prompt as ask, test from 'mylib'
		
String code = fs.readFileSync(dirname(test) + "/main.cola", "utf8");
echo("hello!");

With @export command you can export and re-export components:

Object hash = {};
Array users = [];
		
@export hash as _, users
@export each as forEach from "underscore"
@export "mylib" // re-export all

To associate package names with sources you should create packages.json file. You can generate this file with Dr Package.

{
    "underscore": "./components/jashkenas/underscore/1.7.0/underscore.js",
    "uid": "./components/[email protected]/index.js",
    "fmt": "./components/[email protected]/index.js"
}

For browser-cola you can use next code in html:

<script type="text/packages-json">
{
    "underscore": "./components/jashkenas/underscore/1.7.0/underscore.js",
    "uid": "./components/[email protected]/index.js",
    "fmt": "./components/[email protected]/index.js"
}
</script>

or 

<script type="text/packages-json" src="custom/path/to/packages.json"></script>