cloudflare / miniflare

🔥 Fully-local simulator for Cloudflare Workers. For the latest version, see https://github.com/cloudflare/workers-sdk/tree/main/packages/miniflare.
https://miniflare.dev
MIT License
3.78k stars 205 forks source link

Cannot get sitePath to work #630

Closed ItalyPaleAle closed 1 year ago

ItalyPaleAle commented 1 year ago

Trying to start Miniflare serving a site too, with this code:

const mf = new Miniflare({
    host: options.address,
    port: options.port,
    workers: [
        {
            name: 'myapp',
            script: script.toString('utf8'),
            scriptPath: 'index.mjs',
            modules: true,
            modulesRules: [{type: 'ESModule', include: ['**/*.js', '**/*.mjs']}],
            compatibilityDate: '2023-05-18',
            sitePath: options.sitePath,
        },
    ],
})

However, this fails with:

MiniflareCoreError [ERR_MODULE_RULE]: Unable to resolve "index.mjs" dependency "__STATIC_CONTENT_MANIFEST": no matching module rules. If you're trying to import an npm package, you'll need to bundle your Worker first

If I modify getServices to add a console.log, I can see that additionalModules is populated correctly:

[
  {
    name: '__STATIC_CONTENT_MANIFEST',
    text: '{"index.html":"$__MINIFLARE_SITES__$/index.html"}'
  }
]

However, it appears that the next line, invoking getWorkerScript, doesn't get this manifest and panics.

ItalyPaleAle commented 1 year ago

Looks like loading the script using the modules array seems to make it work:

new Miniflare({
    host: options.address,
    port: parseInt(options.port, 10),
    workers: [
        {
            name: 'myapp',
            modules: [{
                type: 'ESModule',
                contents: script.toString('utf8'),
                path: 'index.mjs',
            }],
            compatibilityDate: '2023-05-18',
            sitePath: options.sitePath,
        },
    ],
})
mrbbot commented 1 year ago

Hey! 👋 This was raised by an internal user a couple weeks ago and it looks like a PR (#622) was put up to fix it, but closed. We should get that merged. 👍

mrbbot commented 1 year ago

Fixed by #631