A Rollup plugin that adds a C-like preprocessor to Javascript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

1.6 KiB

rollup-plugin-preprocessor

A Rollup plugin that adds a C-like preprocessor to Javascript.

Screenshots

Very useful comparison image (this is a lie)

Documentation

Options

This plugin exports a preprocess() plugin function that takes in an options object with the following syntax:

{
    //a list of compile-time variables and their values (parsed to an AST, so all JS is valid)
    //NOTE: due to this, strings require single quotes around them to parse to String
    values: {
        EXAMPLE: "'Hello, world!'",
        IS_TRUE: 1,
        WEBPACK: "demon.summon('modules/webpack')"
    },
    //a list of comment transformations
    transforms: {
        //example: this deletes all comments that start with NONPROD in the output AST
        NONPROD: (node, parent, idx) => {parent.comments = []}
    }
}

Included Directives

This plugin currently includes 3, battle-tested, production-ready directives for any code you may want to use them in.
#IF comptime_var - If comptime_var evaluates to true, the code from it until the end is included in the output; otherwise, it is ignored.
#IFNOT comptime_var - If comptime_var evaluates to false, the code from it until the end is included in the output; otherwise, it is ignored.
#END - Ends an #IF/#IFNOT macro.

Commonly Asked Questions

Why?

I prefer doing things like this before runtime with compile-time constants, instead of on runtime with JS variables.

Are you insane?

Yeah, probably.

Why not just use a Regex?

I'm just #special like that.

Is this production ready?

No.