Files
sweet/src/dev/builder_test.ts
T
Christian Svensson 628832af0f fix: ensure file paths in snapshot.json uses / (#2704)
Avoids `\\\\` paths ending up in `snapshot.json` when using `deno task
build` on Windows. Backslashes do not which pathnames in URLs, so these
assets are not returned.

Closes #2703
2024-10-15 11:29:35 +02:00

124 lines
3.0 KiB
TypeScript

import { expect } from "@std/expect";
import * as path from "@std/path";
import { Builder } from "./builder.ts";
import { App } from "../app.ts";
import { RemoteIsland } from "@marvinh-test/fresh-island";
Deno.test({
name: "Builder - chain onTransformStaticFile",
fn: async () => {
const logs: string[] = [];
const builder = new Builder();
builder.onTransformStaticFile(
{ pluginName: "A", filter: /\.css$/ },
() => {
logs.push("A");
},
);
builder.onTransformStaticFile(
{ pluginName: "B", filter: /\.css$/ },
() => {
logs.push("B");
},
);
builder.onTransformStaticFile(
{ pluginName: "C", filter: /\.css$/ },
() => {
logs.push("C");
},
);
const tmp = await Deno.makeTempDir();
await Deno.writeTextFile(path.join(tmp, "foo.css"), "body { color: red; }");
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
expect(logs).toEqual(["A", "B", "C"]);
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Builder - handles Windows paths",
fn: async () => {
const builder = new Builder();
const tmp = await Deno.makeTempDir();
await Deno.mkdir(path.join(tmp, "images"));
await Deno.writeTextFile(
path.join(tmp, "images", "batman.svg"),
"<svg></svg>",
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
const snapshotJson = await Deno.readTextFile(
path.join(tmp, "dist", "snapshot.json"),
);
expect(snapshotJson).toContain("/images/batman.svg");
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Builder - hashes CSS urls by default",
fn: async () => {
const builder = new Builder();
const tmp = await Deno.makeTempDir();
await Deno.writeTextFile(
path.join(tmp, "foo.css"),
"body { background: url('/foo.jpg'); }",
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
const css = await Deno.readTextFile(
path.join(tmp, "dist", "static", "foo.css"),
);
expect(css).toContain('body { background: url("/foo.jpg?__frsh_c=');
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Builder - can bundle islands from JSR",
fn: async () => {
const builder = new Builder();
const tmp = await Deno.makeTempDir();
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
app.island("jsr:@marvinh-test/fresh-island", "RemoteIsland", RemoteIsland);
await builder.build(app);
const code = await Deno.readTextFile(
path.join(tmp, "dist", "static", "RemoteIsland.js"),
);
expect(code).toContain('"remote-island"');
},
sanitizeOps: false,
sanitizeResources: false,
});