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> {
|
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 {
|
export function optimizeSvg(source: string): string {
|
||||||
|
|
@ -36,16 +38,29 @@ export function imageOptimization(optimizer: ImageOptimizer): ImageOptimizer {
|
||||||
return optimizer;
|
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 {
|
return {
|
||||||
name,
|
name,
|
||||||
async transform(asset) {
|
async transform(asset) {
|
||||||
if (!asset.path.endsWith(extension)) return 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) };
|
return { ...asset, content: await transform(source) };
|
||||||
},
|
},
|
||||||
setup(context: IntegrationContext) {
|
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);
|
return loaderIntegration("svgo", ".svg", optimizeSvg);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function imageOptimizationIntegration(optimizer: ImageOptimizer): Integration {
|
export function imageOptimizationIntegration(
|
||||||
|
optimizer: ImageOptimizer,
|
||||||
|
): Integration {
|
||||||
return {
|
return {
|
||||||
name: "image-optimization",
|
name: "image-optimization",
|
||||||
async transform(asset) {
|
async transform(asset) {
|
||||||
if (!/\.(png|jpe?g|webp|avif)$/i.test(asset.path)) return 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;
|
const input =
|
||||||
return { ...asset, content: await optimizer.optimize(input, asset.contentType) };
|
typeof asset.content === "string"
|
||||||
|
? new TextEncoder().encode(asset.content)
|
||||||
|
: asset.content;
|
||||||
|
return {
|
||||||
|
...asset,
|
||||||
|
content: await optimizer.optimize(input, asset.contentType),
|
||||||
|
};
|
||||||
},
|
},
|
||||||
setup(context: IntegrationContext) {
|
setup(context: IntegrationContext) {
|
||||||
context.loader("image", {
|
context.loader("image", {
|
||||||
name: "image",
|
name: "image",
|
||||||
extensions: [".png", ".jpg", ".jpeg", ".webp", ".avif"],
|
extensions: [".png", ".jpg", ".jpeg", ".webp", ".avif"],
|
||||||
async load(file) {
|
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 };
|
return { data: { optimized }, content: file.source };
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue