47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
// Post-build script to add Durable Object exports to the Worker
|
|
import { readFileSync, writeFileSync } from 'fs';
|
|
import { fileURLToPath } from 'url';
|
|
import { dirname, join } from 'path';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
|
|
// Go up one level from scripts/ to the project root
|
|
const projectRoot = join(__dirname, '..');
|
|
const workerPath = join(projectRoot, '.svelte-kit', 'cloudflare', '_worker.js');
|
|
|
|
try {
|
|
let content = readFileSync(workerPath, 'utf-8');
|
|
|
|
// Check if already patched
|
|
if (content.includes('CounterDurableObject')) {
|
|
console.log('✓ Worker already patched with Durable Object exports');
|
|
process.exit(0);
|
|
}
|
|
|
|
// Add import for Durable Objects after the env import
|
|
const importPattern = /import { env } from "cloudflare:workers";/;
|
|
const importReplacement = `import { env } from "cloudflare:workers";
|
|
|
|
// Import Durable Objects from hooks.server
|
|
import { CounterDurableObject } from "../output/server/chunks/hooks.server.js";`;
|
|
|
|
content = content.replace(importPattern, importReplacement);
|
|
|
|
// Add Durable Object to exports
|
|
const exportPattern = /export {\s*worker_default as default\s*};/;
|
|
const exportReplacement = `export {
|
|
worker_default as default,
|
|
CounterDurableObject
|
|
};`;
|
|
|
|
content = content.replace(exportPattern, exportReplacement);
|
|
|
|
writeFileSync(workerPath, content, 'utf-8');
|
|
console.log('✓ Successfully patched Worker with Durable Object exports');
|
|
} catch (error) {
|
|
console.error('✗ Failed to patch Worker:', error.message);
|
|
process.exit(1);
|
|
}
|
|
|