honox 0.1.21 → 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -93,6 +93,7 @@ There are three ways to define routes.
93
93
  Each route should return an array of `Handler | MiddlewareHandler`. `createRoute()` is a helper function to return it. You can write a route for a GET request with `default export`.
94
94
 
95
95
  ```tsx
96
+ // app/routes/index.tsx
96
97
  // `createRoute()` helps you create handlers
97
98
  import { createRoute } from 'honox/factory'
98
99
 
@@ -108,6 +109,7 @@ export default createRoute((c) => {
108
109
  You can also handle methods other than GET by `export` `POST`, `PUT`, and `DELETE`.
109
110
 
110
111
  ```tsx
112
+ // app/routes/index.tsx
111
113
  import { createRoute } from 'honox/factory'
112
114
  import { getCookie, setCookie } from 'hono/cookie'
113
115
 
@@ -157,6 +159,7 @@ export default app
157
159
  Or simply, you can just return JSX.
158
160
 
159
161
  ```tsx
162
+ // app/routes/index.tsx
160
163
  export default function Home(_c: Context) {
161
164
  return <h1>Welcome!</h1>
162
165
  }
@@ -660,21 +663,17 @@ Finally, add `vite.config.ts` configuration to output assets for the production.
660
663
  ```ts
661
664
  import honox from 'honox/vite'
662
665
  import { defineConfig } from 'vite'
666
+ import pages from '@hono/vite-cloudflare-pages'
663
667
 
664
- export default defineConfig(({ mode }) => {
665
- if (mode === 'client') {
666
- return {
667
- build: {
668
- rollupOptions: {
669
- input: ['/app/style.css'],
670
- },
668
+ export default defineConfig({
669
+ plugins: [
670
+ honox({
671
+ client: {
672
+ input: ['/app/style.css'],
671
673
  },
672
- }
673
- } else {
674
- return {
675
- plugins: [honox()],
676
- }
677
- }
674
+ }),
675
+ pages(),
676
+ ],
678
677
  })
679
678
  ```
680
679
 
@@ -690,13 +689,10 @@ import remarkFrontmatter from 'remark-frontmatter'
690
689
  import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
691
690
  import { defineConfig } from 'vite'
692
691
 
693
- const entry = './app/server.ts'
694
-
695
692
  export default defineConfig(() => {
696
693
  return {
697
694
  plugins: [
698
695
  honox(),
699
- devServer({ entry }),
700
696
  mdx({
701
697
  jsxImportSource: 'hono/jsx',
702
698
  remarkPlugins: [remarkFrontmatter, remarkMdxFrontmatter],
@@ -800,28 +796,6 @@ export default defineConfig({
800
796
  })
801
797
  ```
802
798
 
803
- If you want to include client-side scripts and assets:
804
-
805
- ```ts
806
- // vite.config.ts
807
- import pages from '@hono/vite-cloudflare-pages'
808
- import honox from 'honox/vite'
809
- import client from 'honox/vite/client'
810
- import { defineConfig } from 'vite'
811
-
812
- export default defineConfig(({ mode }) => {
813
- if (mode === 'client') {
814
- return {
815
- plugins: [client()],
816
- }
817
- } else {
818
- return {
819
- plugins: [honox(), pages()],
820
- }
821
- }
822
- })
823
- ```
824
-
825
799
  Build command (including a client):
826
800
 
827
801
  ```txt
@@ -49,6 +49,7 @@ const createApp = (options) => {
49
49
  paths.sort((a, b) => a.split("/").length - b.split("/").length);
50
50
  return paths;
51
51
  };
52
+ const errorHandlerMap = {};
52
53
  for (const map of routesMap) {
53
54
  for (const [dir, content] of Object.entries(map)) {
54
55
  const subApp = new Hono();
@@ -105,7 +106,16 @@ const createApp = (options) => {
105
106
  });
106
107
  }
107
108
  }
108
- applyError(subApp, dir, errorMap);
109
+ const errorHandler = getErrorHandler(dir, errorMap);
110
+ if (errorHandler) {
111
+ errorHandlerMap[dir] = errorHandler;
112
+ }
113
+ for (const [path, errorHandler2] of Object.entries(errorHandlerMap)) {
114
+ const regExp = new RegExp(`^${path}`);
115
+ if (regExp.test(dir) && errorHandler2) {
116
+ subApp.onError(errorHandler2);
117
+ }
118
+ }
109
119
  let rootPath = getRootPath(dir);
110
120
  if (trailingSlash) {
111
121
  rootPath = /\/$/.test(rootPath) ? rootPath : rootPath + "/";
@@ -143,20 +153,23 @@ function applyNotFound(app, dir, map) {
143
153
  }
144
154
  }
145
155
  }
146
- function applyError(app, dir, map) {
156
+ function getErrorHandler(dir, map) {
147
157
  for (const [mapDir, content] of Object.entries(map)) {
148
158
  if (dir === mapDir) {
149
159
  const errorFile = content[ERROR_FILENAME];
150
160
  if (errorFile) {
151
- const errorHandler = errorFile.default;
152
- app.onError(async (error, c) => {
153
- const importingIslands = errorFile[IMPORTING_ISLANDS_ID];
154
- if (importingIslands) {
155
- c.set(IMPORTING_ISLANDS_ID, importingIslands);
156
- }
157
- c.status(500);
158
- return errorHandler(error, c);
159
- });
161
+ const matchedErrorHandler = errorFile.default;
162
+ if (matchedErrorHandler) {
163
+ const errorHandler = async (error, c) => {
164
+ const importingIslands = errorFile[IMPORTING_ISLANDS_ID];
165
+ if (importingIslands) {
166
+ c.set(IMPORTING_ISLANDS_ID, importingIslands);
167
+ }
168
+ c.status(500);
169
+ return matchedErrorHandler(error, c);
170
+ };
171
+ return errorHandler;
172
+ }
160
173
  }
161
174
  }
162
175
  }
@@ -19,8 +19,8 @@ const createApp = (options) => {
19
19
  }),
20
20
  ROUTES: options?.ROUTES ?? import.meta.glob(
21
21
  [
22
- "/app/routes/**/!(_*|*.test|*.spec).(ts|tsx|md|mdx)",
23
- "/app/routes/.well-known/!(_*|*.test|*.spec).(ts|tsx|md|mdx)"
22
+ "/app/routes/**/!(_*|$*|*.test|*.spec).(ts|tsx|md|mdx)",
23
+ "/app/routes/.well-known/!(_*|$*|*.test|*.spec).(ts|tsx|md|mdx)"
24
24
  ],
25
25
  {
26
26
  eager: true
@@ -1,10 +1,11 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
- type Options = {
3
+ type ClientOptions = {
4
4
  jsxImportSource?: string;
5
5
  assetsDir?: string;
6
+ input?: string[];
6
7
  };
7
- declare const defaultOptions: Options;
8
- declare function client(options?: Options): Plugin;
8
+ declare const defaultOptions: ClientOptions;
9
+ declare function client(options?: ClientOptions): Plugin;
9
10
 
10
- export { client as default, defaultOptions };
11
+ export { type ClientOptions, client as default, defaultOptions };
@@ -1,15 +1,23 @@
1
1
  const defaultOptions = {
2
2
  jsxImportSource: "hono/jsx/dom",
3
- assetsDir: "static"
3
+ assetsDir: "static",
4
+ input: []
4
5
  };
5
6
  function client(options) {
6
7
  return {
7
8
  name: "honox-vite-client",
9
+ apply: (_config, { command, mode }) => {
10
+ if (command === "build" && mode === "client") {
11
+ return true;
12
+ }
13
+ return false;
14
+ },
8
15
  config: () => {
16
+ const input = options?.input ?? defaultOptions.input ?? [];
9
17
  return {
10
18
  build: {
11
19
  rollupOptions: {
12
- input: ["/app/client.ts"]
20
+ input: ["/app/client.ts", ...input]
13
21
  },
14
22
  assetsDir: options?.assetsDir ?? defaultOptions.assetsDir,
15
23
  manifest: true
@@ -3,12 +3,14 @@ export { defaultOptions as devServerDefaultOptions } from '@hono/vite-dev-server
3
3
  import { PluginOption } from 'vite';
4
4
  import { IslandComponentsOptions } from './island-components.js';
5
5
  export { islandComponents } from './island-components.js';
6
+ import { ClientOptions } from './client.js';
6
7
 
7
8
  type Options = {
8
9
  islands?: boolean;
9
10
  entry?: string;
10
11
  devServer?: DevServerOptions;
11
12
  islandComponents?: IslandComponentsOptions;
13
+ client?: ClientOptions;
12
14
  external?: string[];
13
15
  };
14
16
  declare const defaultOptions: Options;
@@ -3,6 +3,7 @@ import devServer, { defaultOptions as devServerDefaultOptions } from "@hono/vite
3
3
  import { injectImportingIslands } from "./inject-importing-islands.js";
4
4
  import { islandComponents } from "./island-components.js";
5
5
  import { restartOnAddUnlink } from "./restart-on-add-unlink.js";
6
+ import client from "./client.js";
6
7
  const defaultOptions = {
7
8
  islands: true,
8
9
  entry: path.join(process.cwd(), "./app/server.ts")
@@ -27,6 +28,7 @@ function honox(options) {
27
28
  }
28
29
  plugins.push(injectImportingIslands());
29
30
  plugins.push(restartOnAddUnlink());
31
+ plugins.push(client(options?.client));
30
32
  return [
31
33
  {
32
34
  name: "honox-vite-config",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "honox",
3
- "version": "0.1.21",
3
+ "version": "0.1.23",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -112,7 +112,7 @@
112
112
  "@babel/parser": "^7.23.6",
113
113
  "@babel/traverse": "^7.23.6",
114
114
  "@babel/types": "^7.23.6",
115
- "@hono/vite-dev-server": "^0.12.1",
115
+ "@hono/vite-dev-server": "^0.12.2",
116
116
  "jsonc-parser": "^3.2.1",
117
117
  "precinct": "^12.0.2"
118
118
  },