sveltebrowserifyes6-modules

How to import lz4 in the Svelte


My Problem is: I want to use the lz4 library in Svelte without Svelte/kit only in the browser.

I know in the docs that they use the require syntax, but it doesnt work. Because I can only use the module import/from. And if I try importing it, like:

Nothing worked its allways: Uncaught SyntaxError: ambiguous indirect export: default

Then I saw that all the node.js files get preprocessed by browserify.

Thats the command the lib uses:

browserify -r ./lib/utils-js.js:./utils -r buffer -r xxhashjs -r ./lib/lz4l.js:lz4 -o build/lz4.js lib/lz4l.js

But as I found out browserify doesnt support import/from so I need to use babelify.

Thats now my command to create the script:

browserify -r ./lib/utils-js.js:./utils -r buffer -r xxhashjs -r ./lib/lz4l.js:lz4 -s lz4 -t babelify -o build/lz4.js

But it still displays the same error when importing.


Solution

  • I got it working, but I am not satisfied with the solution:

    Just like in the docs I add a script at the index.html with a script:

    <script src="https://cdn.jsdelivr.net/npm/lz4@0.6.5"></script>

    and then just use it in your component like this:

    const LZ4 = require('lz4');

    I thought I need to import it with import/from and if there is any solution to achieve that it would be perfect. I tried it before with const LZ4 = require('lz4'); but i thought i need to put the path to the file and not lz4.