← Cloudflare Workers / workers / testing / miniflare / core
Модули
Включение модулей
Miniflare поддерживает как традиционный service-worker и более новый modules форматов для написания воркеров. Чтобы использовать 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"] },
],
});Правила по умолчанию
Следующие правила автоматически добавляются в конец вашего списка правил modules.
Вы можете переопределить их, указав правила, соответствующие тем же globs:
[
{ type: "ESModule", include: ["**/*.mjs"] },
{ type: "CommonJS", include: ["**/*.js", "**/*.cjs"] },
];