JSPM

  • Created
  • Published
  • Downloads 46
  • Score
    100M100P100Q65150F
  • License MIT

Source file preprocessor and comments remover for JavaScript in JavaScript

Package Exports

  • jspreproc

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jspreproc) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm version

jspreproc

A JavaScript source file preprocessor in pure JavaScript, with duplicate empty lines and comments remover.

Install

npm install jspreproc

Command-line

jspreproc name for command-line interface is jspp

jspp [options] file1 file2 ...
options description
-D, --define add a define for use in expressions (e.g. -D NAME=value)
type: string
--headers text to insert before each file.
type: string - default: '\n//// __FILE\n\n'
--eol-type normalize end of lines to "unix", "win", or "mac" style
type: string - default: unix
--empty-lines how much empty lines keep in the output (-1: keep all)
type: number - default: 1
-C, --comments treatment of comments, one of:
"all": keep all, "none": remove all, "filter": apply filter
type: string - default: filter
-F, --filter keep comments matching filter. "all" to apply all filters,
or one or more of: license, jsdocs, jslint, jshint, eslint
type: string - default: [license]
-V, --version print version to stdout and exit.
-h, --help display a short help
Example:
jspp -D DEBUG --filter jsdoc lib/myfile > tmp/myfile.js

node.js

var jspp = require('jspreproc')
var stream = jspp(files, options)

Parameter files can be an array, options is an object with the same options from command-line, but replace eol-type with eolType, and empty-lines with emptyLines. Example:

jspp('file1', {define:'NDEBUG', emptyLines: 0}).pipe(process.stdout)

return value is a stream.PassThrough instance.

Conditional Comments (CC)

Conditional Comments allows remove unused parts and build different versions of your application.

  • Keep CC in their own line, with no other tokens (only single-line comment).
  • CC keywords are case sensitive and must begin at the start of the comment.
  • Only spaces and tabs are allowed between the CC parts.

Keywords

CC follows the C preprocessor style, with the same keywords, preceded by //

//#if expression
//#elif __expression__

If the expression evaluates to falsy, the block of code that follows the statement is removed.

//#ifdef NAME
//#ifndef NAME

Test the existence of a defined name. These are shorthands for #if defined(NAME) and #if !defined(NAME).

//#else
//#endif

Default block and closing statements.

//#define NAME value  // value defaults to 1
//#undef NAME

Once declared, the DEFINEs are global to all files and their value can be changed at any time. Valid names for defines are all uppercase, starts with one character in the range [$_A-Z], followed by one or more of [_A-Z], so minimum length is 2. You can't use defines inside the values.

//#include filename
//#include_once filename

This statements inserts the content of another file. filename can be an absolute file name, or relative to the current file. Default extension is .js You can include the same file multiple times in the current file, but the statement is ignored if the same file was processed or included in a preceding level (avoids recursion). Use include_once to include only one copy by process.

Examples

//#if DEF == 1
  console.log('only preserved if DEF is 1')
//#endif
//# if (DEF)      // can have spaces between `#` and the keyword
  //# if FOO      // ...but not between the `//` and `#`
  //# endif
//# else
//#   if BAR
//#   endif
//# endif
//#if defined(DEF1) || defined(DEF2)  // defined() is supported
  console.log('DEF 1 or 2 defined')
//#elif defined(DEF3)
  console.log('DEF 3 defined')
//#endif
//#if DEBUG             // returns false if DEBUG is falsy or not defined
  console.log('info')
//#endif

Defines

//#define FOO "one"
//#define BAR 2
//#define DEBUG         // DEBUG value is 1
//#define FOO (1+2)     // redefine FOO

Includes

//#include myfile       // myfile.js in the same folder of current file
//#include_once ../myone
//#include myfile       // ok to insert again
//#include ../myone.js  // ignored

TODO

Tests, docs. If you wish and have time, help me improving this page... English is not my best friend.