0.2.0 • Published 2 years ago

esbuild-ifdef v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

esbuild-ifdef

This esbuild plugin allows you to include/exclude code from your bundle conditionally on compile time using /// #if comments.

Installation

# npm
npm install esbuild-ifdef

#yarn
yarn add esbuild-ifdef

Usage

Example:

/// #if NODE_ENV === "production"
console.log('production specific code');
/// #elif NODE_ENV === "debug"
console.log('debug specific code');
/// #else
/// #warning Unknown NODE_ENV
console.log('something else');
/// #endif

All directives accept any valid javascript expressions. If blocks can be nested.

Available comments

  • /// #if expression
  • /// #elseif expression, /// #elif expression
  • /// #else
  • /// #endif
  • /// #warning text, /// #warn text - shows up a warning at compile time
  • /// #error text, /// #err text - throws an error at compile time

Using plugin

esbuild.build({
    entryPoints: ['./index.js'],
    bundle: true,
    target: 'es6',
    outfile: './out.js',
    plugins: [
        ifdefPlugin({
            variables: {
                VARIABLE_NAME: 'variable value'
            },
            // ... plugin config
        })
    ]
});

Configuration

NameTypeDefaultDescription
verbosebooleanfalseEnabled logging of the included strings and expression results
fillWithSpacesbooleanfalseFill removed lines with spaces instead of commenting out
requireTripleSlashbooleantrueRequire usage of /// before directives
filePathRegExp/\.[jt]sx?/File matching RegExp.
regExpRegExp-Custom parsing RegExp. Overrides requireTripleSlash option. RegExp should have one group named token and one group named expression. Default RegExp for parsing triple slash directives is /\/\/\/[\s]*#(?<token>.*?)(?:[\s]+(?<expression>.*?))?[\s]*$/
variablesRecord<string, any>process.envVariables for the expressions

License

MIT

0.2.0

2 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.0

3 years ago