Closed berlysia closed 7 months ago
(edited) I thought "it works!" but there is errors. I folded up what I was writing.
References which points to generated codes built by client build cause not found and fallback to index.html
And previous workarounds 1 and 2 are also have same problem, but there is no error because the files by client build is also in dist
directory.
@berlysia
Thanks! I'll check the details later!
Hi, looks like it's working this time, I believe.
in short: Just work with build client and toSSG
into dist
. For my usecase, @hono/vite-ssg
is useless.
Sample repository is here (and deployed here).
◆If you have no time, you can skip to after <hr /> 😉◆
In my investigation, following config achieves the subject.
Build takes following steps:
vite build --mode client
.
.hono
directory and emit client bundles and manifest.json .vite build
.
@hono/vite-ssg
generates pages by toSSG
into .hono
directory.
.hono
directory.dist
directory.toSSG
-> vite build
generates __vite__mapDeps.viteFileDeps
twice . But there is no error because __vite__mapDeps.viteFileDeps
is empty.
This is remote cause the weird errors described in https://github.com/honojs/honox/issues/48#issuecomment-1944578146 .
But, I think we don't need the step 2-ii, 2-iii . Just work with build client and toSSG
into dist
, right?
This fundamentally resolves the problem surrounding __vite__mapDeps.viteFileDeps
.
So what I want is just like this:
vite build
with plugins: [client()]
build.ts
that contains https://github.com/honojs/vite-plugins/blob/55e3abfb8017c7ee9606458da665e3f09e0d428f/packages/ssg/src/ssg.ts#L34-L50 , with NODE_ENV=production
Hi @berlysia
Sorry to be late!
But, I think we don't need the step 2-ii, 2-iii . Just work with build client and
toSSG
intodist
, right?
Indeed, you are right. I've looked into your sample repository, that's simple and great! Perhaps, we can apply that way to the @hono/vite-ssg
. Can you try to fix the @hono/vite-ssg
to support it?
Great. I will try it.
What is the feature you are proposing?
(update) Does not work even with the steps written in the README , there is no client bundles!
Currently,
@hono/vite-ssg
andhonox/vite/client
combination cannot work in naive way.vite build --mode client && vite build
results the latter build cleanups the former.vite build
. A generated client script referenced byScript
component cannot resolve by Vite.current `vite.config.ts` file example
```ts // vite.config.ts import honox from "honox/vite"; import client from "honox/vite/client"; import { defineConfig } from "vite"; import ssg from "@hono/vite-ssg"; const entry = "./app/server.ts"; export default defineConfig(({ mode, command }) => { const plugins = mode === "client" ? [client()] : [honox(), ssg({ entry })]; return { build: { rollupOptions: { input: ["./app/style.css"], output: { assetFileNames: "static/assets/[name].[ext]", }, } }, plugins, }; }); ```An error log of server build
``` Error: [vite]: Rollup failed to resolve import "/static/client-qvhPWvF1.js" from "(repo-root)/.hono/index.html". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to `build.rollupOptions.external` at viteWarn (file://(repo-root)/node_modules/vite/dist/node/chunks/dep-94_H5fT6.js:67040:27) at onRollupWarning (file://(repo-root)/node_modules/vite/dist/node/chunks/dep-94_H5fT6.js:67068:9) at onwarn (file://(repo-root)/node_modules/vite/dist/node/chunks/dep-94_H5fT6.js:66777:13) at file://(repo-root)/node_modules/rollup/dist/es/shared/node-entry.js:17457:13 at Object.logger [as onLog] (file://(repo-root)/node_modules/rollup/dist/es/shared/node-entry.js:19117:9) at ModuleLoader.handleInvalidResolvedId (file://(repo-root)/node_modules/rollup/dist/es/shared/node-entry.js:18061:26) at file://(repo-root)/node_modules/rollup/dist/es/shared/node-entry.js:18019:26 error: script "build" exited with code 1 ``` Use of `build.rollupOptions.external` does not work. A generated script element will be removed in a generated HTML.WORKAROUND for 1: Write a custom plugin that build.emptyOutDir set to false. This derives from build.emptyOutDir set to true by @hono/vite-ssg .
WORKAROUND for 2: Use resolve.alias.
`vite.config.ts` file with workaround
```ts // vite.config.ts import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import honox from "honox/vite"; import client from "honox/vite/client"; import { defineConfig } from "vite"; import ssg from "@hono/vite-ssg"; const entry = "./app/server.ts"; export default defineConfig(({ mode, command }) => { const plugins = mode === "client" ? [client()] : [ honox({ entry }), ssg({ entry }), { config() { return { build: { emptyOutDir: false } }; }, }, ]; return { build: { rollupOptions: { input: ["./app/style.css"], output: { assetFileNames: "static/assets/[name].[ext]", }, } }, plugins, resolve: { alias: [ { find: /^\/static\/(.*?)\.js/, replacement: resolve( // Node 18 support, for 20 or upper, `import.meta.dirname` also works dirname(fileURLToPath(import.meta.url)), "dist/static/$1.js" ), }, ], }, }; }); ```