@blinkk/root 1.0.0-alpha.2 → 1.0.0-alpha.20

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,7 +34,10 @@ 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;
@@ -38,7 +52,10 @@ function Script(props) {
38
52
  try {
39
53
  context = useContext2(SCRIPT_CONTEXT);
40
54
  } catch (err) {
41
- throw new Error("<Script> component is not supported in the browser, or during suspense renders.", { cause: err });
55
+ throw new Error(
56
+ "<Script> component is not supported in the browser, or during suspense renders.",
57
+ { cause: err }
58
+ );
42
59
  }
43
60
  context.push(props);
44
61
  return null;
@@ -49,21 +66,6 @@ import path from "node:path";
49
66
  import { createContext as createContext3 } from "preact";
50
67
  import { useContext as useContext3 } from "preact/hooks";
51
68
  var I18N_CONTEXT = createContext3(null);
52
- function t(str, params) {
53
- const context = useContext3(I18N_CONTEXT);
54
- if (!context) {
55
- throw new Error("could not find i18n context");
56
- }
57
- const translations = (context == null ? void 0 : context.translations) || {};
58
- let translation = translations[str] || str;
59
- if (params) {
60
- for (const key in params) {
61
- const val = String(params[key] || "");
62
- translation = translation.replaceAll(`{${key}}`, val);
63
- }
64
- }
65
- return translation;
66
- }
67
69
  function getTranslations(locale) {
68
70
  const translations = {};
69
71
  const translationsFiles = import.meta.glob(["/translations/*.json"], {
@@ -79,6 +81,32 @@ function getTranslations(locale) {
79
81
  });
80
82
  return translations[locale] || {};
81
83
  }
84
+ function useTranslations() {
85
+ const context = useContext3(I18N_CONTEXT);
86
+ if (!context) {
87
+ throw new Error("could not find i18n context");
88
+ }
89
+ const translations = (context == null ? void 0 : context.translations) || {};
90
+ const t = (str, params) => {
91
+ let translation = translations[str] || str || "";
92
+ if (params) {
93
+ for (const key of Object.keys(params)) {
94
+ const val = String(params[key] || "");
95
+ translation = translation.replaceAll(`{${key}}`, val);
96
+ }
97
+ }
98
+ return translation;
99
+ };
100
+ return t;
101
+ }
102
+
103
+ // src/core/context.ts
104
+ import { createContext as createContext4 } from "preact";
105
+ import { useContext as useContext4 } from "preact/hooks";
106
+ var REQUEST_CONTEXT = createContext4(null);
107
+ function useRequestContext() {
108
+ return useContext4(REQUEST_CONTEXT);
109
+ }
82
110
 
83
111
  export {
84
112
  ErrorPage,
@@ -87,7 +115,9 @@ export {
87
115
  SCRIPT_CONTEXT,
88
116
  Script,
89
117
  I18N_CONTEXT,
90
- t,
91
- getTranslations
118
+ getTranslations,
119
+ useTranslations,
120
+ REQUEST_CONTEXT,
121
+ useRequestContext
92
122
  };
93
- //# sourceMappingURL=chunk-4SVK7R4Z.js.map
123
+ //# sourceMappingURL=chunk-X6ZHRE6O.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/components/error-page.tsx","../src/core/components/head.ts","../src/core/components/script.ts","../src/core/i18n.ts","../src/core/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 {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 useTranslations() {\n const context = useContext(I18N_CONTEXT);\n if (!context) {\n throw new Error('could not find i18n context');\n }\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 route: Route;\n}\n\nexport const REQUEST_CONTEXT = createContext<RequestContext | null>(null);\n\nexport function useRequestContext() {\n return useContext(REQUEST_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;AACA,UAAQ,KAAK,MAAM,QAAQ;AAC3B,SAAO;AACT;;;AC1BA,SAAQ,iBAAAA,sBAAoB;AAC5B,SAAQ,cAAAC,mBAAiB;AAElB,IAAM,iBAAiBD,eAA6B,CAAC,CAAC;AA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,cAAAC,mBAAiB;AAOlB,IAAM,kBAAkBD,eAAqC,IAAI;AAEjE,SAAS,oBAAoB;AAClC,SAAOC,YAAW,eAAe;AACnC;","names":["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(rootDir?: string): 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 };