Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda