Skip to content

Make images smaller using best-in-class codecs, right in the browser.

JavaScript

Moar prettier

Surma6e3e6af70eea
diff --git a/cli/lib/asset-plugin.js b/cli/lib/asset-plugin.js
index c15f95ab..29f93ee7 100644
--- a/cli/lib/asset-plugin.js
+++ b/cli/lib/asset-plugin.js
@@ -10,11 +10,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-import { promises as fs } from 'fs';
-import { basename } from 'path';
+import { promises as fs } from "fs";
+import { basename } from "path";
const defaultOpts = {
- prefix: 'asset-url',
+ prefix: "asset-url"
};
export default function assetPlugin(opts) {
@@ -23,9 +23,9 @@ export default function assetPlugin(opts) {
/** @type {Map<string, Buffer>} */
let assetIdToSourceBuffer;
- const prefix = opts.prefix + ':';
+ const prefix = opts.prefix + ":";
return {
- name: 'asset-plugin',
+ name: "asset-plugin",
buildStart() {
assetIdToSourceBuffer = new Map();
},
@@ -56,20 +56,20 @@ export default function assetPlugin(opts) {
throw Error(`Cannot find ${realId}`);
}
// Add an additional .js to the end so it ends up with .js at the end in the _virtual folder.
- return prefix + resolveResult.id + '.js';
+ return prefix + resolveResult.id + ".js";
},
async load(id) {
if (!id.startsWith(prefix)) return;
- const realId = id.slice(prefix.length, -'.js'.length);
+ const realId = id.slice(prefix.length, -".js".length);
const source = await fs.readFile(realId);
assetIdToSourceBuffer.set(id, source);
this.addWatchFile(realId);
return `export default import.meta.ROLLUP_FILE_URL_${this.emitFile({
- type: 'asset',
+ type: "asset",
source,
- name: basename(realId),
+ name: basename(realId)
})}`;
- },
+ }
};
}
diff --git a/cli/lib/json-plugin.js b/cli/lib/json-plugin.js
index ed0419e2..f5bdf2df 100644
--- a/cli/lib/json-plugin.js
+++ b/cli/lib/json-plugin.js
@@ -1,4 +1,4 @@
-import {promises as fsp} from "fs";
+import { promises as fsp } from "fs";
const prefix = "json:";
@@ -21,13 +21,13 @@ export default function ejsAssetPlugin() {
const realId = id.slice(prefix.length);
const source = await fsp.readFile(realId, "utf8");
- let code = "";
- for(const [key, value] of Object.entries(JSON.parse(source))) {
- code += `
+ let code = "";
+ for (const [key, value] of Object.entries(JSON.parse(source))) {
+ code += `
export const ${key} = ${JSON.stringify(value)};
- `
- }
- return code;
- },
+ `;
+ }
+ return code;
+ }
};
}
diff --git a/cli/src/codecs.js b/cli/src/codecs.js
index 084b246e..262b3d0f 100644
--- a/cli/src/codecs.js
+++ b/cli/src/codecs.js
@@ -1,4 +1,4 @@
-import {promises as fsp} from "fs";
+import { promises as fsp } from "fs";
// MozJPEG
import mozEnc from "../../codecs/mozjpeg/enc/mozjpeg_enc.js";
@@ -19,14 +19,14 @@ import avifDec from "../../codecs/avif/dec/avif_dec.js";
import avifDecWasm from "asset-url:../../codecs/avif/dec/avif_dec.wasm";
function instantiateEmscriptenWasm(factory, path) {
- if(path.startsWith("file://")) {
- path = path.slice("file://".length);
- }
- return factory({
- locateFile() {
- return path;
- }
- })
+ if (path.startsWith("file://")) {
+ path = path.slice("file://".length);
+ }
+ return factory({
+ locateFile() {
+ return path;
+ }
+ });
}
export default {
@@ -34,8 +34,8 @@ export default {
name: "MozJPEG",
extension: "jpg",
detectors: [/^\xFF\xD8\xFF/],
- dec: () => instantiateEmscriptenWasm( mozDec, mozDecWasm),
- enc: () => instantiateEmscriptenWasm( mozEnc, mozEncWasm),
+ dec: () => instantiateEmscriptenWasm(mozDec, mozDecWasm),
+ enc: () => instantiateEmscriptenWasm(mozEnc, mozEncWasm),
defaultEncoderOptions: {
quality: 75,
baseline: false,
@@ -59,8 +59,8 @@ export default {
name: "WebP",
extension: "webp",
detectors: [/^RIFF....WEBPVP8[LX ]/],
- dec: () => instantiateEmscriptenWasm( webpDec, webpDecWasm),
- enc: () => instantiateEmscriptenWasm( webpEnc, webpEncWasm),
+ dec: () => instantiateEmscriptenWasm(webpDec, webpDecWasm),
+ enc: () => instantiateEmscriptenWasm(webpEnc, webpEncWasm),
defaultEncoderOptions: {
quality: 75,
target_size: 0,
@@ -95,8 +95,8 @@ export default {
name: "AVIF",
extension: "avif",
detectors: [/^\x00\x00\x00 ftypavif\x00\x00\x00\x00/],
- dec: () => instantiateEmscriptenWasm( avifDec, avifDecWasm),
- enc: () => instantiateEmscriptenWasm( avifEnc, avifEncWasm),
+ dec: () => instantiateEmscriptenWasm(avifDec, avifDecWasm),
+ enc: () => instantiateEmscriptenWasm(avifEnc, avifEncWasm),
defaultEncoderOptions: {
minQuantizer: 16,
maxQuantizer: 16,
diff --git a/cli/src/image_data.js b/cli/src/image_data.js
index ee4f96b5..7d7736be 100644
--- a/cli/src/image_data.js
+++ b/cli/src/image_data.js
@@ -4,4 +4,4 @@ export default class ImageData {
this.width = width;
this.height = height;
}
-};
+}
diff --git a/cli/src/index.js b/cli/src/index.js
index aab7106e..5a9cb440 100644
--- a/cli/src/index.js
+++ b/cli/src/index.js
@@ -1,14 +1,10 @@
-import {program} from "commander";
+import { program } from "commander";
import JSON5 from "json5";
-import {
- Worker,
- isMainThread,
- parentPort
-} from "worker_threads";
+import { Worker, isMainThread, parentPort } from "worker_threads";
import { cpus } from "os";
-import {extname,join,basename} from "path";
-import {promises as fsp} from "fs";
-import {version} from "json:../package.json";
+import { extname, join, basename } from "path";
+import { promises as fsp } from "fs";
+import { version } from "json:../package.json";
import supportedFormats from "./codecs.js";
@@ -131,10 +127,7 @@ async function processFiles(files) {
value.defaultEncoderOptions,
JSON5.parse(program[encName])
);
- const outputFile = join(
- program.outputDir,
- `${base}.${value.extension}`
- );
+ const outputFile = join(program.outputDir, `${base}.${value.extension}`);
jobs.push(
jobPromise(pool[i], {
bitmap,