-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathimg.js
37 lines (29 loc) · 857 Bytes
/
img.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import path from 'node:path';
import assert from 'node:assert';
import {promisify} from 'node:util';
import {createRequire} from 'node:module';
const require = createRequire(import.meta.url);
const fromString = promisify(require('css-b64-images').fromString);
const ONE_KB = 2 ** 10;
const defaultOptions = {
maxSize: 100 * ONE_KB,
};
/**
* minify css data.
* if can not minify return data
*
* @param name
* @param data
* @param userOptions - (optional) object that may contain an `img` key with an object of options
*/
export default (name, data, userOptions) => {
const dir = path.dirname(name);
const dirRelative = `${dir}/../`;
const options = {
...defaultOptions,
...userOptions?.img || {},
};
assert(name);
assert(data);
return fromString(data, dir, dirRelative, options);
};