mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-23 21:17:54 +02:00
refactor: migrate lqip-loader to TS, fix typing for Webpack Loaders (#5779)
This commit is contained in:
parent
ca5d70d7fb
commit
68c970175a
18 changed files with 254 additions and 265 deletions
52
packages/lqip-loader/src/lqip.ts
Normal file
52
packages/lqip-loader/src/lqip.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import Vibrant from 'node-vibrant';
|
||||
import path from 'path';
|
||||
import sharp from 'sharp';
|
||||
import {toPalette, toBase64} from './utils';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const {version} = require('../package.json');
|
||||
|
||||
const ERROR_EXT = `Error: Input file is missing or uses unsupported image format, lqip v${version}`;
|
||||
|
||||
const SUPPORTED_MIMES: Record<string, string> = {
|
||||
jpeg: 'image/jpeg',
|
||||
jpg: 'image/jpeg',
|
||||
png: 'image/png',
|
||||
};
|
||||
|
||||
async function base64(file: string): Promise<string> {
|
||||
let extension = path.extname(file) || '';
|
||||
extension = extension.split('.').pop()!;
|
||||
|
||||
if (!SUPPORTED_MIMES[extension]) {
|
||||
throw new Error(ERROR_EXT);
|
||||
}
|
||||
|
||||
const data = await sharp(file).resize(10).toBuffer();
|
||||
if (data) {
|
||||
return toBase64(SUPPORTED_MIMES[extension], data);
|
||||
}
|
||||
throw new Error('Unhandled promise rejection in base64 promise');
|
||||
}
|
||||
|
||||
async function palette(file: string): Promise<string[]> {
|
||||
const vibrant = new Vibrant(file, {});
|
||||
const pal = await vibrant.getPalette();
|
||||
if (pal) {
|
||||
return toPalette(pal);
|
||||
}
|
||||
throw new Error(`Unhandled promise rejection in colorPalette ${pal}`);
|
||||
}
|
||||
|
||||
process.on('unhandledRejection', (up) => {
|
||||
throw up;
|
||||
});
|
||||
|
||||
export {base64, palette};
|
Loading…
Add table
Add a link
Reference in a new issue