0

I have an ES6 module with 4 primitive values stored in const variables at the top of my file, along with a default function being exported. This function is imported into main.js and Rollup bundles it into a corresponding dist/js folder. When I look at the outputted file in dist/js I notice that Rollup has included the 4 variables:

module.js:

const x = 1,
      y = 2,
      z = 3, 
      a = 4;

export default function startTimer() {
    console.log('test');
}

main.js:

import startTimer from './path/to/module.js'

dist/js/main.js:

var x = 1,
    y = 2,
    z = 3,
    a = 4;

function startTimer() {
    console.log('test');
}

Why are the variables being imported? The function is in no way related to the variables or vice versa.

Thanks!

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
jakewies
  • 342
  • 6
  • 18
  • Because rollup doesn't tree-shake statements with side effects (like those assignments), only declarations. See https://github.com/rollup/rollup/issues/551 – Bergi Nov 23 '16 at 04:46
  • @bergi Thanks, I'm going to need to read more about side effect prevention in JS because It's still quite fuzzy for me. – jakewies Nov 23 '16 at 21:26
  • What you are doing is not a side effect, it's just Rollup's fault not to recognise that (because in general it's really hard to do statically). – Bergi Nov 23 '16 at 21:46

0 Answers0