Get started

Get started with pin

This page takes about ten minutes. You install pin from npm, compile a program, and run its tests.

What you need

1. Add pin to an existing project

Most people add pin to a project they already have. Install the bundler plugin and the compiler as development dependencies:

npm install -D @pin-lang/bundler @pin-lang/compiler

With other package managers, use the same packages:

pnpm add -D @pin-lang/bundler @pin-lang/compiler
yarn add -D @pin-lang/bundler @pin-lang/compiler
bun add -d @pin-lang/bundler @pin-lang/compiler

In a project, run the command with npx pin, or put it in a package.json script.

Then add the plugin to your bundler config, as shown in step 4.

Use the pin command without a project

To compile and test files outside a project, install the command, or run it once with npx:

npm install -g @pin-lang/compiler
npx @pin-lang/compiler --help

Check that the command works:

pin --help

The commands in the rest of this page use npx pin. Replace npx pin with pin when you install the command globally.

2. Compile your first program

Save this file as hello.pin:

name = "pin"
greeting = "hello #{name}"
console.log(greeting)

Compile it:

npx pin hello.pin

This writes three files next to hello.pin: hello.ts, hello.ts.map, and hello.pin.d.ts. The .ts file keeps the names and the line structure of your program. The .d.ts file gives TypeScript the types of your exports.

3. Format and test

Format the file in place:

npx pin fmt hello.pin

Add a test to the file:

test "greets pin":
  assert "hello #{"pin"}" is "hello pin"

Run the tests:

npx pin test hello.pin

Each test prints ok or FAIL. The command exits with 1 when a test fails.

4. Connect the bundler

Add the plugin to your bundler config. That is all the setup there is. Workers, extensions, and loaders are configured for you.

Vite:

// vite.config.js
import { defineConfig } from "vite";
import pin from "@pin-lang/bundler/vite";

export default defineConfig({ plugins: [pin()] });

Rollup:

// rollup.config.js
import pin from "@pin-lang/bundler/rollup";

export default { input: "src/main.pin", output: { dir: "dist", format: "esm" }, plugins: [pin()] };

webpack:

// webpack.config.js
import pin from "@pin-lang/bundler/webpack";

export default { entry: "./src/main.pin", plugins: [pin()] };

esbuild:

// build.mjs
import pin from "@pin-lang/bundler/esbuild";
import { build } from "esbuild";

await build({ entryPoints: ["src/main.pin"], bundle: true, outfile: "dist/main.js", plugins: [pin()] });

Then import a .pin file like any other module:

import { fib } from "./math.pin";
console.log(fib(30));

5. Use the compiler from JavaScript

Use the compiler API when you build your own tool:

import { ready, compile } from "@pin-lang/compiler";

await ready();
const { main } = compile(
  'name = "pin"\ngreeting = "hello #{name}"\nconsole.log(greeting)\n'
);
console.log(main);

main is TypeScript. Errors throw a PinCompileError with a line:col message.

Where to go next