manzt / numcodecs.js

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

add sideEffects === false for webpack #11

Closed manzt closed 4 years ago

manzt commented 4 years ago

I'm not very experienced with webpack, but I saw this in the docs on tree-shaking:

The new webpack 4 release expands on this capability with a way to provide hints to the compiler via the "sideEffects" package.json property to denote which files in your project are "pure" and therefore safe to prune if unused.

Since all exports (codecs) are side effect free in numcodecs, I added this to the repo's package.json. This seemed to fix #9, removing unused codecs from the final webpack bundle in my brief experimenting:

index.js

import { Blosc } from 'numcodecs';

const codec = new Blosc();

webpack.config.js

const path = require('path');

module.exports = {
  entry: 'index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  mode: 'production',
  optimization: {
    minimize: false // just so I could inspect the bundled output
  }
}
manzt commented 4 years ago

@jbms - would you mind editing node_modules/numcodecs.js/package.json for your project, adding "sideEffects": false, and seeing if it fixes your issues? I will then merge and make a new release for you.

manzt commented 4 years ago

The "sideEffects" property also seems to be used by @rollup/plugin-node-resolve:

index.js

import { Blosc } from 'numcodecs';

const codec = new Blosc();

rollup.config.js

import resolve from '@rollup/plugin-node-resolve';

export default {
  input: './index.js',
  output: {
    file: './bundle.js',
    format: 'es'
  },
  plugins: [
    resolve()
  ]
}

$ rollup -c now produces an output with only blosc codec (which it did not before).

jbms commented 4 years ago

Thanks for looking into this. Yes, I can confirm that setting "sideEffects": false fixes the issue.

manzt commented 4 years ago

Great, thanks for checking!