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 はすべてのモジュールタイプに対応しています。ESModule、CommonJS、Text、Data、CompiledWasm です。追加のモジュール解決ルールは次のように指定できます。
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"] },
];