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

226
Views
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 answers
Answer question

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 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!