mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-11 16:26:20 +00:00
Moar prettier
This commit is contained in:
@@ -10,11 +10,11 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import { promises as fs } from 'fs';
|
import { promises as fs } from "fs";
|
||||||
import { basename } from 'path';
|
import { basename } from "path";
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
prefix: 'asset-url',
|
prefix: "asset-url"
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function assetPlugin(opts) {
|
export default function assetPlugin(opts) {
|
||||||
@@ -23,9 +23,9 @@ export default function assetPlugin(opts) {
|
|||||||
/** @type {Map<string, Buffer>} */
|
/** @type {Map<string, Buffer>} */
|
||||||
let assetIdToSourceBuffer;
|
let assetIdToSourceBuffer;
|
||||||
|
|
||||||
const prefix = opts.prefix + ':';
|
const prefix = opts.prefix + ":";
|
||||||
return {
|
return {
|
||||||
name: 'asset-plugin',
|
name: "asset-plugin",
|
||||||
buildStart() {
|
buildStart() {
|
||||||
assetIdToSourceBuffer = new Map();
|
assetIdToSourceBuffer = new Map();
|
||||||
},
|
},
|
||||||
@@ -56,20 +56,20 @@ export default function assetPlugin(opts) {
|
|||||||
throw Error(`Cannot find ${realId}`);
|
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.
|
// 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) {
|
async load(id) {
|
||||||
if (!id.startsWith(prefix)) return;
|
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);
|
const source = await fs.readFile(realId);
|
||||||
assetIdToSourceBuffer.set(id, source);
|
assetIdToSourceBuffer.set(id, source);
|
||||||
this.addWatchFile(realId);
|
this.addWatchFile(realId);
|
||||||
|
|
||||||
return `export default import.meta.ROLLUP_FILE_URL_${this.emitFile({
|
return `export default import.meta.ROLLUP_FILE_URL_${this.emitFile({
|
||||||
type: 'asset',
|
type: "asset",
|
||||||
source,
|
source,
|
||||||
name: basename(realId),
|
name: basename(realId)
|
||||||
})}`;
|
})}`;
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {promises as fsp} from "fs";
|
import { promises as fsp } from "fs";
|
||||||
|
|
||||||
const prefix = "json:";
|
const prefix = "json:";
|
||||||
|
|
||||||
@@ -21,13 +21,13 @@ export default function ejsAssetPlugin() {
|
|||||||
const realId = id.slice(prefix.length);
|
const realId = id.slice(prefix.length);
|
||||||
const source = await fsp.readFile(realId, "utf8");
|
const source = await fsp.readFile(realId, "utf8");
|
||||||
|
|
||||||
let code = "";
|
let code = "";
|
||||||
for(const [key, value] of Object.entries(JSON.parse(source))) {
|
for (const [key, value] of Object.entries(JSON.parse(source))) {
|
||||||
code += `
|
code += `
|
||||||
export const ${key} = ${JSON.stringify(value)};
|
export const ${key} = ${JSON.stringify(value)};
|
||||||
`
|
`;
|
||||||
}
|
}
|
||||||
return code;
|
return code;
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {promises as fsp} from "fs";
|
import { promises as fsp } from "fs";
|
||||||
|
|
||||||
// MozJPEG
|
// MozJPEG
|
||||||
import mozEnc from "../../codecs/mozjpeg/enc/mozjpeg_enc.js";
|
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";
|
import avifDecWasm from "asset-url:../../codecs/avif/dec/avif_dec.wasm";
|
||||||
|
|
||||||
function instantiateEmscriptenWasm(factory, path) {
|
function instantiateEmscriptenWasm(factory, path) {
|
||||||
if(path.startsWith("file://")) {
|
if (path.startsWith("file://")) {
|
||||||
path = path.slice("file://".length);
|
path = path.slice("file://".length);
|
||||||
}
|
}
|
||||||
return factory({
|
return factory({
|
||||||
locateFile() {
|
locateFile() {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -34,8 +34,8 @@ export default {
|
|||||||
name: "MozJPEG",
|
name: "MozJPEG",
|
||||||
extension: "jpg",
|
extension: "jpg",
|
||||||
detectors: [/^\xFF\xD8\xFF/],
|
detectors: [/^\xFF\xD8\xFF/],
|
||||||
dec: () => instantiateEmscriptenWasm( mozDec, mozDecWasm),
|
dec: () => instantiateEmscriptenWasm(mozDec, mozDecWasm),
|
||||||
enc: () => instantiateEmscriptenWasm( mozEnc, mozEncWasm),
|
enc: () => instantiateEmscriptenWasm(mozEnc, mozEncWasm),
|
||||||
defaultEncoderOptions: {
|
defaultEncoderOptions: {
|
||||||
quality: 75,
|
quality: 75,
|
||||||
baseline: false,
|
baseline: false,
|
||||||
@@ -59,8 +59,8 @@ export default {
|
|||||||
name: "WebP",
|
name: "WebP",
|
||||||
extension: "webp",
|
extension: "webp",
|
||||||
detectors: [/^RIFF....WEBPVP8[LX ]/],
|
detectors: [/^RIFF....WEBPVP8[LX ]/],
|
||||||
dec: () => instantiateEmscriptenWasm( webpDec, webpDecWasm),
|
dec: () => instantiateEmscriptenWasm(webpDec, webpDecWasm),
|
||||||
enc: () => instantiateEmscriptenWasm( webpEnc, webpEncWasm),
|
enc: () => instantiateEmscriptenWasm(webpEnc, webpEncWasm),
|
||||||
defaultEncoderOptions: {
|
defaultEncoderOptions: {
|
||||||
quality: 75,
|
quality: 75,
|
||||||
target_size: 0,
|
target_size: 0,
|
||||||
@@ -95,8 +95,8 @@ export default {
|
|||||||
name: "AVIF",
|
name: "AVIF",
|
||||||
extension: "avif",
|
extension: "avif",
|
||||||
detectors: [/^\x00\x00\x00 ftypavif\x00\x00\x00\x00/],
|
detectors: [/^\x00\x00\x00 ftypavif\x00\x00\x00\x00/],
|
||||||
dec: () => instantiateEmscriptenWasm( avifDec, avifDecWasm),
|
dec: () => instantiateEmscriptenWasm(avifDec, avifDecWasm),
|
||||||
enc: () => instantiateEmscriptenWasm( avifEnc, avifEncWasm),
|
enc: () => instantiateEmscriptenWasm(avifEnc, avifEncWasm),
|
||||||
defaultEncoderOptions: {
|
defaultEncoderOptions: {
|
||||||
minQuantizer: 16,
|
minQuantizer: 16,
|
||||||
maxQuantizer: 16,
|
maxQuantizer: 16,
|
||||||
|
|||||||
@@ -4,4 +4,4 @@ export default class ImageData {
|
|||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
import {program} from "commander";
|
import { program } from "commander";
|
||||||
import JSON5 from "json5";
|
import JSON5 from "json5";
|
||||||
import {
|
import { Worker, isMainThread, parentPort } from "worker_threads";
|
||||||
Worker,
|
|
||||||
isMainThread,
|
|
||||||
parentPort
|
|
||||||
} from "worker_threads";
|
|
||||||
import { cpus } from "os";
|
import { cpus } from "os";
|
||||||
import {extname,join,basename} from "path";
|
import { extname, join, basename } from "path";
|
||||||
import {promises as fsp} from "fs";
|
import { promises as fsp } from "fs";
|
||||||
import {version} from "json:../package.json";
|
import { version } from "json:../package.json";
|
||||||
|
|
||||||
import supportedFormats from "./codecs.js";
|
import supportedFormats from "./codecs.js";
|
||||||
|
|
||||||
@@ -131,10 +127,7 @@ async function processFiles(files) {
|
|||||||
value.defaultEncoderOptions,
|
value.defaultEncoderOptions,
|
||||||
JSON5.parse(program[encName])
|
JSON5.parse(program[encName])
|
||||||
);
|
);
|
||||||
const outputFile = join(
|
const outputFile = join(program.outputDir, `${base}.${value.extension}`);
|
||||||
program.outputDir,
|
|
||||||
`${base}.${value.extension}`
|
|
||||||
);
|
|
||||||
jobs.push(
|
jobs.push(
|
||||||
jobPromise(pool[i], {
|
jobPromise(pool[i], {
|
||||||
bitmap,
|
bitmap,
|
||||||
|
|||||||
Reference in New Issue
Block a user