manzt / numcodecs.js

Buffer compression and transformation codecs
MIT License
29 stars 6 forks source link

Importing a single codec when using a browser build #9

Closed jbms closed 4 years ago

jbms commented 4 years ago

From node.js it is possible to import just a single codec, but this does not seem to work from a browser-targeted build using typescript/webpack. Typescript gives the error:

import {Blosc} from 'numcodecs/blosc';
Cannot find module 'numcodecs/blosc' or its corresponding type declarations.

Using

import {Blosc} from 'numcodecs';

works but also pulls in pako, which increases the bundle size a fair bit.

manzt commented 4 years ago

Hi, thanks for opening the issue. Yes, Node's conditional exports are not well supported by blunders currently (unfortunately). There is an issue here fore rollup, but it seems like it's landed in Webpack 5 from what I've been able to find.

Each codec is bundled separately using code splitting, and is the default export from module, so the import shouldn't have the brackets.

import Blosc from 'numcodecs/blosc';
// not { Blosc }

I'm not sure if webpack has tree-shaking, but it shouldn't have a hard time doing so. The main entry point is just exporting each of the separate codecs.

https://github.com/manzt/numcodecs.js/blob/9a3be86dba899c65bc791c2d3b138fc6f755723c/src/index.ts#L1-L4

manzt commented 4 years ago

If you continue to have issues with configuring webpack, you can import the esm module for blosc directly:

import Blosc from './node_modules/numcodecs/dist/blosc.mjs';