Search code examples
javascriptjsonecmascript-6rollupjses6-modules

Why does rollup.js not include - exclusively - exported values in its output?


I am new to Rollup - rollupjs.org - and don't understand why exported values are being removed; presumably by treeshaking during the rollup.

I have a data.json with some basic data that I will want to provide as part of a module:

{
    "colors": ["red", "green", "blue"],
    "shapes": ["circle", "triangle", "square"]
}

... and a module that will be exporting them:

import { colors, shapes } from './data.json';

function run() {
  console.log('Run, Forest, Run.');
}

export default run;

export {
  colors,
  shapes
};

Does rollup.js not recognize export statements as considered "use" so anything that is exclusively in there will get "treeshaken" out of the output?


Solution

  • If the entry module exports colors and shapes, that's considered 'using' those values. If some internal module exports them, they'll only be included if another module imports colors or shapes and uses them.

    Here's a demonstration of the first case (entry module exports the values), and here's a demonstration of the second case (the values are exported by not-main.js).

    If that's not what's happening in your case, it's possible you've encountered a bug, in which case please file an issue!