New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Treeshaking does not work when excluding components #718
Comments
@BafS thanks for submitting this issue, we'll take a deeper look and reply shortly |
Hi @BafS. The library is not designed to tree-shake individual entropy sources. We consider adding this feature in a future release, but we can't say when exactly and whether we'll do it. At the moment the only option for you is modifying the library source code. Remove the entropy sources that you don't need or add
Please notice that you use an undocumented API ( |
Thanks for your answer, another solution would be to clone the repository and import the needed typescript modules directly which is maybe the easiest to be sync with the library. |
If it can help anyone, I finally achieve it by doing a babel plugin to visit the AST and remove the nodes I don't need. For example, in your /**
* @param {string[]} functionsToRemove
*/
const removeFunctions = (functionsToRemove) => () => ({
visitor: {
/** @param {import('@babel/core').NodePath} path */
Identifier(path) {
// Remove the functions from "sources"
// https://github.com/fingerprintjs/fingerprintjs/blob/master/src/sources/index.ts
if (path.node.name === 'sources' && path.parent.type === 'VariableDeclarator') {
path.parent.init.properties.forEach((p, i) => {
if (functionsToRemove.includes(p.value.name)) {
path.parent.init.properties.splice(i, 1)
}
})
}
},
/** @param {import('@babel/core').NodePath} path */
FunctionDeclaration(path) {
// Remove the function
if (functionsToRemove.includes(path.node.id.name)) {
path.remove();
}
},
},
});
module.exports = {
// ...
module: {
rules: [
{
test: /fp.esm.js$/,
include: /node_modules/,
use: {
loader: "babel-loader",
options: {
plugins: [removeFunctions(['getAudioFingerprint', 'startRenderingAudio'])],
}
},
},
],
},
// ...
} Use at your own risks |
A related discussion: #757 |
A related discussion: #815 |
The documentation explains how to remove components (https://github.com/fingerprintjs/fingerprintjs/blob/master/docs/extending.md#extending-fingerprintjs) but unfortunately it doesn't seems to work well with treeshaking, the components stay in the compiled source.
It's possible to get the "
sources
" and whitelist components but the blobs are still not treeshaked. I tried with webpack 5.64 and rollup 2.42.Example of what I had in mind:
Another solution would be to be able to import every component individually (like
import { audio } from '...'
), threeshaking should work then.Thanks in advance
The text was updated successfully, but these errors were encountered: