モジュール
ESMインポート
Section titled “ESMインポート”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 構文
Section titled “from...import 構文”モジュールパスを先に書く構文も使用できます:
from ///express/// import expressfrom ///hono/// import [Hono]from ///vitest/// import [describe, it, expect]from ///axios/// import axios, [AxiosError]from ///fs/// import all as fs上記の import...from 構文と同じJavaScriptにコンパイルされます。
副作用インポート
Section titled “副作用インポート”バインディングなしでモジュールの副作用のみをインポートします:
import ///dotenv/config///import ///./polyfills///import "dotenv/config";import "./polyfills";インポート属性
Section titled “インポート属性”with キーワードを使用してインポート属性を指定できます:
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" };from...import 構文でも 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(ドットパスインポート)
Section titled “Use(ドットパスインポート)”use std.mathfrom std.math use sin, cosimport * as math from "std/math";import { sin, cos } from "std/math";エクスポート
Section titled “エクスポート”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");}モジュール名前空間
Section titled “モジュール名前空間”namespace utils fn helper return 42const utils = (() => { function helper() { return 42; }})();モジュールタイプ設定
Section titled “モジュールタイプ設定”デフォルトでは .purus ファイルは ES Modules(ESM)としてコンパイルされます。--type CLIオプション、config.purus、package.json を使用してモジュールタイプを CommonJS に設定できます。
- CLI
--typeオプション(最優先) config.purusのtypeフィールドpackage.jsonのtypeフィールド- デフォルト:
module(ESM)
CLIオプション
Section titled “CLIオプション”purus build --type commonjspurus build --type moduleconfig.purus
Section titled “config.purus”const type be ///module///または
const type be ///commonjs///値は package.json の type フィールドと同じです: module(ESM)または commonjs(CJS)。
CommonJS出力
Section titled “CommonJS出力”モジュールタイプが commonjs に設定されている場合、インポートとエクスポートは CJS 構文にコンパイルされます:
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");