包详细信息

rollup-plugin-jscc

aMarCruz126.3kMIT2.0.0

Conditional comments and variable replacement for rollup, based on jscc

rollup, rollup-plugin, javascript, typescript

自述文件

rollup-plugin-jscc

npm Version Build Status AppVeyor Status Maintainability Test coverage License

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:

  • Conditional inclusion/exclusion of blocks, based on compile-time variables*
  • Compile-time variables with all the power of JavaScript expressions
  • Replacement of variables in the sources, by its value at compile-time
  • Sourcemap support, useful for JavaScript sources.
  • TypeScript v3 definitions

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

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.

Name Type Description
asloader boolean If false, run the plugin as a transformer, otherwise run as loader (the default).
escapeQuotes string String with the type of quotes to escape in the output of strings: 'single', 'double' or 'both'.
Default nothing.
keepLines boolean Preserves the empty lines of directives and blocks that were removed.
Use this option with sourceMap:false if you are interested only in keeping the line numbering.
Default false
mapHires boolean Make a hi-res source-map, if sourceMap:true (the default).
Default true
prefixes string | RegExp |
Array<string|RegExp>
The start of a directive. That is the characters before the '#', usually the start of comments.
Default ['//', '/*', '<!--'] (with one optional space after them).
sourcemap boolean Must include a sourcemap?
Should be the same value as the property sourcemap of the Rollup output.
Default true
mapContent boolean Include the original source in the sourcemap, if sourceMap:true (the default).
Default true
values object Plain object defining the variables used by jscc during the preprocessing.
Default {}
extensions string | Array<string> Array of strings that specifies the file extensions to process.
Default ['js', 'jsx', 'ts', 'tsx', 'mjs', 'tag']
include string | Array<string> minimatch or array of minimatch patterns for paths that must be included in the processing.
exclude string | 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

更新日志

rollup-plugin-jscc changes

[2.0.0] - 2020-08-28

Added

  • PrettierX + TypeScript for formating
  • markdownlint and prettierx config files
  • VS Code settings for this project

Changed

  • Update dependencies (using rollup ^2.26) and adjust rollup config
  • Update ESLint config, now PrettierX is used for code formating
  • Update code format to comply with prettierx rules
  • Require Rollup v2 & NodeJS v10 or above
  • Update test (remove NodeJS v6, add v14)
  • Update license

Fixed

  • PR #5: fix source map support - Thanks to @billowz
    • Upgrade rollup to 1.0.0
    • Added mapContent option
    • Added sourcemap test case
    • Fix source path in the sourcemap
  • Fix #8: if entry point is named '.mjs' it does not works by @muayyad-alsadi

[1.0.0] - 2018-11-23

Added

Changed

  • Only CommonJS version with dependency on jscc v1.1.0
  • The minimum supported version of node.js is 6
  • The predefined extensions were extended to include those of React and TypeScript.
  • RegEx and Date values now outputs its literal content in replacements.
  • Objects containing NaN now outputs NaN in replacements.
  • Infinite and -Infinity in JSON objects are replaced with Number.MAX_VALUE and Number.MIN_VALUE, respectively.

[0.3.3] - fix in jscc

  • Using jscc v0.3.3 that fixes bug in sourceMap generation.

[0.3.2] - sync with jscc

  • From this release, the version number will be in sync with jscc.
  • Updated devDependencies.

[0.2.2] - jscc own repo

  • The staring sequence of HTML comments ('<!--') is included in the predefined prefixes.
  • jscc was moved to its own github repository and has 100% coverage.

[0.2.1] - fixing issues

  • New predefined _VERSION varname contains version from the package.json file in the current working dir or some level up.
  • Fix the test of predefined _FILE varname.
  • Fix an issue with the space between keywords and expressions allowing line endings.

[0.2.0] - getting better

  • It is a complete rewrite, ready for inclussion in the rollup wiki.
  • Fix issues with bublé inclussion and regexes.js in Windows.

[0.1.3] - refactorization

  • More simple, less options, more tests.

[0.1.2] - html

  • This is the last version with removal of regular comments, please use rollup-plugin-cleanup for this.
  • Removes direct dependency on regexes (this will be a module in npm).
  • Fix regex for matching literal regexes skipping html tags.
  • Fix html test by removing new Date().

[0.1.1] - Fixes

Some fixes and support for html comments.

[0.1.0] - 2016-09-01

First public release