コンテンツにスキップ

モジュール

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 ///express/// import express
from ///hono/// import [Hono]
from ///vitest/// import [describe, it, expect]
from ///axios/// import axios, [AxiosError]
from ///fs/// import all as fs

上記の import...from 構文と同じJavaScriptにコンパイルされます。

バインディングなしでモジュールの副作用のみをインポートします:

import ///dotenv/config///
import ///./polyfills///
import "dotenv/config";
import "./polyfills";

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 キーワードでPurus組み込みの標準ライブラリモジュールをインポートできます。すべてのモジュール名には p- プレフィックスが付いています。as キーワードは任意で、省略時はモジュール名がそのままバインディング名になります(JS出力では -_ に変換)。ツリーシェイキングにより、実際に使用した関数のみがコンパイル出力に含まれます。

use p-random as r
use p-math as m
use p-string as s
use p-datetime as dt
use p-json as j
use p-object as o
use p-number as n
use p-array as a
use 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) { ... } };
モジュール内容
p-randomrandom, randint, randrange, randbool, uniform, triangular, gauss, expovariate, gammavariate, betavariate, lognormvariate, vonmisesvariate, paretovariate, weibullvariate, choice, choices, wchoices, shuffle, sample, binomial, poisson, geometric, clamp, lerp
p-mathJS Math エイリアス+小文字の定数エイリアス(pi, e, ln2, ln10, log2e, log10e, sqrt2, sqrt1_2
p-stringlen, 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-datetimenow, 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-jsonparse, stringify, prettify
p-objectkeys, values, entries, fromentries, assign, freeze, seal, isfrozen, issealed, hasown, create, is, len, merge, clone, pick, omit
p-numberisfinite, isinteger, isnan, issafe, parsefloat, parseint, tofixed, toprecision, toexponential, tostring, clamp + 定数: maxsafe, minsafe, epsilon, maxvalue, minvalue, posinf, neginf
p-arrayisarray, from, of, len, first, last, range, flatten, unique, zip, unzip, chunk, sum, product, min, max, sortasc, sortdesc, compact, count, groupby
p-errorcreate, type, range, reference, syntax, uri, iserror, message, name, stack, cause, wrap
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");
}
namespace utils
fn helper
return 42
const utils = (() => {
function helper() {
return 42;
}
})();

デフォルトでは .purus ファイルは ES Modules(ESM)としてコンパイルされます。--type CLIオプション、config.puruspackage.json を使用してモジュールタイプを CommonJS に設定できます。

  1. CLI --type オプション(最優先)
  2. config.purustype フィールド
  3. package.jsontype フィールド
  4. デフォルト: module(ESM)
Terminal window
purus build --type commonjs
purus build --type module
const type be ///module///

または

const type be ///commonjs///

値は package.jsontype フィールドと同じです: module(ESM)または commonjs(CJS)。

モジュールタイプが 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 42
const VERSION = "1.0";
exports.VERSION = VERSION;
module.exports = 42;
const fs be require[///fs///]
const fs = require("fs");