Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Rollup - keep individual exports from tree-shaking

I am building a components library which is pre-compiled using Rollup.

My components lib has hundreds of files, at random depths, and I need all files with specific name pattern (*.constants.js) to not be tree-shaken (legacy backward-compatibility with another project which consumes the one I am working on)

Simplified example of my code

main.js (entry file)

import { A } from 'foo';
console.log(A)

foo.js

export const A = 1;
export const B = 1;
export const C = 1;

Rollup output /build/foo.js

const A = 1;

export { A };

A very important thing for me is to keep the ability of other codebases (which are using "my" components lib) to be able to import specific things from specific files, such as the above foo.js example

Question -
How do I keep the exports from being tree-shaken out of the output (only for *.constants.js in my real scenario)?


Rollup setup:

{
  input: 'src/main.js',
  output: {
    dir: 'build',
    format: 'es', // imperative
    chunkFileNames: '[name].js',
    entryFileNames: '[name].js',
    preserveModules: true,  // imperative
    preserveModulesRoot: 'src',  // imperative
    sourcemap: true,
    exports: 'named',
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I ended up doing something ugly, but minimal, by tricking rollup to treat tree-shaken variables as if they are used, but dumping them on the global this variable, under a random namespace:

For the code example in the question, where only A is really used internally by my components, lib, if I want to keep the exports for B & C I do:

globalThis.__rollup_no_tree_shaking = {B, C};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!