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

216
Visualizações
How can I tree-shake a JavaScript AST in memory?

I know that tools like rollup, webpack and even babel are capable of producing tree-shaken bundles. But I would like to do that, but for an abstract syntax tree I parsed from a file, without writing to disk first. Does that make sense? Is it possible? Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

One of the ways you can achieve what you want is using 🐊Putout code transformer with a couple built-in plugins and one new:

export const fix = (path) => {
    path.replaceWith(path.node.declaration);
}
export const include = () => [
    'ExportDefaultDeclaration',
    'ExportNamedDeclaration'
];

That will remove all kind of exports, since we don't need it in our AST because of tree shake.

It looks this way:

treeshake using 🐊Putout

You can edit it in 🐊Putout Editor.

This is the whole tree shaker:

import putout from 'putout';

putout('your code', {
    plugins: [
        'remove-unused-variables',
        'remove-unused-expressions',
        'remove-unreachable-code',
        'remove-unreferenced-variables',
        ['remove-export', {
            fix: (path) => {
                path.replaceWith(path.node.declaration);
            },
            include: () => [
                'ExportDefaultDeclaration', 
                'ExportNamedDeclaration'
            ]
        }]
    ]
});

You can take a look to all unsafe transformations from eslint-plugin-putout.

🐊Putout is one of tools I'm working on and I'm always glad to help with any issues related to AST and code transformations.

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