Skip to content

Commit

Permalink
change dist files organisation
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulBlanche committed Aug 20, 2023
1 parent be2b805 commit 650c1b9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 41 deletions.
6 changes: 5 additions & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ export class FrugalConfig {
return new URL(".cache/", this.outdir);
}

get tempdir() {
return new URL(".temp/", this.outdir);
}

get builddir() {
return new URL("build/", this.cachedir);
return new URL("build/", this.tempdir);
}

get buildCacheFile() {
Expand Down
8 changes: 3 additions & 5 deletions src/build/plugins/cleanOutdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ export function cleanOutdir(config: FrugalConfig, cleanAll: boolean): esbuild.Pl
);

for await (const entry of Deno.readDir(config.outdir)) {
if (entry.name !== ".cache") {
await Deno.remove(new URL(entry.name, config.outdir), {
const entryURL = new URL(entry.name, config.outdir);
if (!entry.isDirectory || `${entryURL.href}/` !== config.cachedir.href) {
await Deno.remove(entryURL, {
recursive: true,
});
}
}
await Deno.remove(esbuildOutDir, {
recursive: true,
});
} else {
log(
`clean directory ${esbuildOutDir}`,
Expand Down
1 change: 1 addition & 0 deletions src/cache/BuildCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class BuildCache implements Cache {
async save() {
const filePath = path.fromFileUrl(this.#config.buildCacheFile);

await fs.ensureFile(filePath);
await Deno.writeTextFile(
filePath,
JSON.stringify({ current: this.#current, previous: this.#previous }, undefined, 2),
Expand Down
61 changes: 27 additions & 34 deletions src/plugin/googleFonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,37 @@ export function googleFonts({ type = "local" }: Config = {}): Plugin {
.toString();
const ext = path.extname(matched[1]);

const fontPath = `fonts/${name}${ext}`;
const fontUrl = new URL(fontPath, frugal.config.publicdir);

if (await exists(fontUrl)) {
css = css.replace(matched[1], `/fonts/${name}${ext}`);
} else {
log(`Loading font ${++index} of ${urls.length}`, {
scope: "frugal:googleFonts",
level: "debug",
});
const response = await fetch(matched[1]);
const readableStream = response.body?.getReader();
if (readableStream) {
const reader = streams.readerFromStreamReader(readableStream);

await fs.ensureFile(fontUrl);
const file = await Deno.open(fontUrl, { create: true, write: true });
try {
const fontPath = `googleFonts/${name}${ext}`;
const fontUrl = new URL(fontPath, frugal.config.cachedir);

try {
await fs.ensureDir(path.dirname(path.fromFileUrl(fontUrl)));
const file = await Deno.open(fontUrl, { createNew: true, write: true });
try {
log(`Loading font ${++index} of ${urls.length}`, {
scope: "frugal:googleFonts",
level: "debug",
});
const response = await fetch(matched[1]);
const readableStream = response.body?.getReader();
if (readableStream) {
const reader = streams.readerFromStreamReader(readableStream);

await streams.copy(reader, file);
css = css.replace(matched[1], `/fonts/${name}${ext}`);
} finally {
file.close();
}
} finally {
file.close();
}
} catch (error) {
if (!(error instanceof Deno.errors.AlreadyExists)) {
throw error;
}
}

const fontDest = new URL(`fonts/${name}${ext}`, frugal.config.publicdir);
await fs.ensureDir(path.dirname(path.fromFileUrl(fontDest)));
await fs.copy(fontUrl, fontDest);
css = css.replace(matched[1], `/fonts/${name}${ext}`);
}
}
}
Expand All @@ -113,16 +119,3 @@ export function googleFonts({ type = "local" }: Config = {}): Plugin {
};
};
}

async function exists(path: string | URL) {
try {
await Deno.stat(path);
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return false;
}

throw error;
}
}
2 changes: 1 addition & 1 deletion src/plugin/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function script(
for (const asset of assets) {
const entrypoint = asset.entrypoint;
const facadePath = path.join("asset", "script", entrypoint);
const facadeUrl = new URL(facadePath, frugal.config.cachedir);
const facadeUrl = new URL(facadePath, frugal.config.tempdir);
const facadeContent = `import "${asset.url.href}";`;
facadesMap[entrypoint] = facadesMap[entrypoint] ?? {
entrypoint,
Expand Down

0 comments on commit 650c1b9

Please sign in to comment.