Skip to content
ModularRocks

Slimfast for Node.js

Automatically modularise your codebase! Put your codebase on a diet!

Installation

npm install @modular-rocks/slimfast-node

or

yarn add @modular-rocks/slimfast-node

Disclaimer

This is experimental and still very much WIP, but its also very customisable, so its already ready to use and customise.

Usage

Slimfast is built on top of Workspace, specifically workspace-node. Make sure you’re familar with Workspace as its the Slimfast base (Its very simple!).

If you want to see its basic usage, take a look here.

    import Slimfast from '@modular-rocks/slimfast-node'

    const files: [string, string][] = [['/path/to/file', code]]; // example

    const opts: SlimFastOpts = {
      files, // optional, will use src and read the files if omitted
      src: '/path/to/directory',
      extensions: [],
      ignoredFiles: [],
      ignoredImports: [],
      packageContents: {},
    };

    const slimFast = new Slimfast(opts);
    await slimFast.run(); // wrap this in an async function if you want it to wait

It accepts the same options as a Workspace and uses Workspace to create a virtual copy of your codebase, called original and immediately creates a copy called refactored, which is changed when run is invoked.

Workspace pipeline

Workspace has a pipeline functionality of blocking functions that are invoked in order; none of the functions will begin until the last is complete.

Although the pipeline can be overridden in the options, the default pipeline for Slimfast is Extract, Name and Build, each of which can be overridden by passing a function under the corresponding name, placed directly in the main options hash:

OptionDescriptionType
extractExtracts AST PathNodes, invoking visitorsFunction
nameAdds a name and folder to each AST PathNodeFunction
buildBuilds the new ASTs by wrappinng the PathNodes into functions and applying the relevant import statementsFunction

Name is very simple and in most cases should be overridden - modern LLMs like GPT do a great job at naming functions.

Extract invokes an array of visitors, more on that in the next section.

Build has default functionality but can be customised with the following options (which can be placed directly in the main options hash):

OptionDescriptionType
wrapWraps the PathNode in an AST, with relevant `import statements“ to be placed into a newfileFunction
replaceReplaces the PathNode in the AST with a calleeFunction
functionGeneratorWraps the PathNode inside wrap in a functionFunction
jsxGeneratorWraps the PathNode inside wrap in a JSX component, if it contains JSXFunction
functionReplacerReplaces the PathNode in the AST with a callee functionFunction
jsxReplacerReplaces the PathNode in the AST with a callee component, if it contains JSXFunction

Visitors

The Extract function invokes visitors in order. Each Visitor visits the code and extracts a PathNode from the AST that matches a criteria defined by its contraints.

By default, only the Expression Visitor is added to the pipeline. But in general, the idea is to start with a general visitor, like the Expression Visitor and then follow on with more specific visitors by including them later in the array.

Each visitor is unique, with unique constraints, hard coded, and must extend from Visitor.

The Visitors array can be overridden in the main options:

OptionDescriptionType
visitorsAn array of visitors, each extending from VisitorFunction[]

Saving files after modularisation

After invoking the run method and the modularisation has taken place, you can run save on the refactored codebase to save the files.

slimfast.refactored.save();

Examples

Examples coming soon…

License

Apache 2.0