better formatting
This commit is contained in:
parent
bc74499d96
commit
7703659f81
3 changed files with 51 additions and 25 deletions
44
index.ts
44
index.ts
|
|
@ -19,9 +19,11 @@ export async function minifyHtml(source: string): Promise<string> {
|
|||
});
|
||||
}
|
||||
|
||||
|
||||
export async function minifyJavaScript(source: string): Promise<string> {
|
||||
return (await terser.minify(source, { format: { comments: false } })).code ?? source;
|
||||
return (
|
||||
(await terser.minify(source, { format: { comments: false } })).code ??
|
||||
source
|
||||
);
|
||||
}
|
||||
|
||||
export function optimizeSvg(source: string): string {
|
||||
|
|
@ -36,16 +38,29 @@ export function imageOptimization(optimizer: ImageOptimizer): ImageOptimizer {
|
|||
return optimizer;
|
||||
}
|
||||
|
||||
function loaderIntegration(name: string, extension: string, transform: (source: string) => string | Promise<string>): Integration {
|
||||
function loaderIntegration(
|
||||
name: string,
|
||||
extension: string,
|
||||
transform: (source: string) => string | Promise<string>,
|
||||
): Integration {
|
||||
return {
|
||||
name,
|
||||
async transform(asset) {
|
||||
if (!asset.path.endsWith(extension)) return asset;
|
||||
const source = typeof asset.content === "string" ? asset.content : new TextDecoder().decode(asset.content);
|
||||
const source =
|
||||
typeof asset.content === "string"
|
||||
? asset.content
|
||||
: new TextDecoder().decode(asset.content);
|
||||
return { ...asset, content: await transform(source) };
|
||||
},
|
||||
setup(context: IntegrationContext) {
|
||||
context.loader(name, { name, extensions: [extension], async load(file) { return { data: {}, content: await transform(file.source) }; } });
|
||||
context.loader(name, {
|
||||
name,
|
||||
extensions: [extension],
|
||||
async load(file) {
|
||||
return { data: {}, content: await transform(file.source) };
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -66,20 +81,31 @@ export function svgoIntegration(): Integration {
|
|||
return loaderIntegration("svgo", ".svg", optimizeSvg);
|
||||
}
|
||||
|
||||
export function imageOptimizationIntegration(optimizer: ImageOptimizer): Integration {
|
||||
export function imageOptimizationIntegration(
|
||||
optimizer: ImageOptimizer,
|
||||
): Integration {
|
||||
return {
|
||||
name: "image-optimization",
|
||||
async transform(asset) {
|
||||
if (!/\.(png|jpe?g|webp|avif)$/i.test(asset.path)) return asset;
|
||||
const input = typeof asset.content === "string" ? new TextEncoder().encode(asset.content) : asset.content;
|
||||
return { ...asset, content: await optimizer.optimize(input, asset.contentType) };
|
||||
const input =
|
||||
typeof asset.content === "string"
|
||||
? new TextEncoder().encode(asset.content)
|
||||
: asset.content;
|
||||
return {
|
||||
...asset,
|
||||
content: await optimizer.optimize(input, asset.contentType),
|
||||
};
|
||||
},
|
||||
setup(context: IntegrationContext) {
|
||||
context.loader("image", {
|
||||
name: "image",
|
||||
extensions: [".png", ".jpg", ".jpeg", ".webp", ".avif"],
|
||||
async load(file) {
|
||||
const optimized = await optimizer.optimize(new TextEncoder().encode(file.source), "image/*");
|
||||
const optimized = await optimizer.optimize(
|
||||
new TextEncoder().encode(file.source),
|
||||
"image/*",
|
||||
);
|
||||
return { data: { optimized }, content: file.source };
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue