Skip to content

Commit

Permalink
feat: root cli flag, partial route prerender implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianCataldo committed Jun 8, 2024
1 parent 976153c commit 3634356
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/cli/src/bin/gracile-watch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// NOTE: shebang via `@gracile/gracile`

import { join } from 'node:path';

import { program } from '@commander-js/extra-typings';
import { logger } from '@gracile/internal-utils/logger';

Expand All @@ -16,11 +18,13 @@ program
// .command('dev')
.option('-p, --port <number>', 'Assign a local port (overrides config. file)')
.option('-h, --host', 'Expose the server to you local network (0.0.0.0)')
.option('-r, --root <string>', 'Root directory for the project')
.action((_str, options) => {
const opts = options.opts();
dev({
port: opts.port ? Number(opts.port) : undefined,
expose: opts.host,
root: opts.root ? join(process.cwd(), opts.root) : undefined,
}).catch((e) => logger.error(String(e)));
});

Expand Down
20 changes: 15 additions & 5 deletions packages/cli/src/bin/gracile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// NOTE: shebang via `@gracile/gracile`

import { join } from 'node:path';

import { program } from '@commander-js/extra-typings';
import { build } from '@gracile/engine/build/build';
import { preview } from '@gracile/engine/preview';
Expand All @@ -10,21 +12,29 @@ import { greet } from '../utils.js';
greet();

// BUILD
program.command('build').action((_str, _options) => {
build().catch((error) => logger.error(String(error)));
});
program
.command('build')
.option('-r, --root <string>', 'Root directory for the project')
.action((_str, options) => {
const opts = options.opts();
build(opts.root ? join(process.cwd(), opts.root) : undefined).catch(
(error) => logger.error(String(error)),
);
});

// PREVIEW
program
.command('preview')
.option('p, --port <number>')
.option('h, --host')
.option('-p, --port <number>', 'Assign a local port (overrides config. file)')
.option('-h, --host', 'Expose the server to you local network (0.0.0.0)')
.option('-r, --root <string>', 'Root directory for the project')

.action((_str, options) => {
const opts = options.opts();
preview({
port: opts.port ? Number(opts.port) : undefined,
expose: opts.host,
root: opts.root ? join(process.cwd(), opts.root) : undefined,
}).catch((error) => logger.error(String(error)));
});

Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

export async function dev(options: {
port?: number | undefined;
root?: string;
root?: string | undefined;
expose?: boolean | undefined;
}) {
logger.info(c.gray('\n— Development mode —\n'));
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function preview({
}: {
port?: number | undefined;
expose?: boolean | undefined;
root?: string;
root?: string | undefined;
}) {
logger.info(c.gray('\n— Preview mode —\n'));

Expand Down
17 changes: 15 additions & 2 deletions packages/engine/src/routes/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export type ModuleOptions = {
staticPaths?: StaticPathsGeneric /* | undefined */;
handler?: HandlerGeneric;

template?: (context: RouteContextGeneric) => RouteTemplateResult;
prerender?: boolean | undefined;

document?: DocumentTemplate<RouteContextGeneric>;

template?: (context: RouteContextGeneric) => RouteTemplateResult;
};

// TODO: put in engine
Expand All @@ -50,6 +52,12 @@ export class RouteModule {
return this.#document;
}

readonly #prerender;

public get prerender() {
return this.#prerender;
}

readonly #template;

public get template() {
Expand All @@ -59,17 +67,22 @@ export class RouteModule {
constructor(options: ModuleOptions) {
if (typeof options.staticPaths === 'function')
this.#staticPaths = options.staticPaths;

if (
(typeof options.handler === 'object' ||
typeof options.handler === 'function') &&
options.handler
)
this.#handler = options.handler;
// if (options.fragment) this.#fragment = options.fragment;

if (typeof options.template === 'function')
this.#template = options.template;

if (typeof options.document === 'function')
this.#document = options.document;

if (typeof options.prerender === 'boolean')
this.#prerender = options.prerender;
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/engine/src/user-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import type { UserConfig } from 'vite';
export class GracileConfig {
port?: number;

/**
* Root directory for the project
*/
root?: string;

/**
* @defaultValue 'static'
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export function defineRoute<

staticPaths?: (() => StaticPathOptions[]) | undefined;

// TODO: Make it type dependent with handler
prerender?: boolean | undefined;

document?: R.DocumentTemplate<RouteContext>;

template?: R.BodyTemplate<RouteContext>;
Expand Down

0 comments on commit 3634356

Please sign in to comment.