johannschopplich/unlazy

Don't include hashing algorithms, if not necessary

felixranesberger opened this issue · 4 comments

Describe the bug

When importing unlazy using import { lazyLoad } from 'unlazy', the Thumbhash and Blurhash algorithms for decoding the hash to a valid data uri are always included.

This may not always be necessary, since we can also create a valid data uri on the serverside.

I'm not sure if the __ENABLE_HASH_DECODING__ variable is exposed for the default build. If so, you can use a plugin like Rollup replace to conditionally set it to false, which would tree-shake the hashing logic.

Source:

// Compile-time flag to exclude BlurHash and ThumbHash from IIFE bundle
const __ENABLE_HASH_DECODING__ = true

If the variable is not exposed in the dist files, then we could enforce it to support tree-shaking.

@felixranesberger My approach above won't work, since the __ENABLE_HASH_DECODING__ variable will be dumped when bundling the library. Can you tell me which bundler you are using? There's still a workaround to exclude the hashing libraries.

I'm using Vite to bundle the JS.
So Rollup under the hood.

@felixranesberger I have added a guide which should meet your requirements: Build Flags