Home

Awesome

rollup-plugin-jscc

<!-- prettier-ignore-start -->

npm Version Build Status AppVeyor Status Maintainability Test coverage License

<!-- prettier-ignore-end -->

Conditional compilation and compile-time variable replacement for Rollup.

rollup-plugin-jscc is not a transpiler, it is a wrapper of jscc, a tiny and powerful, language agnostic file preprocessor that uses JavaScript to transform text based on expressions at compile time.

With jscc, you have:

* This feature allows you the conditional declaration of ES6 imports (See the example).

Since jscc is a preprocessor, rollup-plugin-jscc is implemented as a file loader, so it runs before any transpiler and is invisible to them. This behavior allows you to use it in a wide range of file types but, if necessary, it can be used as a Rollup transformer instead of a loader.

NOTE

The removal of non-jscc comments is not included, but you can use rollup-plugin-cleanup, which brings compaction and normalization of lines in addition to the conditional removal of JS comments.

Support my Work

I'm a full-stack developer with more than 20 year of experience and I try to share most of my work for free and help others, but this takes a significant amount of time, effort and coffee so, if you like my work, please consider...

<img src="https://amarcruz.github.io/images/kofi_blue.png" height="36" title="Support Me on Ko-fi" />

Of course, feedback, PRs, and stars are also welcome 🙃

Thanks for your support!

Install

npm i rollup-plugin-jscc -D
# or
yarn add rollup-plugin-jscc -D

rollup-plugin-jscc requires rollup v2.0 and node.js v10.12 or later.

Usage

rollup.config.js

import { rollup } from 'rollup'
import jscc from 'rollup-plugin-jscc'

export default {
  input: 'src/main.js',
  plugins: [
    jscc({
      values: { _APPNAME: 'My App', _DEBUG: 1 },
    }),
  ],
  //...other options
}

in your source:

/*#if _DEBUG
import mylib from 'mylib-debug';
//#else */
import mylib from 'mylib'
//#endif

mylib.log('Starting $_APPNAME v$_VERSION...')

output:

import mylib from 'mylib-debug'

mylib.log('Starting My App v1.0.0...')

That's it.

* jscc has two predefined memvars: _FILE and _VERSION, in addition to giving access to the environment variables through the nodejs proccess.env object.

Options

Plain JavaScript object, with all properties optional.

NameTypeDescription
asloaderbooleanIf false, run the plugin as a transformer, otherwise run as loader (the default).
escapeQuotesstringString with the type of quotes to escape in the output of strings: 'single', 'double' or 'both'.<br>Default nothing.
keepLinesbooleanPreserves the empty lines of directives and blocks that were removed.<br>Use this option with sourceMap:false if you are interested only in keeping the line numbering.<br>Default false
mapHiresbooleanMake a hi-res source-map, if sourceMap:true (the default).<br>Default true
prefixesstring | RegExp |<br>Array<string|RegExp>The start of a directive. That is the characters before the '#', usually the start of comments.<br>Default ['//', '/*', '<!--'] (with one optional space after them).
sourcemapbooleanMust include a sourcemap?<br>Should be the same value as the property sourcemap of the Rollup output.<br>Default true
mapContentbooleanInclude the original source in the sourcemap, if sourceMap:true (the default).<br>Default true
valuesobjectPlain object defining the variables used by jscc during the preprocessing.<br>Default {}
extensionsstring | Array<string>Array of strings that specifies the file extensions to process.<br>Default ['js', 'jsx', 'ts', 'tsx', 'mjs', 'tag']
includestring | Array<string>minimatch or array of minimatch patterns for paths that must be included in the processing.
excludestring | Array<string>minimatch or array of minimatch patterns for paths that should be ignored.

Directives

Please see the jscc wiki to know about directives used by jscc.

What's New

Please see the CHANGELOG.

License

The MIT License