Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

モジュール

最終更新 Markdown で表示Agent セットアップ

モジュールを有効にする

Miniflare は、従来の service-worker 形式と、新しい modules 形式の両方で Worker を書けます。modules 形式を使うには、次のように有効にします。

const mf = new Miniflare({
	modules: true,
});

そのうえで、次のような modules 形式の Worker スクリプトを使えます。

export default {
	async fetch(request, env, ctx) {
		// - `request` is the incoming `Request` instance
		// - `env` contains bindings, KV namespaces, Durable Objects, etc
		// - `ctx` contains `waitUntil` and `passThroughOnException` methods
		return new Response("Hello Miniflare!");
	},
	async scheduled(controller, env, ctx) {
		// - `controller` contains `scheduledTime` and `cron` properties
		// - `env` contains bindings, KV namespaces, Durable Objects, etc
		// - `ctx` contains the `waitUntil` method
		console.log("Doing something scheduled...");
	},
};

モジュールルール

Miniflare はすべてのモジュールタイプに対応しています。ESModuleCommonJSTextDataCompiledWasm です。追加のモジュール解決ルールは次のように指定できます。

const mf = new Miniflare({
	modulesRules: [
		{ type: "ESModule", include: ["**/*.js"], fallthrough: true },
		{ type: "Text", include: ["**/*.txt"] },
	],
});

デフォルトルール

次のルールは、モジュールルールリストの末尾に自動で追加されます。同じ globs に一致するルールを指定すると上書きできます。

[
	{ type: "ESModule", include: ["**/*.mjs"] },
	{ type: "CommonJS", include: ["**/*.js", "**/*.cjs"] },
];

役に立ちましたか?