Modules
ESM Import
Section titled “ESM Import”import express from ///express///import [Hono] from ///hono///import [describe, it, expect] from ///vitest///import axios, [AxiosError] from ///axios///import all as fs from ///fs///import express from "express";import { Hono } from "hono";import { describe, it, expect } from "vitest";import axios, { AxiosError } from "axios";import * as fs from "fs";from...import syntax
Section titled “from...import syntax”You can also write imports with the module path first:
from ///express/// import expressfrom ///hono/// import [Hono]from ///vitest/// import [describe, it, expect]from ///axios/// import axios, [AxiosError]from ///fs/// import all as fsThis compiles to the same JavaScript as the import...from syntax above.
Side-effect Import
Section titled “Side-effect Import”Import a module for its side effects only (no bindings):
import ///dotenv/config///import ///./polyfills///import "dotenv/config";import "./polyfills";Import Attributes
Section titled “Import Attributes”Use the with keyword to specify import attributes:
import package from ///./package.json/// with [ type be ///json/// ]import [name; version] from ///./package.json/// with [ type be ///json/// ]import package from "./package.json" with { type: "json" };import { name, version } from "./package.json" with { type: "json" };The from...import syntax also supports with:
from ///./data.json/// import data with [ type be ///json/// ]from ///./package.json/// import [name, version] with [ type be ///json/// ]import data from "./data.json" with { type: "json" };import { name, version } from "./package.json" with { type: "json" };Use (dot-path import)
Section titled “Use (dot-path import)”use std.mathfrom std.math use sin, cosimport * as math from "std/math";import { sin, cos } from "std/math";Export
Section titled “Export”public fn greet name to console.log[name]public const VERSION be ///1.0///export default fn main console.log[///hi///]export function greet(name) { console.log(name); }export const VERSION = "1.0";export default function main() { console.log("hi");}Module namespace
Section titled “Module namespace”namespace utils fn helper return 42const utils = (() => { function helper() { return 42; }})();Module Type Configuration
Section titled “Module Type Configuration”By default, .purus files are compiled as ES Modules (ESM). You can configure the module type to CommonJS using the --type CLI option, config.purus, or package.json.
Resolution Order
Section titled “Resolution Order”- CLI
--typeoption (highest priority) config.purustypefieldpackage.jsontypefield- Default:
module(ESM)
CLI Option
Section titled “CLI Option”purus build --type commonjspurus build --type moduleconfig.purus
Section titled “config.purus”const type be ///module///or
const type be ///commonjs///Values are the same as package.json’s type field: module (ESM) or commonjs (CJS).
CommonJS Output
Section titled “CommonJS Output”When module type is set to commonjs, imports and exports are compiled to CJS syntax:
import express from ///express///import [Hono] from ///hono///import all as fs from ///fs///import ///dotenv/config///const express = require("express");const { Hono } = require("hono");const fs = require("fs");require("dotenv/config");public const VERSION be ///1.0///export default 42const VERSION = "1.0";exports.VERSION = VERSION;module.exports = 42;CommonJS
Section titled “CommonJS”const fs be require[///fs///]const fs = require("fs");