@azerion/bunosaurus-lqip-loader 0.0.0-stage → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -2
- package/lib/index.d.ts +16 -0
- package/lib/index.js +43 -0
- package/lib/lqip.d.ts +7 -0
- package/lib/lqip.js +42 -0
- package/package.json +30 -4
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@docusaurus/lqip-loader`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Low Quality Image Placeholders (LQIP) loader for webpack.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save-dev @docusaurus/lqip-loader
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Example
|
|
12
|
+
|
|
13
|
+
Generating Base64 for a jpeg image imported in your JS bundle:
|
|
14
|
+
|
|
15
|
+
> The large image file will be emitted & only 400byte of Base64 (if set to true in the loader options) will be bundled.
|
|
16
|
+
|
|
17
|
+
### `webpack.config.js`
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
{
|
|
21
|
+
// OPTION A: default file-loader fallback
|
|
22
|
+
test: /\.jpe?g$/,
|
|
23
|
+
loaders: [
|
|
24
|
+
{
|
|
25
|
+
loader: '@docusaurus/lqip-loader',
|
|
26
|
+
options: {
|
|
27
|
+
path: '/media', // your image going to be in media folder in the output dir
|
|
28
|
+
name: '[name].[ext]', // you can use [contenthash].[ext] too if you wish,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
// OPTION B: Chained with your own url-loader or file-loader
|
|
34
|
+
test: /\.(png|jpe?g)$/,
|
|
35
|
+
loaders: [
|
|
36
|
+
'@docusaurus/lqip-loader',
|
|
37
|
+
{
|
|
38
|
+
loader: 'url-loader',
|
|
39
|
+
options: {
|
|
40
|
+
limit: 8000
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### `your-app-module.js`
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
import banner from './images/banner.jpg';
|
|
51
|
+
|
|
52
|
+
console.log(banner.preSrc);
|
|
53
|
+
// outputs: "data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhY....
|
|
54
|
+
|
|
55
|
+
console.log(banner.src); // that's the original image URL to load later!
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Important note
|
|
59
|
+
|
|
60
|
+
To save memory and improve GPU performance, browsers (including Chrome started from 61.0.3163.38) will now render a slightly more crisp or pixelated Base64 encoded images. If you want the blur to be very intense (smooth), here's a fix!
|
|
61
|
+
|
|
62
|
+
```css
|
|
63
|
+
img {
|
|
64
|
+
filter: blur(25px);
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
More history about the issue can be [found here](https://bugs.chromium.org/p/chromium/issues/detail?id=771110#c3) and [here](https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/6L_3ZZeuA0M).
|
|
69
|
+
|
|
70
|
+
## Credits
|
|
71
|
+
|
|
72
|
+
This package has been imported from [`@endiliey/lqip-loader`](https://github.com/endiliey/lqip-loader) which was a fork of the original [`lqip-loader`](https://github.com/zouhir/lqip-loader) created exclusively for Docusaurus.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { LoaderContext } from 'webpack';
|
|
8
|
+
type Options = {
|
|
9
|
+
base64: boolean;
|
|
10
|
+
palette: boolean;
|
|
11
|
+
};
|
|
12
|
+
declare function lqipLoader(this: LoaderContext<Options>, contentBuffer: Buffer): Promise<void>;
|
|
13
|
+
export default lqipLoader;
|
|
14
|
+
declare namespace lqipLoader {
|
|
15
|
+
var raw: boolean;
|
|
16
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.default = lqipLoader;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const lqip = tslib_1.__importStar(require("./lqip"));
|
|
12
|
+
async function lqipLoader(contentBuffer) {
|
|
13
|
+
this.cacheable();
|
|
14
|
+
const callback = this.async();
|
|
15
|
+
const imgPath = this.resourcePath;
|
|
16
|
+
let content = contentBuffer.toString('utf8');
|
|
17
|
+
const contentIsUrlExport = /^(?:export default|module.exports =) "data:.*base64,.*/.test(content);
|
|
18
|
+
const contentIsFileExport = /^(?:export default|module.exports =) .*/.test(content);
|
|
19
|
+
let source;
|
|
20
|
+
if (contentIsUrlExport) {
|
|
21
|
+
source = content.match(/^(?:export default|module.exports =) (?<source>.*)/).groups.source;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
if (!contentIsFileExport) {
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
26
|
+
const fileLoader = require('file-loader');
|
|
27
|
+
// @ts-expect-error: type is a bit unwieldy...
|
|
28
|
+
content = fileLoader.call(this, contentBuffer);
|
|
29
|
+
}
|
|
30
|
+
source = content.match(/^(?:export default|module.exports =) (?<source>.*);/).groups.source;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const preSrc = await lqip.base64(imgPath);
|
|
34
|
+
const finalObject = JSON.stringify({ src: 'STUB', preSrc });
|
|
35
|
+
const result = `module.exports = ${finalObject.replace('"STUB"', source)};`;
|
|
36
|
+
callback(null, result);
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
console.error(err);
|
|
40
|
+
callback(new Error('ERROR'), undefined);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
lqipLoader.raw = true;
|
package/lib/lqip.d.ts
ADDED
package/lib/lqip.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.base64 = base64;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
12
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
13
|
+
const sharp_1 = tslib_1.__importDefault(require("sharp"));
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
15
|
+
const { version } = require('../package.json');
|
|
16
|
+
const ERROR_EXT = `Error: Input file is missing or uses unsupported image format, lqip v${version}`;
|
|
17
|
+
const SUPPORTED_MIMES = {
|
|
18
|
+
jpeg: 'image/jpeg',
|
|
19
|
+
jpg: 'image/jpeg',
|
|
20
|
+
png: 'image/png',
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* It returns a Base64 image string with required formatting to work on the web
|
|
24
|
+
* (<img src=".." /> or in CSS url('..'))
|
|
25
|
+
*/
|
|
26
|
+
const toBase64 = (extMimeType, data) => `data:${extMimeType};base64,${data.toString('base64')}`;
|
|
27
|
+
async function base64(file) {
|
|
28
|
+
let extension = node_path_1.default.extname(file);
|
|
29
|
+
extension = extension.split('.').pop();
|
|
30
|
+
const mime = SUPPORTED_MIMES[extension];
|
|
31
|
+
if (!mime) {
|
|
32
|
+
throw new Error(ERROR_EXT);
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const data = await (0, sharp_1.default)(file).resize(10).toBuffer();
|
|
36
|
+
return toBase64(mime, data);
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
logger_1.default.error `Generation of base64 failed for image path=${file}.`;
|
|
40
|
+
throw err;
|
|
41
|
+
}
|
|
42
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azerion/bunosaurus-lqip-loader",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "Low Quality Image Placeholders (LQIP) loader for webpack.",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"watch": "tsc --watch"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/azerion/bunosaurus.git",
|
|
16
|
+
"directory": "packages/lqip-loader"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@docusaurus/logger": "npm:@azerion/bunosaurus-logger@4.0.0",
|
|
21
|
+
"file-loader": "^6.2.0",
|
|
22
|
+
"sharp": "^0.35.1",
|
|
23
|
+
"tslib": "^2.8.1"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"bun": ">=1.4.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/file-loader": "^5.0.4",
|
|
30
|
+
"webpack": "^5.111.0"
|
|
31
|
+
}
|
|
32
|
+
}
|