モジュール
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 キーワードでPurus組み込みの標準ライブラリモジュールをインポートできます。すべてのモジュール名には p- プレフィックスが付いています。as キーワードは任意で、省略時はモジュール名がそのままバインディング名になります(JS出力では - が _ に変換)。ツリーシェイキングにより、実際に使用した関数のみがコンパイル出力に含まれます。
use p-random as ruse p-math as muse p-string as suse p-datetime as dtuse p-json as juse p-object as ouse p-number as nuse p-array as ause p-error as e
-- `as` は省略可能:use p-math -- use p-math as p-math と同じ(binding: p_math)use p-random -- use p-random as p-random と同じ(binding: p_random)// 使用した関数のみが出力されます(ツリーシェイキング)const r = { randint(a, b) { ... }, choice(arr) { ... } };const m = { floor: Math.floor, pi: Math.PI };const s = { upper(str) { ... }, reverse(str) { ... } };利用可能なモジュール
Section titled “利用可能なモジュール”| モジュール | 内容 |
|---|---|
p-random | random, randint, randrange, randbool, uniform, triangular, gauss, expovariate, gammavariate, betavariate, lognormvariate, vonmisesvariate, paretovariate, weibullvariate, choice, choices, wchoices, shuffle, sample, binomial, poisson, geometric, clamp, lerp |
p-math | JS Math エイリアス+小文字の定数エイリアス(pi, e, ln2, ln10, log2e, log10e, sqrt2, sqrt1_2) |
p-string | len, contains, startswith, endswith, indexof, count, upper, lower, capitalize, title, trim, trimstart, trimend, reverse, repeat, replace, replacefirst, padstart, padend, split, lines, words, join, chars, slice, charat, codeat, fromcode |
p-datetime | now, today, timestamp, create, utccreate, fromiso, year, month, day, weekday, hour, minute, second, ms, utcyear, utcmonth, utcday, utcweekday, utchour, utcminute, utcsecond, utcms, tzyear, tzmonth, tzday, tzweekday, tzhour, tzminute, tzsecond, toiso, tolocale, todate, totime, format, addms, addseconds, addminutes, addhours, adddays, diff, diffdays, diffhours, diffminutes, diffseconds, offset, localtz |
p-json | parse, stringify, prettify |
p-object | keys, values, entries, fromentries, assign, freeze, seal, isfrozen, issealed, hasown, create, is, len, merge, clone, pick, omit |
p-number | isfinite, isinteger, isnan, issafe, parsefloat, parseint, tofixed, toprecision, toexponential, tostring, clamp + 定数: maxsafe, minsafe, epsilon, maxvalue, minvalue, posinf, neginf |
p-array | isarray, from, of, len, first, last, range, flatten, unique, zip, unzip, chunk, sum, product, min, max, sortasc, sortdesc, compact, count, groupby |
p-error | create, type, range, reference, syntax, uri, iserror, message, name, stack, cause, wrap |
エクスポート
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");