rollup-plugin-jscc
raw JSON → 2.0.0 verified Mon Apr 27 auth: no javascript maintenance
Rollup plugin for conditional compilation and compile-time variable replacement using jscc. Current stable version is 2.0.0, released 2020-08-28, with a maintenance cadence (last major release). Key differentiators: runs before transpilers as a file loader (or optionally as transformer), supports TypeScript definitions, sourcemaps, and uses full JavaScript expressions for conditional blocks. Unlike other preprocessors, it is language agnostic and tightly integrated with Rollup v2+, requiring Node >=10.12.0.
Common errors
error Error: RollupError: Could not resolve './some-module' from src/main.js ↓
cause Conditional import not excluded because the preprocessor directive is incorrectly placed or missing.
fix
Ensure conditional blocks use correct jscc syntax, e.g., /*#if _DEBUG ... //#else ... //#endif*/
error TypeError: Cannot read property 'unset' of undefined ↓
cause Using an older version of jscc that lacks certain features or using incorrect variable names.
fix
Update to rollup-plugin-jscc v2.0.0 and ensure variable names match pattern /^_[0-9A-Z][_0-9A-Z]*$/.
error SyntaxError: Invalid or unexpected token ↓
cause Directives like //#if not recognized because the prefix does not match default (default: ['//', '/*', '<!--']).
fix
Set 'prefixes' option to include your chosen comment prefix, e.g., prefixes: ['//#', '/*#'].
Warnings
breaking Requires Rollup v2 and Node.js >=10.12.0. Older versions of rollup-plugin-jscc (v1.x) were compatible with Rollup v1. ↓
fix Update Rollup to v2+ and Node.js to >=10.12.0.
deprecated The asloader option is deprecated. Use the default loader behavior instead. ↓
fix Remove 'asloader' option from plugin config.
gotcha Variable names must match regex /^_[0-9A-Z][_0-9A-Z]*$/. Predefined variables like _FILE and _VERSION are automatically available. ↓
fix Use uppercase names starting with underscore for custom variables.
gotcha The removal of non-jscc comments is not supported. Use rollup-plugin-cleanup if needed. ↓
fix Add rollup-plugin-cleanup to remove JavaScript comments.
breaking v2.0.0 removed support for CommonJS output from the plugin itself; it only provides ESM. ↓
fix Use 'import' syntax to include the plugin in your rollup config.
Install
npm install rollup-plugin-jscc yarn add rollup-plugin-jscc pnpm add rollup-plugin-jscc Imports
- jscc wrong
const jscc = require('rollup-plugin-jscc')correctimport jscc from 'rollup-plugin-jscc' - jscc wrong
import { jscc } from 'rollup-plugin-jscc'correctimport jscc from 'rollup-plugin-jscc' - type JsccOptions wrong
import { JsccOptions } from 'rollup-plugin-jscc'correctimport type { JsccOptions } from 'rollup-plugin-jscc'
Quickstart
// rollup.config.js
import jscc from 'rollup-plugin-jscc'
export default {
input: 'src/main.js',
output: { file: 'dist/bundle.js', format: 'esm' },
plugins: [
jscc({
values: {
_APPNAME: 'My App',
_DEBUG: process.env.DEBUG ? 1 : 0
}
})
]
}
// src/main.js
/*#if _DEBUG
console.log('Debug mode');
//#else
console.log('Release mode');
//#endif*/
console.log('$_APPNAME v$_VERSION');
/* Output when DEBUG is truthy:
console.log('Debug mode');
console.log('My App v2.0.0');
*/