Skip to content

Instantly share code, notes, and snippets.

@markknol
Last active May 9, 2023 20:16
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markknol/0de2d8d05e8f3d725946eb5515cc771b to your computer and use it in GitHub Desktop.
Save markknol/0de2d8d05e8f3d725946eb5515cc771b to your computer and use it in GitHub Desktop.
How to get started with Haxe / Coconut

Use Haxe / coconut.mvc from scratch

Prerequisites

Installation

Create a new project directory somewhere, open commandline here

yarn init

yarn add lix
yarn lix scope create
yarn lix use haxe stable
yarn lix install github:MVCoconut/coconut.vdom
md src
md bin

(If the project doesn't compile, copy/paste the content haxe_libraries folder from the todomvc example to yours and run yarn lix download)

Create build.hxml

-cp src
-lib coconut.vdom
-main Main
-dce full
-D analyzer-optimize
-D js_es=6
-js bin/main.js

Create bin/index.html

<!DOCTYPE>
<html>
<head>
 <meta charset="utf8" />
 <title>Hello coconut</title>
</head>
<body>
 <script src="main.js"></script>
</body>
</html>

Create src/Main.hx

package;

import coconut.data.*;
import coconut.ui.*;
import coconut.Ui.hxx;
import tink.state.*;
import js.Browser.*;

using tink.CoreApi;

class Main {
	static function main() {
		coconut.ui.Renderer.mount(
			cast document.body.appendChild(document.createDivElement()), hxx('<Root />')
		);
	}
}

class Root extends View {
	@:state var items:List<Item> = null;

	function add() {
		items = items.append(new Item({id: window.prompt("name")}));
	}

	function remove(id:String) {
		items = items.filter(function(i) return i.id != id);
	}

	function render() '
		<div>
			<button onclick=${add}>Add item</button>
			<ul>
				<for ${i in items}>
					<li>${i.id} <button onclick=${remove(i.id)}>x</button></li>
				</for>
			</ul>
		</div>
	';
}

class Item implements Model {
	@:constant var id:String;
}

Open Visual Studio Code

  1. File > Open folder > Select your new project folder
  2. Build the project: Press CTRL-SHIFT-B > select build.hxml
  3. Open the project: open bin/index.html in your browser, preferably using some webserver.

You're ready to create cool stuff now

Make production build

Minify sources

Since we use ES6 output we need to use terser instead of uglify (from NPM) but we can use the uglify library from Haxelib.

  1. Add dependencies
    yarn add terser
    yarn lix install haxelib:uglifyjs
    
  2. Append to your build.hxml:
    -lib uglifyjs
    -D uglifyjs_overwrite
    -D uglifyjs_bin=node_modules/.bin/terser 
    --no-traces
    
    if you go back to development, you could temporary add -D uglifyjs_disable
  3. In vscode, compile again

Ship it!

@sonygod
Copy link

sonygod commented Jun 25, 2019

thank you @markknol

@sonygod
Copy link

sonygod commented Jun 30, 2019

@markknol
Copy link
Author

markknol commented Jul 1, 2019

its seem install lix by this way

https://jasono.co/2017/10/01/lix-a-step-forward-in-dependency-management-for-haxe-projects/

I personally don't prefer to install lix globally, I just add it as dependency of the project.

@melMass
Copy link

melMass commented Feb 24, 2020

Thanks a lot for the starter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment