0.3.0 • Published 5 months ago

esbuild-ifdef-next v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

esbuild-ifdef-next

This is fork of the esbuild-ifdef plugin with the latest version of esbuild. 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-next

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.3.0

5 months ago