I currently have a TypeScript project with two main directories: `package/minimal` and `package/full`. The minimal version only includes a subset of the full version, and this minimal version is copied to another codebase periodically. This setup is necessary for access control, meaning certain users can only work with a limited portion of the code, and I cannot change this process. I'm looking for a solution that would allow me to use tools like Babel to process the full codebase and automatically strip it down to the minimal version by removing the unnecessary parts, such as lines checking `if (VERSION === 'full') {}` and any unused imports. Does anyone know of any tools or documentation that can help with this?
3 Answers
You might want to consider using TypeScript's Abstract Syntax Tree (AST). Many IDEs can strip out unused imports and you can modify the code by converting it to an AST, then writing out the changes. It's a bit complex but could work as a starting point.
Look into using Webpack's DefinePlugin or Vite's 'define' setting. This way, you can create two builds, one for minimal and one for full, and the treeshaking feature should handle unused code automatically.
That seemed like a solid plan at first, but I need the output in uncompiled TypeScript. I don't think Webpack can produce that, can it?
Have you thought about extracting the sensitive shared code into a private repository? You could then install it via your package manager, which might make things cleaner if it's possible within your constraints.

I get that it sounds complex; I was hoping there might be some scripts or articles online to help me get started without having to build everything from scratch.