Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda