コンテンツにスキップ

モジュール

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 std.math
from std.math use sin, cos
import * as math from "std/math";
import { sin, cos } from "std/math";
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");