@blinkk/root 1.0.0-alpha.3 → 1.0.0-alpha.30

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/bin/root.js CHANGED
@@ -4,26 +4,37 @@ import {createRequire} from 'node:module';
4
4
  import path from 'node:path';
5
5
  import {fileURLToPath} from 'node:url';
6
6
  import {Command} from 'commander';
7
- import {build, dev, start} from '../dist/cli.js';
7
+ import {build, dev, preview, start} from '../dist/cli.js';
8
+ import {bgGreen, black} from 'kleur/colors';
8
9
 
9
10
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
10
11
  const require = createRequire(import.meta.url);
11
12
  const packageJson = require(path.join(__dirname, '../package.json'));
12
13
 
13
- const program = new Command('root');
14
- program.version(packageJson.version);
14
+ async function main() {
15
+ console.log(`🌱 ${bgGreen(black(' root.js '))} v${packageJson.version}`);
15
16
 
16
- program
17
- .command('build [path]')
18
- .description('generates a static build')
19
- .action(build);
20
- program
21
- .command('dev [path]')
22
- .description('starts the server in development mode')
23
- .action(dev);
24
- program
25
- .command('start [path]')
26
- .description('starts the server in production mode')
27
- .action(start);
17
+ const program = new Command('root');
18
+ program.version(packageJson.version);
19
+ program
20
+ .command('build [path]')
21
+ .description('generates a static build')
22
+ .option('--ssr-only', 'produce a ssr-only build')
23
+ .option('--mode <mode>', 'see: https://vitejs.dev/guide/env-and-mode.html#modes', 'production')
24
+ .action(build);
25
+ program
26
+ .command('dev [path]')
27
+ .description('starts the server in development mode')
28
+ .action(dev);
29
+ program
30
+ .command('preview [path]')
31
+ .description('starts the server in preview mode')
32
+ .action(preview);
33
+ program
34
+ .command('start [path]')
35
+ .description('starts the server in production mode')
36
+ .action(start);
37
+ await program.parseAsync(process.argv);
38
+ }
28
39
 
29
- program.parse(process.argv);
40
+ main();
@@ -1,5 +1,5 @@
1
1
  // src/core/components/error-page.tsx
2
- import { h } from "preact";
2
+ import { jsx, jsxs } from "preact/jsx-runtime";
3
3
  function ErrorPage(props) {
4
4
  const error = props.error;
5
5
  let message = void 0;
@@ -10,7 +10,18 @@ function ErrorPage(props) {
10
10
  message = String(error);
11
11
  }
12
12
  }
13
- return /* @__PURE__ */ h("div", null, /* @__PURE__ */ h("div", null, /* @__PURE__ */ h("p", null, "An error occured during route handling or page rendering."), message && /* @__PURE__ */ h("pre", null, message)));
13
+ return /* @__PURE__ */ jsx("div", {
14
+ children: /* @__PURE__ */ jsxs("div", {
15
+ children: [
16
+ /* @__PURE__ */ jsx("p", {
17
+ children: "An error occured during route handling or page rendering."
18
+ }),
19
+ message && /* @__PURE__ */ jsx("pre", {
20
+ children: message
21
+ })
22
+ ]
23
+ })
24
+ });
14
25
  }
15
26
 
16
27
  // src/core/components/head.ts
@@ -23,22 +34,47 @@ function Head(props) {
23
34
  context = useContext(HEAD_CONTEXT);
24
35
  } catch (err) {
25
36
  console.log(err);
26
- throw new Error("<Head> component is not supported in the browser, or during suspense renders.", { cause: err });
37
+ throw new Error(
38
+ "<Head> component is not supported in the browser, or during suspense renders.",
39
+ { cause: err }
40
+ );
27
41
  }
28
42
  context.push(props.children);
29
43
  return null;
30
44
  }
31
45
 
32
- // src/core/components/script.ts
46
+ // src/core/components/html.ts
33
47
  import { createContext as createContext2 } from "preact";
34
48
  import { useContext as useContext2 } from "preact/hooks";
35
- var SCRIPT_CONTEXT = createContext2([]);
49
+ var HTML_CONTEXT = createContext2({ attrs: {} });
50
+ function Html({ children, ...attrs }) {
51
+ let context;
52
+ try {
53
+ context = useContext2(HTML_CONTEXT);
54
+ context.attrs = attrs;
55
+ } catch (err) {
56
+ console.log(err);
57
+ throw new Error(
58
+ "<Html> component is not supported in the browser, or during suspense renders.",
59
+ { cause: err }
60
+ );
61
+ }
62
+ return children;
63
+ }
64
+
65
+ // src/core/components/script.ts
66
+ import { createContext as createContext3 } from "preact";
67
+ import { useContext as useContext3 } from "preact/hooks";
68
+ var SCRIPT_CONTEXT = createContext3([]);
36
69
  function Script(props) {
37
70
  let context;
38
71
  try {
39
- context = useContext2(SCRIPT_CONTEXT);
72
+ context = useContext3(SCRIPT_CONTEXT);
40
73
  } catch (err) {
41
- throw new Error("<Script> component is not supported in the browser, or during suspense renders.", { cause: err });
74
+ throw new Error(
75
+ "<Script> component is not supported in the browser, or during suspense renders.",
76
+ { cause: err }
77
+ );
42
78
  }
43
79
  context.push(props);
44
80
  return null;
@@ -46,9 +82,9 @@ function Script(props) {
46
82
 
47
83
  // src/core/i18n.ts
48
84
  import path from "node:path";
49
- import { createContext as createContext3 } from "preact";
50
- import { useContext as useContext3 } from "preact/hooks";
51
- var I18N_CONTEXT = createContext3(null);
85
+ import { createContext as createContext4 } from "preact";
86
+ import { useContext as useContext4 } from "preact/hooks";
87
+ var I18N_CONTEXT = createContext4(null);
52
88
  function getTranslations(locale) {
53
89
  const translations = {};
54
90
  const translationsFiles = import.meta.glob(["/translations/*.json"], {
@@ -64,12 +100,16 @@ function getTranslations(locale) {
64
100
  });
65
101
  return translations[locale] || {};
66
102
  }
67
- function useTranslations() {
68
- const context = useContext3(I18N_CONTEXT);
103
+ function useI18nContext() {
104
+ const context = useContext4(I18N_CONTEXT);
69
105
  if (!context) {
70
106
  throw new Error("could not find i18n context");
71
107
  }
72
- const translations = (context == null ? void 0 : context.translations) || {};
108
+ return context;
109
+ }
110
+ function useTranslations() {
111
+ const context = useI18nContext();
112
+ const translations = context.translations || {};
73
113
  const t = (str, params) => {
74
114
  let translation = translations[str] || str || "";
75
115
  if (params) {
@@ -83,14 +123,31 @@ function useTranslations() {
83
123
  return t;
84
124
  }
85
125
 
126
+ // src/core/request-context.ts
127
+ import { createContext as createContext5 } from "preact";
128
+ import { useContext as useContext5 } from "preact/hooks";
129
+ var REQUEST_CONTEXT = createContext5(null);
130
+ function useRequestContext() {
131
+ const context = useContext5(REQUEST_CONTEXT);
132
+ if (!context) {
133
+ throw new Error("could not find request context");
134
+ }
135
+ return context;
136
+ }
137
+
86
138
  export {
87
139
  ErrorPage,
88
140
  HEAD_CONTEXT,
89
141
  Head,
142
+ HTML_CONTEXT,
143
+ Html,
90
144
  SCRIPT_CONTEXT,
91
145
  Script,
92
146
  I18N_CONTEXT,
93
147
  getTranslations,
94
- useTranslations
148
+ useI18nContext,
149
+ useTranslations,
150
+ REQUEST_CONTEXT,
151
+ useRequestContext
95
152
  };
96
- //# sourceMappingURL=chunk-CDPH3RKE.js.map
153
+ //# sourceMappingURL=chunk-MLJ4KUD5.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/components/error-page.tsx","../src/core/components/head.ts","../src/core/components/html.ts","../src/core/components/script.ts","../src/core/i18n.ts","../src/core/request-context.ts"],"sourcesContent":["export interface ErrorPageProps {\n error: unknown;\n}\n\nexport function ErrorPage(props: ErrorPageProps) {\n const error = props.error;\n\n let message = undefined;\n if (import.meta.env.DEV) {\n if (error instanceof Error) {\n message = error.stack;\n } else {\n message = String(error);\n }\n }\n\n return (\n <div>\n <div>\n <p>An error occured during route handling or page rendering.</p>\n {message && <pre>{message}</pre>}\n </div>\n </div>\n );\n}\n","import {ComponentChildren, createContext} from 'preact';\nimport {useContext} from 'preact/hooks';\n\nexport const HEAD_CONTEXT = createContext<ComponentChildren[]>([]);\n\nexport interface HeadProps {\n children?: ComponentChildren;\n}\n\n/**\n * The <Head> component can be used for injecting elements into the HTML head\n * tag from any part of a page.\n */\nexport function Head(props: HeadProps) {\n let context: ComponentChildren[];\n try {\n context = useContext(HEAD_CONTEXT);\n } catch (err) {\n console.log(err);\n throw new Error(\n '<Head> component is not supported in the browser, or during suspense renders.',\n {cause: err}\n );\n }\n context.push(props.children);\n return null;\n}\n","import {ComponentChildren, createContext} from 'preact';\nimport {useContext} from 'preact/hooks';\n\nexport interface HtmlContextValue {\n attrs: Record<string, string>;\n}\n\nexport const HTML_CONTEXT = createContext<HtmlContextValue>({attrs: {}});\n\nexport type HtmlProps = preact.JSX.HTMLAttributes<HTMLHtmlElement> & {\n children?: ComponentChildren;\n};\n\n/**\n * The `<Html>` component can be used to update attrs used in the `<html>` tag.\n *\n * Usage:\n *\n * import {Html} from '@blinkk/root';\n *\n * export default function Page() {\n * return (\n * <Html lang={locale}>\n * <h1>Hello world</h1>\n * </Html>\n * );\n * }\n */\nexport function Html({children, ...attrs}: HtmlProps) {\n let context: Record<string, any>;\n try {\n context = useContext(HTML_CONTEXT);\n context.attrs = attrs;\n } catch (err) {\n console.log(err);\n throw new Error(\n '<Html> component is not supported in the browser, or during suspense renders.',\n {cause: err}\n );\n }\n return children;\n}\n","import {createContext} from 'preact';\nimport {useContext} from 'preact/hooks';\n\nexport const SCRIPT_CONTEXT = createContext<ScriptProps[]>([]);\n\nexport interface ScriptProps {\n src: string;\n type?: string;\n}\n\n/**\n * The <Script> component is used for rendering any custom script modules. At\n * the moment, the system only pre-renders and bundles files that are in the\n * `/bundles` folder at the root of the project.\n */\nexport function Script(props: ScriptProps) {\n let context: ScriptProps[];\n try {\n context = useContext(SCRIPT_CONTEXT);\n } catch (err) {\n throw new Error(\n '<Script> component is not supported in the browser, or during suspense renders.',\n {cause: err}\n );\n }\n context.push(props);\n return null;\n}\n","import path from 'node:path';\nimport {createContext} from 'preact';\nimport {useContext} from 'preact/hooks';\n\nexport const I18N_CONTEXT = createContext<I18nContext | null>(null);\n\nexport interface I18nContext {\n locale: string;\n translations: Record<string, string>;\n}\n\nexport function getTranslations(locale: string): Record<string, string> {\n const translations: Record<string, Record<string, string>> = {};\n const translationsFiles = import.meta.glob(['/translations/*.json'], {\n eager: true,\n }) as Record<string, {default?: Record<string, string>}>;\n Object.keys(translationsFiles).forEach((translationPath) => {\n const parts = path.parse(translationPath);\n const locale = parts.name;\n const module = translationsFiles[translationPath];\n if (module && module.default) {\n translations[locale] = module.default;\n }\n });\n return translations[locale] || {};\n}\n\nexport function useI18nContext() {\n const context = useContext(I18N_CONTEXT);\n if (!context) {\n throw new Error('could not find i18n context');\n }\n return context;\n}\n\nexport function useTranslations() {\n const context = useI18nContext();\n const translations = context.translations || {};\n const t = (str: string, params?: Record<string, string>) => {\n let translation = translations[str] || str || '';\n if (params) {\n for (const key of Object.keys(params)) {\n const val = String(params[key] || '');\n translation = translation.replaceAll(`{${key}}`, val);\n }\n }\n return translation;\n };\n return t;\n}\n","import {createContext} from 'preact';\nimport {useContext} from 'preact/hooks';\nimport {Route} from '../render/router';\n\nexport interface RequestContext {\n /** The route file. */\n route: Route;\n /**\n * Route param values. E.g. for a route like `routes/blog/[slug].tsx`,\n * visiting `/blog/foo` will pass {slug: 'foo'} here.\n */\n routeParams: Record<string, string>;\n /** Props passed to the route's server component. */\n props: any;\n /** The current locale. */\n locale: string;\n /** Translations map for the current locale. */\n translations: Record<string, string>;\n}\n\nexport const REQUEST_CONTEXT = createContext<RequestContext | null>(null);\n\nexport function useRequestContext() {\n const context = useContext(REQUEST_CONTEXT);\n if (!context) {\n throw new Error('could not find request context');\n }\n return context;\n}\n"],"mappings":";AAkBM,SACE,KADF;AAdC,SAAS,UAAU,OAAuB;AAC/C,QAAM,QAAQ,MAAM;AAEpB,MAAI,UAAU;AACd,MAAI,YAAY,IAAI,KAAK;AACvB,QAAI,iBAAiB,OAAO;AAC1B,gBAAU,MAAM;AAAA,IAClB,OAAO;AACL,gBAAU,OAAO,KAAK;AAAA,IACxB;AAAA,EACF;AAEA,SACE,oBAAC;AAAA,IACC,+BAAC;AAAA,MACC;AAAA,4BAAC;AAAA,UAAE;AAAA,SAAyD;AAAA,QAC3D,WAAW,oBAAC;AAAA,UAAK;AAAA,SAAQ;AAAA;AAAA,KAC5B;AAAA,GACF;AAEJ;;;ACxBA,SAA2B,qBAAoB;AAC/C,SAAQ,kBAAiB;AAElB,IAAM,eAAe,cAAmC,CAAC,CAAC;AAU1D,SAAS,KAAK,OAAkB;AACrC,MAAI;AACJ,MAAI;AACF,cAAU,WAAW,YAAY;AAAA,EACnC,SAAS,KAAP;AACA,YAAQ,IAAI,GAAG;AACf,UAAM,IAAI;AAAA,MACR;AAAA,MACA,EAAC,OAAO,IAAG;AAAA,IACb;AAAA,EACF;AAC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AACD,SAAO,aAAa,WAAW,CAAC;AAClC;AAEO,SAAS,iBAAiB;AAC/B,QAAM,UAAUD,YAAW,YAAY;AACvC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,6BAA6B;AAAA,EAC/C;AACA,SAAO;AACT;AAEO,SAAS,kBAAkB;AAChC,QAAM,UAAU,eAAe;AAC/B,QAAM,eAAe,QAAQ,gBAAgB,CAAC;AAC9C,QAAM,IAAI,CAAC,KAAa,WAAoC;AAC1D,QAAI,cAAc,aAAa,QAAQ,OAAO;AAC9C,QAAI,QAAQ;AACV,iBAAW,OAAO,OAAO,KAAK,MAAM,GAAG;AACrC,cAAM,MAAM,OAAO,OAAO,QAAQ,EAAE;AACpC,sBAAc,YAAY,WAAW,IAAI,QAAQ,GAAG;AAAA,MACtD;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;;;ACjDA,SAAQ,iBAAAE,sBAAoB;AAC5B,SAAQ,cAAAC,mBAAiB;AAmBlB,IAAM,kBAAkBD,eAAqC,IAAI;AAEjE,SAAS,oBAAoB;AAClC,QAAM,UAAUC,YAAW,eAAe;AAC1C,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,gCAAgC;AAAA,EAClD;AACA,SAAO;AACT;","names":["createContext","useContext","createContext","useContext","createContext","useContext","locale","createContext","useContext"]}
@@ -0,0 +1,31 @@
1
+ // src/core/plugin.ts
2
+ async function configureServerPlugins(server, callback, plugins, options) {
3
+ const postHooks = [];
4
+ for (const plugin of plugins) {
5
+ if (plugin.configureServer) {
6
+ const postHook = await plugin.configureServer(server, options);
7
+ if (postHook) {
8
+ postHooks.push(postHook);
9
+ }
10
+ }
11
+ }
12
+ callback();
13
+ for (const postHook of postHooks) {
14
+ await postHook();
15
+ }
16
+ }
17
+ function getVitePlugins(plugins) {
18
+ const vitePlugins = [];
19
+ for (const plugin of plugins) {
20
+ if (plugin.vitePlugins) {
21
+ vitePlugins.push(...plugin.vitePlugins);
22
+ }
23
+ }
24
+ return vitePlugins;
25
+ }
26
+
27
+ export {
28
+ configureServerPlugins,
29
+ getVitePlugins
30
+ };
31
+ //# sourceMappingURL=chunk-ZB7R6ZOY.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/plugin.ts"],"sourcesContent":["import {PluginOption as VitePlugin} from 'vite';\nimport {Server} from './types';\n\ntype MaybePromise<T> = T | Promise<T>;\n\nexport type ConfigureServerHook = (\n server: Server,\n options: ConfigureServerOptions\n) => MaybePromise<void> | MaybePromise<() => void>;\n\nexport interface ConfigureServerOptions {\n type: 'dev' | 'preview' | 'prod';\n}\n\nexport interface Plugin {\n name?: string;\n\n /** Configures the root.js express server . */\n configureServer?: ConfigureServerHook;\n\n /** Adds vite plugins. */\n vitePlugins?: VitePlugin[];\n}\n\n/**\n * Runs the pre-hook configureServer method of every plugin, calls a callback\n * function, and then runs the configureServer's post-hook if provided. Plugins\n * provide a post-hook by returning a callback function from configureServer.\n */\nexport async function configureServerPlugins(\n server: Server,\n callback: () => Promise<void>,\n plugins: Plugin[],\n options: ConfigureServerOptions\n) {\n const postHooks: Array<() => void> = [];\n\n // Call the `configureServer()` method for each plugin.\n for (const plugin of plugins) {\n if (plugin.configureServer) {\n const postHook = await plugin.configureServer(server, options);\n if (postHook) {\n postHooks.push(postHook);\n }\n }\n }\n\n // Register any built-in middleware.\n callback();\n\n // Run any post hooks returned by `plugin.configureServer()`.\n for (const postHook of postHooks) {\n await postHook();\n }\n}\n\nexport function getVitePlugins(plugins: Plugin[]): VitePlugin[] {\n const vitePlugins: VitePlugin[] = [];\n for (const plugin of plugins) {\n if (plugin.vitePlugins) {\n vitePlugins.push(...plugin.vitePlugins);\n }\n }\n return vitePlugins;\n}\n"],"mappings":";AA6BA,eAAsB,uBACpB,QACA,UACA,SACA,SACA;AACA,QAAM,YAA+B,CAAC;AAGtC,aAAW,UAAU,SAAS;AAC5B,QAAI,OAAO,iBAAiB;AAC1B,YAAM,WAAW,MAAM,OAAO,gBAAgB,QAAQ,OAAO;AAC7D,UAAI,UAAU;AACZ,kBAAU,KAAK,QAAQ;AAAA,MACzB;AAAA,IACF;AAAA,EACF;AAGA,WAAS;AAGT,aAAW,YAAY,WAAW;AAChC,UAAM,SAAS;AAAA,EACjB;AACF;AAEO,SAAS,eAAe,SAAiC;AAC9D,QAAM,cAA4B,CAAC;AACnC,aAAW,UAAU,SAAS;AAC5B,QAAI,OAAO,aAAa;AACtB,kBAAY,KAAK,GAAG,OAAO,WAAW;AAAA,IACxC;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
package/dist/cli.d.ts CHANGED
@@ -1,7 +1,13 @@
1
- declare function dev(rootDir?: string): void;
1
+ declare function dev(rootProjectDir?: string): Promise<void>;
2
2
 
3
- declare function build(rootDir?: string): Promise<void>;
3
+ interface BuildOptions {
4
+ ssrOnly?: boolean;
5
+ mode?: string;
6
+ }
7
+ declare function build(rootProjectDir?: string, options?: BuildOptions): Promise<void>;
4
8
 
5
- declare function start(): Promise<void>;
9
+ declare function preview(rootProjectDir?: string): Promise<void>;
6
10
 
7
- export { build, dev, start };
11
+ declare function start(rootProjectDir?: string): Promise<void>;
12
+
13
+ export { build, dev, preview, start };