Skip to content

Commit 110f14a

Browse files
feat: __UNLAZY_LOGGING__ build flag (closes #36)
1 parent e718aae commit 110f14a

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

docs/advanced/build-flags.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ Most bundlers provide a `define` option to set build flags:
88
- [@rollup/plugin-replace](https://www.npmjs.com/package/@rollup/plugin-replace)
99
- [esbuild](https://esbuild.github.io/api/#define)
1010

11-
## Disable Hash Decoding <Badge type="info" text="^0.10.0" />
12-
13-
unlazy ships with the [BlurHash](/placeholders/blurhash) and [ThumbHash](/placeholders/thumbhash) decoding algorithms to decode the hash values into images.
14-
15-
In case your project doesn't use these placeholders, you can disable the hash decoding algorithms to reduce the bundle size. Use the following build flags to tree-shake the hash decoding algorithms:
16-
17-
- `__ENABLE_HASH_DECODING__`: This flag is set to `true` by default.
18-
1911
As an example, you can tree-shake the hash decoding algorithms in Vite by setting the `define` option in your `vite.config.ts` file:
2012

2113
```ts
@@ -24,13 +16,36 @@ import { defineConfig } from 'vite'
2416

2517
export default defineConfig({
2618
define: {
27-
__ENABLE_HASH_DECODING__: false,
19+
// Defaults to `true`
20+
__UNLAZY_HASH_DECODING__: false,
21+
// Defaults to `true`
22+
__UNLAZY_LOGGING__: false,
2823
},
2924
})
3025
```
3126

27+
## Disable Hash Decoding <Badge type="info" text="^0.10.0" />
28+
29+
unlazy ships with the [BlurHash](/placeholders/blurhash) and [ThumbHash](/placeholders/thumbhash) decoding algorithms to decode the hash values into images.
30+
31+
In case your project doesn't use these placeholders, you can disable the hash decoding algorithms to reduce the bundle size. Use the following build flags to tree-shake the hash decoding algorithms:
32+
33+
- `__UNLAZY_HASH_DECODING__`: This flag is set to `true` by default.
34+
3235
::: warning
3336
This will only tree-shake the BlurHash and ThumbHash decoding algorithms when using the [`lazyLoad`](/api/lazy-load) method.
3437

3538
If you use either `unlazy/blurhash` or `unlazy/thumbhash` sub-path imports directly, the decoding algorithms will still be bundled.
3639
:::
40+
41+
## Disable Client Logging <Badge type="info" text="^0.10.2" />
42+
43+
unlazy will help you locate missing `data-src` or `data-srcset` attributes in your project by logging a warning in the browser console. An example warning message looks like this:
44+
45+
```bash
46+
[unlazy] Missing `data-src` or `data-srcset` attribute: <img>
47+
```
48+
49+
If you want to disable these warnings, you can use the following build flag:
50+
51+
- `__UNLAZY_LOGGING__`: This flag is set to `true` by default.

packages/core/src/lazyLoad.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export function lazyLoad<T extends HTMLImageElement>(
2929

3030
// Generate the blurry placeholder from a Blurhash or ThumbHash string if applicable
3131
if (
32-
// @ts-expect-error: Compile-time flag to exclude this code from the bundle
33-
(typeof __ENABLE_HASH_DECODING__ === 'undefined' || __ENABLE_HASH_DECODING__)
32+
// @ts-expect-error: Compile-time flag
33+
(typeof __UNLAZY_HASH_DECODING__ === 'undefined' || __UNLAZY_HASH_DECODING__)
3434
&& hash
3535
) {
3636
const placeholder = createPlaceholderFromHash({
@@ -45,7 +45,9 @@ export function lazyLoad<T extends HTMLImageElement>(
4545

4646
// Bail if the image doesn't provide a `data-src` or `data-srcset` attribute
4747
if (!image.dataset.src && !image.dataset.srcset) {
48-
console.error('[unlazy] Missing `data-src` or `data-srcset` attribute', image)
48+
// @ts-expect-error: Compile-time flag
49+
if (typeof __UNLAZY_LOGGING__ === 'undefined' || __UNLAZY_LOGGING__)
50+
console.error('[unlazy] Missing `data-src` or `data-srcset` attribute', image)
4951
continue
5052
}
5153

@@ -166,7 +168,9 @@ export function createPlaceholderFromHash(
166168
}
167169
}
168170
catch (error) {
169-
console.error(`Error generating ${hashType} placeholder:`, error)
171+
// @ts-expect-error: Compile-time flag
172+
if (typeof __UNLAZY_LOGGING__ === 'undefined' || __UNLAZY_LOGGING__)
173+
console.error(`Error generating ${hashType} placeholder:`, error)
170174
}
171175
}
172176

0 commit comments

Comments
 (0)