0

I'm having an issue tree-shaking with rollup. I exported a new function min that does basically nothing from my utils/index.js module. Along with it in that file there are a ton of imports and other exported functions. When I bundle I'm seeing a compiled file consisting of every external module I'm using as well as all the exported functions in utils/index.js where I'd expect only min to be n the bundle and no require statements. How can I enable tree-shaking? Is there a setting I'm missing?

Here's my entry file.

import { min } from '../utils'    
export default min

Here's my rollup.config.js file.

module.exports = {
  external: [],
  entry: './src/appProxypass/index.js',
  dest: './packages/proxypass-app/index.js',
  format: 'cjs',
  plugins: [
    // require('rollup-plugin-async')({
    //   exclude: 'node_modules/**'
    // }),
    // require('rollup-plugin-buble')
    // require('rollup-plugin-json')({
    //   // exclude: 'node_modules/**'
    //   include: 'node_modules/**'
    // }),
    // require('rollup-plugin-node-resolve')({
    //   // main: true,
    //   // skip: ['axios', 'sqlite3', 'aws-sign2', 'lodash']
    //   // exclude: './node_modules/axios/**',
    //   // extensions: [ '.js', '.json' ]
    // }),
    require('rollup-plugin-commonjs')({
      // exclude: 'node_modules/**',
    }),
    require("rollup-plugin-babel")({
      babelrc: false,
      // runtimeHelpers: true,
      externalHelpers: true,
      "presets": [
        "es2015-rollup",
        "stage-2"
      ],
      "plugins": [
        "external-helpers",
        "transform-async-to-generator",
        "syntax-async-functions",
        "transform-flow-strip-types",
        // "transform-runtime",
        "transform-class-properties",
      ],
      exclude: 'node_modules/**'
    }),
    // require('rollup-plugin-cleanup')()
  ]
}
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • 1
    See https://github.com/rollup/rollup/wiki/Troubleshooting#tree-shaking-doesnt-seem-to-be-working. In particular, a lot of CommonJS modules are fundamentally un-tree-shakeable, so if there is an import statement that leads to them they will be included even if their output isn't used. – Rich Harris Oct 02 '16 at 13:17
  • @richharris I always assumed that rollup would be able to detect the used code and be able to drop functions and imports that weren't used. In my case it shouldn't import any of those functions or imports because the exported function doesn't use them. Is there any way to make this work? – ThomasReggi Oct 02 '16 at 19:23
  • 1
    Impossible to say from the information at hand. Rollup can only eliminate code that it can *guarantee* isn't used and doesn't have side-effects. (CommonJS breaks those guarantees.) By the same logic, if any of your modules import external dependencies, the bundle as a whole will have the same external dependencies *whether or not they are actually used* because it can't guarantee they don't have side-effects (might be polyfills, etc). – Rich Harris Oct 02 '16 at 22:01
  • @RichHarris thanks for the clarification. There's currently no way to flag the commonjs plugin and say deliberately that there's no pollyfills / imports of commonjs modules that have side-effects? – ThomasReggi Oct 02 '16 at 22:11
  • No, but the [plugins](https://github.com/rollup/rollup/wiki/Plugins) API is very flexible – you could 'include' specific external modules in your bundle, but replace them with empty modules, if there's no way to just not import them in the first place – Rich Harris Oct 02 '16 at 23:11

0 Answers0