-
-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
__STATIC_CONTENT_MANIFEST error when bundling with Cloudflare's wrangler. #1127
Comments
Hi @JeffBue, Try using esbuild with this command:
But it shouldn't throw an error when using Wrangler. |
Thank you @yusukebe for responding!
Here's the command logs I get:
Please let me know if this helps narrow the troubleshooting. |
I believe you don't need to use the |
@yusukebe
commenting out the import and just using the global. |
When I start this project, I get the same error: pnpm run dev
update: |
@yusukebe I'm also facing this issue. I noticed it while working on the solution for workers site at honojs/vite-plugins#26. This issue arises from the fact that the configuration settings for the build set by users of the hono (such as vite or tsup) cannot affect the configuration settings of the hono itself. To address this issue, it is necessary to change it to a form where the For example, let's say you added:
In this case, the imports will be resolved in the order of (1), then (2). When building at this point, you will encounter the following error message:
Next, include // Plugin for Vite
{
name: "example/vite-plugin",
resolveId(id, importer, options) {
if (id === "__STATIC_CONTENT_MANIFEST") {
return id;
}
return null; // ignore other cases
},
load(id) {
if (id === "__STATIC_CONTENT_MANIFEST") {
// Provide a custom code for __STATIC_CONTENT_MANIFEST
return `export default "{}"`;
}
return null;
},
}
// global.d.ts
declare module '__STATIC_CONTENT_MANIFEST' {
const value: string;
export default value;
} After this, if you build again, you will notice that the initial import issue is resolved, but now an error occurs on the hono side.
|
You can avoid the error by marking import { defineConfig } from 'vite'
import devServer from '@hono/vite-dev-server'
export default defineConfig({
build: {
rollupOptions: {
external: ['__STATIC_CONTENT_MANIFEST']
}
},
plugins: [
devServer({
entry: 'src/index.tsx'
})
]
}) |
@yusukebe I've already tried that. I cannot avoid it Same reason: #1127 (comment) |
@yusukebe https://github.com/Code-Hex/hono-serve You will get error message such as below:
|
Thank you! It is reproduced. However, I didn't expect It's the same with Cloudflare Pages. You can refer to this template: https://github.com/honojs/starter/tree/main/templates/cloudflare-pages If you only build it, you won't face this issue. |
@yusukebe Thank you for your reply. Yes, the build step is no problem. But I don't think we can develop an application w/o development mode.
The issue here arises from the fact that the As I mentioned in the linked comments, I am currently working on modifying the plugin mechanism to make it compatible with Wrangler as well. If you continue to hold your position, I might have to consider pausing my work. |
How about trying this, even though it might not be what you were expecting? |
@yusukebe While it might be acceptable for my specific use case, I believe it's a bit delicate to restrict users in how they can use it if we intend to release it as an official plugin. |
Yes, ideally, we should support it. However, I think our current status is sufficient. This is a matter of perspective. For the development phase, if we want fast reloading, we should use the The reason I created the Vite plugin |
@yusukebe Thanks. I believe the main focus of this discussion, in my opinion, is whether it's feasible to achieve the preliminary stage before discussing speed. First, it should be possible to reference the 'public' path (as specified in Wrangler) only in development mode. Perhaps we share the same understanding here. The second and foremost point is that, in development, you cannot modify the behavior specific to Workers Site (such as The reason for this is that hono is already bundling its own specific code. |
Thanks for summarizing. I've been considering the The popular library @cloudflare/kv-asset-handler, which is used for handling KV assets, treats import { getAssetFromKV, NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler'
import manifestJSON from '__STATIC_CONTENT_MANIFEST'
const assetManifest = JSON.parse(manifestJSON)
export default {
async fetch(request, env, ctx) {
if (request.url.includes('/docs')) {
try {
return await getAssetFromKV(
{
request,
waitUntil(promise) {
return ctx.waitUntil(promise)
},
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
ASSET_MANIFEST: assetManifest,
},
)
} catch (e) {
if (e instanceof NotFoundError) {
// ...
} else if (e instanceof MethodNotAllowedError) {
// ...
} else {
return new Response('An unexpected error occurred', { status: 500 })
}
}
} else return fetch(request)
},
} So, I could follow this method, but I included '__STATIC_CONTENT_MANIFEST' directly in Hono's code because I prefer not to write an import statement for convenience. I'm not sure if this decision was good or bad, but in this situation, it became necessary to make it a required option. Anyway, your PR #1804 introduces a breaking change, so we can't merge it right now. If we do, it should be when releasing v4. I think the PR is good and we can include it in v4. |
@yusukebe How about importing this line dynamically? It seems hard because it needs to be async 😇 |
Dynamic import! Perhap, can we do that with this code? diff --git a/src/adapter/cloudflare-workers/serve-static.ts b/src/adapter/cloudflare-workers/serve-static.ts
index e03a473..3bff189 100644
--- a/src/adapter/cloudflare-workers/serve-static.ts
+++ b/src/adapter/cloudflare-workers/serve-static.ts
@@ -35,9 +35,8 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew
if (!path) return await next()
const content = await getContentFromKVAsset(path, {
- manifest: options.manifest,
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
+ manifest:
+ typeof options.manifest === 'function' ? await options.manifest() : options.manifest,
namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : undefined,
})
if (content) {
diff --git a/src/adapter/cloudflare-workers/server-static-module.ts b/src/adapter/cloudflare-workers/server-static-module.ts
index 1eed8ab..0a1050e 100644
--- a/src/adapter/cloudflare-workers/server-static-module.ts
+++ b/src/adapter/cloudflare-workers/server-static-module.ts
@@ -2,7 +2,6 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// For ES module mode
-import manifest from '__STATIC_CONTENT_MANIFEST'
import type { ServeStaticOptions } from './serve-static'
import { serveStatic } from './serve-static'
@@ -10,7 +9,9 @@ const module = (options: ServeStaticOptions = { root: '' }) => {
return serveStatic({
root: options.root,
path: options.path,
- manifest: options.manifest ? options.manifest : manifest,
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ manifest: options.manifest ? options.manifest : async () => import('__STATIC_CONTENT_MANIFEST'),
rewriteRequestPath: options.rewriteRequestPath,
})
}
diff --git a/tsconfig.json b/tsconfig.json
index c686743..dd5c067 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "ES2020",
+ "module": "ESNext",
"declaration": true,
"moduleResolution": "Node",
"outDir": "./dist" |
@yusukebe Great your idea! |
Resolve by #1984 |
When using [email protected] for Cloudflare's Workers, when bundling with Wrangler or ESbuild I get this error when importing
serveStatic
fromhono/cloudflare-workers
Even when I mark it as
external
andexclude it form the bundle
When running the worker I get an error statingUncaught SyntaxError: Cannot use import statement outside a module
The text was updated successfully, but these errors were encountered: