@eserstack/laroux-server 0.0.0-stage → 5.0.0
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/LICENSE +13 -0
- package/README.md +410 -2
- package/dist/adapters/react/error-chunk.d.ts +11 -0
- package/dist/adapters/react/error-chunk.js +12 -0
- package/dist/adapters/react/html-shell.d.ts +15 -0
- package/dist/adapters/react/html-shell.js +109 -0
- package/dist/adapters/react/inline-rsc-emitter.d.ts +62 -0
- package/dist/adapters/react/inline-rsc-emitter.js +120 -0
- package/dist/adapters/react/mod.d.ts +30 -0
- package/dist/adapters/react/mod.js +36 -0
- package/dist/adapters/react/render-app.d.ts +46 -0
- package/dist/adapters/react/render-app.js +44 -0
- package/dist/adapters/react/renderer.d.ts +15 -0
- package/dist/adapters/react/renderer.js +86 -0
- package/dist/adapters/react/rsc-flight-renderer.d.ts +64 -0
- package/dist/adapters/react/rsc-flight-renderer.js +623 -0
- package/dist/adapters/react/rsc-handler.d.ts +47 -0
- package/dist/adapters/react/rsc-handler.js +114 -0
- package/dist/adapters/react/script-json.d.ts +12 -0
- package/dist/adapters/react/script-json.js +18 -0
- package/dist/adapters/react/ssr-renderer.d.ts +63 -0
- package/dist/adapters/react/ssr-renderer.js +625 -0
- package/dist/bin-main.d.ts +1 -0
- package/dist/bin-main.js +18 -0
- package/dist/cli.d.ts +26 -0
- package/dist/cli.js +48 -0
- package/dist/commands/build.d.ts +78 -0
- package/dist/commands/build.js +221 -0
- package/dist/commands/dev.d.ts +50 -0
- package/dist/commands/dev.js +133 -0
- package/dist/commands/init.d.ts +29 -0
- package/dist/commands/init.js +228 -0
- package/dist/commands/serve.d.ts +47 -0
- package/dist/commands/serve.js +124 -0
- package/dist/config/load-config.d.ts +14 -0
- package/dist/config/load-config.js +67 -0
- package/dist/domain/action-registry.d.ts +83 -0
- package/dist/domain/action-registry.js +124 -0
- package/dist/domain/build-module.d.ts +30 -0
- package/dist/domain/build-module.js +50 -0
- package/dist/domain/hmr-manager.d.ts +51 -0
- package/dist/domain/hmr-manager.js +90 -0
- package/dist/domain/html-shell.d.ts +56 -0
- package/dist/domain/html-shell.js +15 -0
- package/dist/domain/middleware-dispatcher.d.ts +42 -0
- package/dist/domain/middleware-dispatcher.js +150 -0
- package/dist/domain/rebuild-handler.d.ts +52 -0
- package/dist/domain/rebuild-handler.js +52 -0
- package/dist/domain/renderer.d.ts +62 -0
- package/dist/domain/renderer.js +15 -0
- package/dist/domain/route-dispatcher.d.ts +40 -0
- package/dist/domain/route-dispatcher.js +134 -0
- package/dist/domain/server-modules.d.ts +65 -0
- package/dist/domain/server-modules.js +208 -0
- package/dist/main.d.ts +40 -0
- package/dist/main.js +256 -0
- package/dist/module.d.ts +24 -0
- package/dist/module.js +140 -0
- package/dist/options.d.ts +53 -0
- package/dist/options.js +14 -0
- package/dist/package.json +98 -0
- package/dist/proxy/types.d.ts +19 -0
- package/dist/proxy/types.js +3 -0
- package/dist/runtime/action-guard.d.ts +49 -0
- package/dist/runtime/action-guard.js +115 -0
- package/dist/runtime/html-shell.d.ts +66 -0
- package/dist/runtime/html-shell.js +246 -0
- package/dist/runtime/media-types.d.ts +13 -0
- package/dist/runtime/media-types.js +83 -0
- package/dist/runtime/path-containment.d.ts +11 -0
- package/dist/runtime/path-containment.js +27 -0
- package/dist/runtime/seo/generate-meta.d.ts +16 -0
- package/dist/runtime/seo/generate-meta.js +121 -0
- package/dist/runtime/seo/index.d.ts +5 -0
- package/dist/runtime/seo/index.js +5 -0
- package/dist/runtime/seo/types.d.ts +25 -0
- package/dist/runtime/seo/types.js +5 -0
- package/dist/runtime/server.d.ts +58 -0
- package/dist/runtime/server.js +699 -0
- package/package.json +81 -3
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* laroux init command
|
|
4
|
+
*
|
|
5
|
+
* Creates a new laroux.js project from a project recipe of a kit registry,
|
|
6
|
+
* the same recipes `eser kit new` scaffolds. Without `--registry` it reads
|
|
7
|
+
* eser/stack's registry on GitHub, the default registry of kit.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import * as shellArgs from "@eserstack/shell/args";
|
|
12
|
+
import * as span from "@eserstack/streams/span";
|
|
13
|
+
import * as streams from "@eserstack/streams";
|
|
14
|
+
import * as results from "@eserstack/primitives/results";
|
|
15
|
+
import * as task from "@eserstack/functions/task";
|
|
16
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
17
|
+
import * as registryFetcher from "@eserstack/kit/recipes/fetcher";
|
|
18
|
+
import * as newProjectHandler from "@eserstack/kit/recipes/handlers/new-project";
|
|
19
|
+
/**
|
|
20
|
+
* The templates `laroux init --template` accepts, each with the name of the
|
|
21
|
+
* kit recipe it scaffolds.
|
|
22
|
+
*/
|
|
23
|
+
export const TEMPLATE_RECIPES = {
|
|
24
|
+
minimal: "laroux-app",
|
|
25
|
+
};
|
|
26
|
+
/** The folder init creates the project in when none is given. */
|
|
27
|
+
export const DEFAULT_FOLDER = "my-laroux-app";
|
|
28
|
+
const DEFAULT_TEMPLATE = "minimal";
|
|
29
|
+
const FLAGS = [
|
|
30
|
+
{
|
|
31
|
+
name: "template",
|
|
32
|
+
short: "t",
|
|
33
|
+
type: "string",
|
|
34
|
+
default: DEFAULT_TEMPLATE,
|
|
35
|
+
description: `Project template (${Object.keys(TEMPLATE_RECIPES).join(", ")})`,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: "registry",
|
|
39
|
+
type: "string",
|
|
40
|
+
description: "Path or URL of a kit recipes.json (default: eser/stack's registry on GitHub)",
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "force",
|
|
44
|
+
short: "f",
|
|
45
|
+
type: "boolean",
|
|
46
|
+
description: "Write the template over a folder that is not empty",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: "no-git",
|
|
50
|
+
type: "boolean",
|
|
51
|
+
description: "Skip the template's post-install commands, as --no-install",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: "no-install",
|
|
55
|
+
type: "boolean",
|
|
56
|
+
description: "Skip the template's post-install commands",
|
|
57
|
+
},
|
|
58
|
+
{ name: "help", short: "h", type: "boolean", description: "Show this help" },
|
|
59
|
+
];
|
|
60
|
+
/**
|
|
61
|
+
* The help text of `laroux init`.
|
|
62
|
+
*
|
|
63
|
+
* @returns The usage line, the options and examples
|
|
64
|
+
*/
|
|
65
|
+
export const helpText = () => shellArgs.generateHelp({
|
|
66
|
+
name: "init",
|
|
67
|
+
description: "Create a new laroux.js project from a kit recipe",
|
|
68
|
+
usage: "laroux init [folder] [options]",
|
|
69
|
+
flags: FLAGS,
|
|
70
|
+
children: [],
|
|
71
|
+
examples: [
|
|
72
|
+
"laroux init my-app",
|
|
73
|
+
"laroux init my-app --registry ./.eser/recipes.json --no-install",
|
|
74
|
+
],
|
|
75
|
+
}, ["laroux", "init"]);
|
|
76
|
+
/**
|
|
77
|
+
* The topmost directory of `path` and its parents that does not exist yet,
|
|
78
|
+
* which is what creating `path` creates; null when `path` exists.
|
|
79
|
+
*/
|
|
80
|
+
const firstMissingAncestor = async (path) => {
|
|
81
|
+
let missing = null;
|
|
82
|
+
let dir = path;
|
|
83
|
+
// Each check decides whether the parent is checked next, so they run in
|
|
84
|
+
// order, from the project folder up.
|
|
85
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
86
|
+
while (!(await runtime.fs.exists(dir))) {
|
|
87
|
+
missing = dir;
|
|
88
|
+
const parent = runtime.path.dirname(dir);
|
|
89
|
+
if (parent === dir) {
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
dir = parent;
|
|
93
|
+
}
|
|
94
|
+
return missing;
|
|
95
|
+
};
|
|
96
|
+
const isEmptyDir = async (dir) => {
|
|
97
|
+
for await (const _entry of runtime.fs.readDir(dir)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Remove the directory init created for a project whose scaffolding failed.
|
|
104
|
+
*
|
|
105
|
+
* @returns A line for the failure message: what was removed, or what to
|
|
106
|
+
* remove by hand
|
|
107
|
+
*/
|
|
108
|
+
const removeCreated = async (created) => {
|
|
109
|
+
try {
|
|
110
|
+
await runtime.fs.remove(created, { recursive: true });
|
|
111
|
+
return `Removed ${created}, which laroux init created.`;
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
115
|
+
return `Could not remove ${created}, which laroux init created (${reason}); remove it by hand.`;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const runInit = async (options, out, renderer) => {
|
|
119
|
+
const fail = (lines) => results.fail({
|
|
120
|
+
message: renderer.render([span.red(`\n${lines.join("\n")}`)]),
|
|
121
|
+
exitCode: 1,
|
|
122
|
+
});
|
|
123
|
+
const recipeName = Object.hasOwn(TEMPLATE_RECIPES, options.template)
|
|
124
|
+
? TEMPLATE_RECIPES[options.template]
|
|
125
|
+
: undefined;
|
|
126
|
+
if (recipeName === undefined) {
|
|
127
|
+
return fail([
|
|
128
|
+
`laroux init: no template named "${options.template}".`,
|
|
129
|
+
`Available templates: ${Object.keys(TEMPLATE_RECIPES).join(", ")}`,
|
|
130
|
+
]);
|
|
131
|
+
}
|
|
132
|
+
const targetDir = runtime.path.resolve(options.cwd, options.folder);
|
|
133
|
+
const projectName = runtime.path.basename(targetDir);
|
|
134
|
+
const created = await firstMissingAncestor(targetDir);
|
|
135
|
+
if (created === null) {
|
|
136
|
+
const info = await runtime.fs.stat(targetDir);
|
|
137
|
+
if (!info.isDirectory) {
|
|
138
|
+
return fail([
|
|
139
|
+
`laroux init: ${options.folder} exists and is not a directory; choose another folder.`,
|
|
140
|
+
]);
|
|
141
|
+
}
|
|
142
|
+
if (!options.force && !(await isEmptyDir(targetDir))) {
|
|
143
|
+
return fail([
|
|
144
|
+
`laroux init: ${options.folder} is not empty; pass --force to write the template over its files.`,
|
|
145
|
+
]);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
out.writeln(span.cyan(`\n✨ Creating new laroux.js project in ${options.folder}\n`));
|
|
149
|
+
out.writeln(span.dim(` Template: ${options.template} (recipe ${recipeName})`));
|
|
150
|
+
out.writeln(span.dim(` Registry: ${options.registrySource}\n`));
|
|
151
|
+
const handlerResult = await task
|
|
152
|
+
.runTask(newProjectHandler.newProject({
|
|
153
|
+
templateName: recipeName,
|
|
154
|
+
projectName,
|
|
155
|
+
targetDir,
|
|
156
|
+
registrySource: options.registrySource,
|
|
157
|
+
variables: { project_name: projectName },
|
|
158
|
+
// The TypeScript applier reads the files of a registry on disk from
|
|
159
|
+
// its directory; the native one still reads them from its
|
|
160
|
+
// registryUrl (TASK-362). It prompts only for a variable without a
|
|
161
|
+
// value, and project_name has one.
|
|
162
|
+
interactive: true,
|
|
163
|
+
skipPostInstall: options.skipPostInstall,
|
|
164
|
+
}), { out })
|
|
165
|
+
.catch(async (error) => {
|
|
166
|
+
if (created !== null) {
|
|
167
|
+
await removeCreated(created);
|
|
168
|
+
}
|
|
169
|
+
throw error;
|
|
170
|
+
});
|
|
171
|
+
if (results.isFail(handlerResult)) {
|
|
172
|
+
// The handler's message names the failed step and its whole cause chain,
|
|
173
|
+
// so it is printed without a prefix.
|
|
174
|
+
const lines = [handlerResult.error.message];
|
|
175
|
+
if (created !== null) {
|
|
176
|
+
lines.push(await removeCreated(created));
|
|
177
|
+
}
|
|
178
|
+
return fail(lines);
|
|
179
|
+
}
|
|
180
|
+
out.writeln(span.green(`\n🎉 Project created successfully!`));
|
|
181
|
+
out.writeln();
|
|
182
|
+
out.writeln(span.bold("Start the development server:"));
|
|
183
|
+
out.writeln();
|
|
184
|
+
out.writeln(span.text(` cd ${options.folder}`));
|
|
185
|
+
out.writeln(span.text(" npm install"));
|
|
186
|
+
out.writeln(span.text(" npm run dev"));
|
|
187
|
+
out.writeln();
|
|
188
|
+
out.writeln(span.text("Then open "), span.cyan("http://localhost:8000"), span.text(" in your browser."));
|
|
189
|
+
out.writeln();
|
|
190
|
+
out.writeln(span.dim("Learn more at https://laroux.now/"));
|
|
191
|
+
out.writeln();
|
|
192
|
+
return results.ok(undefined);
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* Run `laroux init [folder] [options]`: scaffold the template's kit recipe
|
|
196
|
+
* into `folder` (default `my-laroux-app`) under the working directory. A
|
|
197
|
+
* failed run removes the folder when it created it.
|
|
198
|
+
*
|
|
199
|
+
* @param args - The arguments after `init`
|
|
200
|
+
* @returns ok when the project was created or the help was printed;
|
|
201
|
+
* otherwise the message to print and exit code 1
|
|
202
|
+
*/
|
|
203
|
+
export const main = async (args) => {
|
|
204
|
+
const { positional, flags } = shellArgs.parseFlags(args ?? [], FLAGS);
|
|
205
|
+
const renderer = streams.renderers.ansi();
|
|
206
|
+
const out = streams.output({
|
|
207
|
+
renderer,
|
|
208
|
+
sink: streams.sinks.stdout(),
|
|
209
|
+
});
|
|
210
|
+
try {
|
|
211
|
+
if (flags["help"] === true) {
|
|
212
|
+
out.writeln(helpText());
|
|
213
|
+
return results.ok(undefined);
|
|
214
|
+
}
|
|
215
|
+
return await runInit({
|
|
216
|
+
folder: positional[0] ?? DEFAULT_FOLDER,
|
|
217
|
+
template: flags["template"] ?? DEFAULT_TEMPLATE,
|
|
218
|
+
cwd: runtime.process.cwd(),
|
|
219
|
+
registrySource: flags["registry"] ??
|
|
220
|
+
registryFetcher.DEFAULT_REGISTRY_URL,
|
|
221
|
+
force: flags["force"] === true,
|
|
222
|
+
skipPostInstall: flags["no-install"] === true || flags["no-git"] === true,
|
|
223
|
+
}, out, renderer);
|
|
224
|
+
}
|
|
225
|
+
finally {
|
|
226
|
+
await out.close();
|
|
227
|
+
}
|
|
228
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as streams from "@eserstack/streams";
|
|
2
|
+
import * as results from "@eserstack/primitives/results";
|
|
3
|
+
declare const VALID_LOG_LEVELS: readonly ["trace", "debug", "info", "warn", "error", "fatal"];
|
|
4
|
+
/** Lowest level the server logs. */
|
|
5
|
+
export type LogLevel = (typeof VALID_LOG_LEVELS)[number];
|
|
6
|
+
type CliResult = results.Result<void, {
|
|
7
|
+
message?: string;
|
|
8
|
+
exitCode: number;
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* The help text of `laroux serve`.
|
|
12
|
+
*
|
|
13
|
+
* @returns The usage line, the options and examples
|
|
14
|
+
*/
|
|
15
|
+
export declare const helpText: () => string;
|
|
16
|
+
/** The production server inputs, after the flags are parsed. */
|
|
17
|
+
export type ServeOptions = {
|
|
18
|
+
/** Port the server listens on */
|
|
19
|
+
readonly port: number | undefined;
|
|
20
|
+
/** Lowest level the server logs */
|
|
21
|
+
readonly logLevel: LogLevel;
|
|
22
|
+
};
|
|
23
|
+
/** The parts of `laroux serve` a test replaces; each defaults to the real one. */
|
|
24
|
+
export type ServeCommandDeps = {
|
|
25
|
+
/** Starts the server; defaults to {@link startProductionServer} */
|
|
26
|
+
readonly start?: (options: ServeOptions) => Promise<CliResult>;
|
|
27
|
+
/** Where the command writes its own lines; defaults to stdout */
|
|
28
|
+
readonly sink?: streams.Sink<unknown>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Serve the production build of the app in the working directory with the
|
|
32
|
+
* React, Tailwind, Lightning CSS, Google Fonts and sharp adapters.
|
|
33
|
+
*
|
|
34
|
+
* @param options - The parsed flags
|
|
35
|
+
* @returns ok when the server stopped; exit code 1 when its build failed
|
|
36
|
+
*/
|
|
37
|
+
export declare const startProductionServer: (options: ServeOptions) => Promise<CliResult>;
|
|
38
|
+
/**
|
|
39
|
+
* Run `laroux serve [options]`: serve the production build of the app in the
|
|
40
|
+
* working directory.
|
|
41
|
+
*
|
|
42
|
+
* @param args - The arguments after `serve`
|
|
43
|
+
* @param deps - Replacements for the server start and the output
|
|
44
|
+
* @returns The result of the server start
|
|
45
|
+
*/
|
|
46
|
+
export declare const main: (args?: readonly string[], deps?: ServeCommandDeps) => Promise<CliResult>;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* laroux serve command
|
|
4
|
+
*
|
|
5
|
+
* Serves the production build locally.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import * as shellArgs from "@eserstack/shell/args";
|
|
10
|
+
import * as span from "@eserstack/streams/span";
|
|
11
|
+
import * as streams from "@eserstack/streams";
|
|
12
|
+
import * as results from "@eserstack/primitives/results";
|
|
13
|
+
const VALID_LOG_LEVELS = [
|
|
14
|
+
"trace",
|
|
15
|
+
"debug",
|
|
16
|
+
"info",
|
|
17
|
+
"warn",
|
|
18
|
+
"error",
|
|
19
|
+
"fatal",
|
|
20
|
+
];
|
|
21
|
+
const FLAGS = [
|
|
22
|
+
{
|
|
23
|
+
name: "port",
|
|
24
|
+
short: "p",
|
|
25
|
+
type: "number",
|
|
26
|
+
default: 8000,
|
|
27
|
+
description: "Server port",
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: "log-level",
|
|
31
|
+
type: "string",
|
|
32
|
+
default: "info",
|
|
33
|
+
description: `Log level (${VALID_LOG_LEVELS.join(", ")})`,
|
|
34
|
+
},
|
|
35
|
+
{ name: "help", short: "h", type: "boolean", description: "Show this help" },
|
|
36
|
+
];
|
|
37
|
+
/**
|
|
38
|
+
* The help text of `laroux serve`.
|
|
39
|
+
*
|
|
40
|
+
* @returns The usage line, the options and examples
|
|
41
|
+
*/
|
|
42
|
+
export const helpText = () => shellArgs.generateHelp({
|
|
43
|
+
name: "serve",
|
|
44
|
+
description: "Serve the production build locally",
|
|
45
|
+
usage: "laroux serve [options]",
|
|
46
|
+
flags: FLAGS,
|
|
47
|
+
children: [],
|
|
48
|
+
examples: ["laroux serve", "laroux serve --port 3000"],
|
|
49
|
+
}, ["laroux", "serve"]);
|
|
50
|
+
/**
|
|
51
|
+
* Serve the production build of the app in the working directory with the
|
|
52
|
+
* React, Tailwind, Lightning CSS, Google Fonts and sharp adapters.
|
|
53
|
+
*
|
|
54
|
+
* @param options - The parsed flags
|
|
55
|
+
* @returns ok when the server stopped; exit code 1 when its build failed
|
|
56
|
+
*/
|
|
57
|
+
export const startProductionServer = async (options) => {
|
|
58
|
+
const [server, reactAdapter, reactBundler, tailwind, lightningcss, fonts, sharp,] = await Promise.all([
|
|
59
|
+
import("../main.js"),
|
|
60
|
+
import("../adapters/react/mod.js"),
|
|
61
|
+
import("@eserstack/laroux-bundler/adapters/react"),
|
|
62
|
+
import("@eserstack/laroux-bundler/adapters/tailwindcss"),
|
|
63
|
+
import("@eserstack/laroux-bundler/adapters/lightningcss"),
|
|
64
|
+
import("@eserstack/laroux-bundler/adapters/fonts"),
|
|
65
|
+
import("@eserstack/laroux-bundler/adapters/sharp"),
|
|
66
|
+
]);
|
|
67
|
+
try {
|
|
68
|
+
await server.startServer({
|
|
69
|
+
mode: "serve",
|
|
70
|
+
port: options.port,
|
|
71
|
+
logLevel: options.logLevel,
|
|
72
|
+
hmr: false,
|
|
73
|
+
renderer: reactAdapter.reactRenderer,
|
|
74
|
+
htmlShell: reactAdapter.reactHtmlShellBuilder,
|
|
75
|
+
frameworkPlugin: reactBundler.reactPlugin,
|
|
76
|
+
cssPlugin: tailwind.createTailwindPlugin({
|
|
77
|
+
globalCssPath: "src/app/styles/global.css",
|
|
78
|
+
}),
|
|
79
|
+
cssProcessor: lightningcss.lightningCssProcessor,
|
|
80
|
+
fontOptimizer: fonts.googleFontOptimizer,
|
|
81
|
+
imageOptimizer: sharp.sharpImageOptimizer,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
// The build already logged its error; the CLI prints this summary and
|
|
86
|
+
// exits non-zero instead of serving a stale or empty dist/.
|
|
87
|
+
if (error instanceof server.BuildFailedError) {
|
|
88
|
+
return results.fail({ message: error.message, exitCode: 1 });
|
|
89
|
+
}
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
return results.ok(undefined);
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Run `laroux serve [options]`: serve the production build of the app in the
|
|
96
|
+
* working directory.
|
|
97
|
+
*
|
|
98
|
+
* @param args - The arguments after `serve`
|
|
99
|
+
* @param deps - Replacements for the server start and the output
|
|
100
|
+
* @returns The result of the server start
|
|
101
|
+
*/
|
|
102
|
+
export const main = async (args, deps = {}) => {
|
|
103
|
+
const { flags } = shellArgs.parseFlags(args ?? [], FLAGS);
|
|
104
|
+
const out = streams.output({
|
|
105
|
+
renderer: streams.renderers.ansi(),
|
|
106
|
+
sink: deps.sink ?? streams.sinks.stdout(),
|
|
107
|
+
});
|
|
108
|
+
if (flags["help"] === true) {
|
|
109
|
+
out.writeln(helpText());
|
|
110
|
+
await out.close();
|
|
111
|
+
return results.ok(undefined);
|
|
112
|
+
}
|
|
113
|
+
out.writeln(span.cyan("\n🚀 Serving production build...\n"));
|
|
114
|
+
await out.close();
|
|
115
|
+
const logLevelInput = flags["log-level"] ?? "info";
|
|
116
|
+
const logLevel = VALID_LOG_LEVELS.includes(logLevelInput.toLowerCase())
|
|
117
|
+
? logLevelInput.toLowerCase()
|
|
118
|
+
: "info";
|
|
119
|
+
const start = deps.start ?? startProductionServer;
|
|
120
|
+
return await start({
|
|
121
|
+
port: flags["port"],
|
|
122
|
+
logLevel,
|
|
123
|
+
});
|
|
124
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration Loader
|
|
3
|
+
* Loads and merges configuration from multiple sources:
|
|
4
|
+
* 1. Built-in defaults (from defaults.ts - single source of truth)
|
|
5
|
+
* 2. User config file (laroux.config.ts)
|
|
6
|
+
* 3. CLI options (passed in by caller)
|
|
7
|
+
*/
|
|
8
|
+
import type { AppConfig, LogLevel } from "@eserstack/laroux/config";
|
|
9
|
+
export type { AppConfig, LogLevel };
|
|
10
|
+
export type { FontDefinition, FontDisplay, FontProvider, FontStyle, FontWeight, ImageFormat, ImageOutputFormat, ImagePlaceholder, ResolvedBrowserShimsConfig, ResolvedBuildConfig, ResolvedCriticalCssConfig, ResolvedCriticalCssViewport, ResolvedImageConfig, ResolvedImageQuality, ResolvedInternalConfig, ResolvedModeConfig, ResolvedServerConfig, ResolvedSSRConfig, SSRMode, SSRStreamMode, } from "@eserstack/laroux/config";
|
|
11
|
+
/**
|
|
12
|
+
* Load complete configuration from all sources
|
|
13
|
+
*/
|
|
14
|
+
export declare function loadConfig(projectRoot?: string): Promise<AppConfig>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Configuration Loader
|
|
4
|
+
* Loads and merges configuration from multiple sources:
|
|
5
|
+
* 1. Built-in defaults (from defaults.ts - single source of truth)
|
|
6
|
+
* 2. User config file (laroux.config.ts)
|
|
7
|
+
* 3. CLI options (passed in by caller)
|
|
8
|
+
*/
|
|
9
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
10
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
11
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
12
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return path;
|
|
16
|
+
};
|
|
17
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
18
|
+
import * as logging from "@eserstack/logging";
|
|
19
|
+
import * as deepMerge from "@eserstack/fp/deep-merge";
|
|
20
|
+
import * as nodeUrl from "node:url";
|
|
21
|
+
import * as config from "@eserstack/laroux/config";
|
|
22
|
+
const configLogger = logging.logger.getLogger(["laroux-server", "config"]);
|
|
23
|
+
/**
|
|
24
|
+
* Convert a filesystem path to a proper file:// URL with `pathToFileURL` from
|
|
25
|
+
* `node:url`, which handles both Unix and Windows paths.
|
|
26
|
+
*/
|
|
27
|
+
function pathToFileUrl(filePath) {
|
|
28
|
+
// Ensure the path is absolute
|
|
29
|
+
const absolutePath = runtime.path.isAbsolute(filePath)
|
|
30
|
+
? filePath
|
|
31
|
+
: runtime.path.resolve(filePath);
|
|
32
|
+
return nodeUrl.pathToFileURL(absolutePath).href;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Load user configuration file if it exists
|
|
36
|
+
*/
|
|
37
|
+
async function loadUserConfig(projectRoot) {
|
|
38
|
+
const configPath = runtime.path.join(projectRoot, "laroux.config.ts");
|
|
39
|
+
if (await runtime.fs.exists(configPath)) {
|
|
40
|
+
try {
|
|
41
|
+
// Dynamic import the config file using proper file:// URL
|
|
42
|
+
const fileUrl = pathToFileUrl(configPath);
|
|
43
|
+
const mod = await import(__rewriteRelativeImportExtension(fileUrl));
|
|
44
|
+
return mod.default ?? {};
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
configLogger
|
|
48
|
+
.with({ error })
|
|
49
|
+
.warn(`load ${configPath}; using the default configuration`);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Load complete configuration from all sources
|
|
57
|
+
*/
|
|
58
|
+
export async function loadConfig(projectRoot) {
|
|
59
|
+
const root = projectRoot ?? runtime.process.cwd();
|
|
60
|
+
// Load user config and merge with defaults
|
|
61
|
+
const userConfig = await loadUserConfig(root);
|
|
62
|
+
// Return with project root
|
|
63
|
+
return {
|
|
64
|
+
...deepMerge.deepMerge(config.DEFAULT_CONFIG, userConfig),
|
|
65
|
+
projectRoot: root,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server Action Registry
|
|
3
|
+
* Manages registration and invocation of server actions
|
|
4
|
+
*
|
|
5
|
+
* @deprecated This module is deprecated. Use React 19's native server actions instead.
|
|
6
|
+
*
|
|
7
|
+
* With the new system:
|
|
8
|
+
* 1. Add "use server" directive at the top of your action file
|
|
9
|
+
* 2. Export async functions directly
|
|
10
|
+
* 3. Import and use them in client components - the bundler handles everything
|
|
11
|
+
*
|
|
12
|
+
* Example:
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // actions.ts
|
|
15
|
+
* "use server";
|
|
16
|
+
*
|
|
17
|
+
* export async function addComment(formData: FormData) {
|
|
18
|
+
* const comment = formData.get("comment");
|
|
19
|
+
* // ... save to database
|
|
20
|
+
* return { success: true };
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* ```tsx
|
|
25
|
+
* // comments.tsx
|
|
26
|
+
* "use client";
|
|
27
|
+
* import * as React from "react";
|
|
28
|
+
* import * as actions from "./actions.ts";
|
|
29
|
+
*
|
|
30
|
+
* export function Comments() {
|
|
31
|
+
* const [state, formAction] = React.useActionState(actions.addComment, null);
|
|
32
|
+
* return <form action={formAction}>...</form>;
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* Server action function signature
|
|
38
|
+
* Takes form data or arguments and returns a result
|
|
39
|
+
*/
|
|
40
|
+
export type ServerAction = (...args: unknown[]) => Promise<unknown>;
|
|
41
|
+
/**
|
|
42
|
+
* Error thrown by {@link invokeAction}. `actionId` names the action; when the
|
|
43
|
+
* action itself threw, `cause` holds that original error.
|
|
44
|
+
*/
|
|
45
|
+
export declare class ServerActionError extends Error {
|
|
46
|
+
/** Id of the action that was invoked */
|
|
47
|
+
readonly actionId: string;
|
|
48
|
+
/**
|
|
49
|
+
* Create the error for one action.
|
|
50
|
+
*
|
|
51
|
+
* @param actionId - Id of the action that was invoked
|
|
52
|
+
* @param message - What failed, naming the action
|
|
53
|
+
* @param options - `cause` carries the action's own error
|
|
54
|
+
*/
|
|
55
|
+
constructor(actionId: string, message: string, options?: ErrorOptions);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Register a server action with a unique ID
|
|
59
|
+
* @param id - Unique identifier for the action
|
|
60
|
+
* @param fn - The server function to execute
|
|
61
|
+
*/
|
|
62
|
+
export declare function registerAction(id: string, fn: ServerAction): void;
|
|
63
|
+
/**
|
|
64
|
+
* Invoke a server action by its ID
|
|
65
|
+
*
|
|
66
|
+
* Errors are returned to the caller, not logged here: the caller that handles
|
|
67
|
+
* the failure logs it once.
|
|
68
|
+
*
|
|
69
|
+
* @param id - The action ID to invoke
|
|
70
|
+
* @param args - Arguments to pass to the action
|
|
71
|
+
* @returns The result of the action execution
|
|
72
|
+
* @throws {ServerActionError} When no action is registered under `id`, or when
|
|
73
|
+
* the action throws; the action's own error is the `cause`
|
|
74
|
+
*/
|
|
75
|
+
export declare function invokeAction(id: string, args: unknown[]): Promise<unknown>;
|
|
76
|
+
/**
|
|
77
|
+
* Get all registered action IDs (for debugging)
|
|
78
|
+
*/
|
|
79
|
+
export declare function getRegisteredActions(): string[];
|
|
80
|
+
/**
|
|
81
|
+
* Clear all registered actions (for testing)
|
|
82
|
+
*/
|
|
83
|
+
export declare function clearActions(): void;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Server Action Registry
|
|
4
|
+
* Manages registration and invocation of server actions
|
|
5
|
+
*
|
|
6
|
+
* @deprecated This module is deprecated. Use React 19's native server actions instead.
|
|
7
|
+
*
|
|
8
|
+
* With the new system:
|
|
9
|
+
* 1. Add "use server" directive at the top of your action file
|
|
10
|
+
* 2. Export async functions directly
|
|
11
|
+
* 3. Import and use them in client components - the bundler handles everything
|
|
12
|
+
*
|
|
13
|
+
* Example:
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // actions.ts
|
|
16
|
+
* "use server";
|
|
17
|
+
*
|
|
18
|
+
* export async function addComment(formData: FormData) {
|
|
19
|
+
* const comment = formData.get("comment");
|
|
20
|
+
* // ... save to database
|
|
21
|
+
* return { success: true };
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* ```tsx
|
|
26
|
+
* // comments.tsx
|
|
27
|
+
* "use client";
|
|
28
|
+
* import * as React from "react";
|
|
29
|
+
* import * as actions from "./actions.ts";
|
|
30
|
+
*
|
|
31
|
+
* export function Comments() {
|
|
32
|
+
* const [state, formAction] = React.useActionState(actions.addComment, null);
|
|
33
|
+
* return <form action={formAction}>...</form>;
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
import * as logging from "@eserstack/logging";
|
|
38
|
+
const actionLogger = logging.logger.getLogger([
|
|
39
|
+
"laroux-server",
|
|
40
|
+
"server-actions",
|
|
41
|
+
]);
|
|
42
|
+
/**
|
|
43
|
+
* Error thrown by {@link invokeAction}. `actionId` names the action; when the
|
|
44
|
+
* action itself threw, `cause` holds that original error.
|
|
45
|
+
*/
|
|
46
|
+
export class ServerActionError extends Error {
|
|
47
|
+
/** Id of the action that was invoked */
|
|
48
|
+
actionId;
|
|
49
|
+
/**
|
|
50
|
+
* Create the error for one action.
|
|
51
|
+
*
|
|
52
|
+
* @param actionId - Id of the action that was invoked
|
|
53
|
+
* @param message - What failed, naming the action
|
|
54
|
+
* @param options - `cause` carries the action's own error
|
|
55
|
+
*/
|
|
56
|
+
constructor(actionId, message, options) {
|
|
57
|
+
// Explicit assignment, not parameter properties: Node's erase-only type
|
|
58
|
+
// stripping cannot emit the assignments they imply.
|
|
59
|
+
super(message, options);
|
|
60
|
+
this.name = "ServerActionError";
|
|
61
|
+
this.actionId = actionId;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Registry of server actions mapped by their unique IDs
|
|
66
|
+
*/
|
|
67
|
+
const actionRegistry = new Map();
|
|
68
|
+
/**
|
|
69
|
+
* Register a server action with a unique ID
|
|
70
|
+
* @param id - Unique identifier for the action
|
|
71
|
+
* @param fn - The server function to execute
|
|
72
|
+
*/
|
|
73
|
+
export function registerAction(id, fn) {
|
|
74
|
+
if (actionRegistry.has(id)) {
|
|
75
|
+
actionLogger.warn(`Action ID "${id}" is already registered. Overwriting.`);
|
|
76
|
+
}
|
|
77
|
+
actionRegistry.set(id, fn);
|
|
78
|
+
actionLogger.debug(`Registered action: ${id}`);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Invoke a server action by its ID
|
|
82
|
+
*
|
|
83
|
+
* Errors are returned to the caller, not logged here: the caller that handles
|
|
84
|
+
* the failure logs it once.
|
|
85
|
+
*
|
|
86
|
+
* @param id - The action ID to invoke
|
|
87
|
+
* @param args - Arguments to pass to the action
|
|
88
|
+
* @returns The result of the action execution
|
|
89
|
+
* @throws {ServerActionError} When no action is registered under `id`, or when
|
|
90
|
+
* the action throws; the action's own error is the `cause`
|
|
91
|
+
*/
|
|
92
|
+
export async function invokeAction(id, args) {
|
|
93
|
+
const action = actionRegistry.get(id);
|
|
94
|
+
if (action === undefined) {
|
|
95
|
+
throw new ServerActionError(id, `invoke server action "${id}": no action is registered under this id; register it with registerAction() first`);
|
|
96
|
+
}
|
|
97
|
+
// Arguments are user input and may hold personal data, so only their count
|
|
98
|
+
// is logged.
|
|
99
|
+
const log = actionLogger.with({ actionId: id, argCount: args.length });
|
|
100
|
+
log.debug("invoking action");
|
|
101
|
+
try {
|
|
102
|
+
const result = await action(...args);
|
|
103
|
+
log.debug("action completed");
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
throw new ServerActionError(id, `invoke server action "${id}"`, {
|
|
108
|
+
cause: error,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get all registered action IDs (for debugging)
|
|
114
|
+
*/
|
|
115
|
+
export function getRegisteredActions() {
|
|
116
|
+
return Array.from(actionRegistry.keys());
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Clear all registered actions (for testing)
|
|
120
|
+
*/
|
|
121
|
+
export function clearActions() {
|
|
122
|
+
actionRegistry.clear();
|
|
123
|
+
actionLogger.debug("Cleared all registered actions");
|
|
124
|
+
}
|