feat: add plugin buildStart and buildEnd hooks (#1770)
This commit is contained in:
@@ -46,7 +46,13 @@ export async function dev(
|
||||
if (isBuild) {
|
||||
// Ensure that build dir is empty
|
||||
await fs.emptyDir(outDir);
|
||||
await build(join(dir, "fresh.gen.ts"), options ?? {});
|
||||
|
||||
options = options ?? {};
|
||||
const plugins = options.plugins ?? [];
|
||||
|
||||
await Promise.all(plugins.map((plugin) => plugin.buildStart?.()));
|
||||
await build(join(dir, "fresh.gen.ts"), options);
|
||||
await Promise.all(plugins.map((plugin) => plugin.buildEnd?.()));
|
||||
} else if (options) {
|
||||
const ctx = await ServerContext.fromManifest(manifest, {
|
||||
...options,
|
||||
|
||||
@@ -427,6 +427,15 @@ export interface Plugin<State = Record<string, unknown>> {
|
||||
*/
|
||||
renderAsync?(ctx: PluginAsyncRenderContext): Promise<PluginRenderResult>;
|
||||
|
||||
/**
|
||||
* Called before running the Fresh build task
|
||||
*/
|
||||
buildStart?(): Promise<void> | void;
|
||||
/**
|
||||
* Called after completing the Fresh build task
|
||||
*/
|
||||
buildEnd?(): Promise<void> | void;
|
||||
|
||||
routes?: PluginRoute[];
|
||||
|
||||
middlewares?: PluginMiddleware<State>[];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { colors, join } from "../src/server/deps.ts";
|
||||
import { join } from "../src/server/deps.ts";
|
||||
import {
|
||||
assert,
|
||||
assertEquals,
|
||||
@@ -9,18 +9,7 @@ import {
|
||||
import versions from "../versions.json" assert { type: "json" };
|
||||
import { CheckFile } from "$fresh/src/dev/update_check.ts";
|
||||
import { WEEK } from "$fresh/src/dev/deps.ts";
|
||||
|
||||
function getStdOutput(
|
||||
out: Deno.CommandOutput,
|
||||
): { stdout: string; stderr: string } {
|
||||
const decoder = new TextDecoder();
|
||||
const stdout = colors.stripColor(decoder.decode(out.stdout));
|
||||
|
||||
const decoderErr = new TextDecoder();
|
||||
const stderr = colors.stripColor(decoderErr.decode(out.stderr));
|
||||
|
||||
return { stdout, stderr };
|
||||
}
|
||||
import { getStdOutput } from "$fresh/tests/test_utils.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "stores update check file in $HOME/fresh",
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"lock": false,
|
||||
"imports": {
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
"jsxImportSource": "preact"
|
||||
}
|
||||
}
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||
|
||||
import dev from "$fresh/dev.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await dev(import.meta.url, "./main.ts", config);
|
||||
@@ -0,0 +1,24 @@
|
||||
import { defineConfig } from "$fresh/src/server/defines.ts";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
{
|
||||
name: "a",
|
||||
buildEnd() {
|
||||
console.log("Plugin a: buildEnd");
|
||||
},
|
||||
buildStart() {
|
||||
console.log("Plugin a: buildStart");
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "b",
|
||||
buildEnd() {
|
||||
console.log("Plugin b: buildEnd");
|
||||
},
|
||||
buildStart() {
|
||||
console.log("Plugin b: buildStart");
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
// DO NOT EDIT. This file is generated by Fresh.
|
||||
// This file SHOULD be checked into source version control.
|
||||
// This file is automatically updated during development when running `dev.ts`.
|
||||
|
||||
import * as $0 from "./routes/index.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
"./routes/index.tsx": $0,
|
||||
},
|
||||
islands: {},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
export default manifest;
|
||||
@@ -0,0 +1,11 @@
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
/// <reference lib="dom.asynciterable" />
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import manifest from "./fresh.gen.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await start(manifest, config);
|
||||
@@ -0,0 +1,7 @@
|
||||
export default function Home() {
|
||||
return (
|
||||
<div>
|
||||
<h1>Hello World</h1>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+17
-1
@@ -8,7 +8,7 @@ import {
|
||||
} from "./deps.ts";
|
||||
import manifest from "./fixture_plugin/fresh.gen.ts";
|
||||
import options from "./fixture_plugin/options.ts";
|
||||
import { startFreshServer } from "./test_utils.ts";
|
||||
import { runBuild, startFreshServer } from "./test_utils.ts";
|
||||
|
||||
const ctx = await ServerContext.fromManifest(manifest, options);
|
||||
const handler = ctx.handler();
|
||||
@@ -129,3 +129,19 @@ Deno.test({
|
||||
for await (const _ of lines) { /* noop */ }
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test("calls buildStart() and buildEnd()", async () => {
|
||||
const result = await runBuild("./tests/fixture_plugin_lifecycle/dev.ts");
|
||||
|
||||
let out = result.stdout.split("\n");
|
||||
const idx = out.findIndex((line) => line.startsWith("Plugin"));
|
||||
const idxEnd = out.findLastIndex((line) => line.startsWith("Plugin"));
|
||||
out = out.slice(idx, idxEnd + 1);
|
||||
|
||||
assertEquals(out, [
|
||||
"Plugin a: buildStart",
|
||||
"Plugin b: buildStart",
|
||||
"Plugin a: buildEnd",
|
||||
"Plugin b: buildEnd",
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { colors } from "$fresh/src/server/deps.ts";
|
||||
import { assert } from "$std/_util/asserts.ts";
|
||||
import * as path from "$std/path/mod.ts";
|
||||
import {
|
||||
assertEquals,
|
||||
delay,
|
||||
@@ -345,3 +347,51 @@ async function spawnServer(
|
||||
|
||||
return { serverProcess, lines, address, output };
|
||||
}
|
||||
|
||||
export async function runBuild(fixture: string) {
|
||||
const outDir = path.join(path.dirname(fixture), "_fresh");
|
||||
try {
|
||||
await Deno.remove(outDir, { recursive: true });
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
assert(
|
||||
fixture.endsWith("dev.ts"),
|
||||
`Build command only works with "dev.ts", but got "${fixture}" instead`,
|
||||
);
|
||||
const res = await new Deno.Command(Deno.execPath(), {
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
fixture,
|
||||
"build",
|
||||
],
|
||||
env: {
|
||||
GITHUB_SHA: "__BUILD_ID__",
|
||||
DENO_DEPLOYMENT_ID: "__BUILD_ID__",
|
||||
},
|
||||
stdin: "null",
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
}).output();
|
||||
|
||||
const output = getStdOutput(res);
|
||||
return {
|
||||
code: res.code,
|
||||
stderr: output.stderr,
|
||||
stdout: output.stdout,
|
||||
};
|
||||
}
|
||||
|
||||
export function getStdOutput(
|
||||
out: Deno.CommandOutput,
|
||||
): { stdout: string; stderr: string } {
|
||||
const decoder = new TextDecoder();
|
||||
const stdout = colors.stripColor(decoder.decode(out.stdout));
|
||||
|
||||
const decoderErr = new TextDecoder();
|
||||
const stderr = colors.stripColor(decoderErr.decode(out.stderr));
|
||||
|
||||
return { stdout, stderr };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user