Spaghetti provides type-safe communication between JavaScript modules.
Because of the untyped nature of JavaScript, modularizing large, evolving JavaScript applications is difficult. To help keep track of changing APIs, Spaghetti uses compilers to check communication between modules, transforming run-time API compatibility problems into more manageable compile errors.
Spaghetti modules are written in compile-to-JS languages like TypeScript and Haxe. Each module's API is defined in a [Spaghetti Interface Definition file](/../../wiki/Spaghetti Syntax). Here's an example of a typical API definition:
module com.example.greeter {
interface Greeter {
sayHello(user: string): string;
}
createGreeter(): Greeter;
}
Based on this abstract definition, Spaghetti ensures type safety on both the implementor and the caller side of an API:
-
checking if a module implements its API properly is done via generated interfaces that the module must implement. This way the compiler can check if you've made a mistake or have forgotten something. From the above example, the generated code for the
Greeter
Spaghetti interface in a TypeScript module looks like this:/* Generated by Spaghetti */ module com.example.greeter { export interface Greeter { sayHello(user:string):string; } }
-
checking if a module is calling the right API of its dependency is also done via code generation. Spaghetti generates language-specific proxy classes to access other modules in a type-safe way, based on those modules' Spaghetti APIs. Here's how you would use
Greeter
from an Haxe module:import com.example.greeter.GreeterModule; // ... var greeter = GreeterModule.createGreeter(); trace(greeter.sayHello("World"));
If you make a typo in sayHello
, or try to pass a number as its parameter, the Haxe compiler will fail with an error.
For a detailed explanation of the steps to building a Spaghetti application, see Workflow on the wiki.
Spaghetti is a Java-based tool, and requires Java 8 or newer. It has multiple interfaces:
- The command-line tool is the quickest way to get working with Spaghetti.
- Gradle plugins make it easy to integrate Spaghetti into your workflow.
- Maven and other build system support has been planned, but no deadline has been set yet.
- Follow the tutorial for a step-by-step introduction to Spaghetti.
- Check the demo application.
- See the code behind the demo, which uses Gradle integration.
The documentation is available on the wiki.
Issues, suggestions and pull-requests are welcome. You can build Spaghetti yourself by running:
./gradlew install
You will need the following installed to build Spaghetti locally:
- Java 7 or newer
- TypeScript and Haxe to run the integration tests
To install Haxe:
- on Mac OS use Homebrew:
brew install haxe
- on Linux and Windows you can download installers from http://haxe.org
To install TypeScript:
- you'll need Node.js and NPM first, then run:
npm install -g typescript
Get in touch with Spaghetti developers at: [email protected].