Détail du package

jspreproc

aMarCruz249MIT0.2.7

C-Style source file preprocessor and comments remover for JavaScript

code, comments, compact, compilation

readme

Build Status Test Coverage npm Version Downloads by Month License

jspreproc

Do you have this?

/**
 * The exposed tmpl function returns the template value from the cache, render with data.
 * @param   {string} str  - Expression or template with zero or more expressions
 * @param   {Object} data - For setting the context
 * @returns {*} Raw expression value or template to render
 * @private
 */
function _tmpl(str, data) {
  if (!str) return str  // catch falsy values here

  //#if DEBUG
  if (data && data._debug_) {
    data._debug_ = 0
    if (!_cache[str]) {
      _cache[str] = _create(str, 1)  // request debug output
      var rs = typeof riot === 'undefined' ?
        '(riot undefined)' : JSON.stringify(riot.settings)
      console.log('--- DEBUG' +
        '\n riot.settings: ' + rs + '\n data: ' + JSON.stringify(data))
    }
  }
  //#endif

  // At this point, the expressions must have been parsed, it only remains to construct
  // the function (if it is not in the cache) and call it to replace expressions with
  // their values. data (`this`) is a Tag instance, logErr is the error handler.

  return (_cache[str] || (_cache[str] = _create(str))).call(data, logErr)
}
// end of _tmpl

And want this?

function _tmpl(str, data) {
  if (!str) return str
  return (_cache[str] || (_cache[str] = _create(str))).call(data, logErr)
}

Me too. This is why jspreproc, a tiny, C-Style source file preprocessor in JavaScript for JavaScript, with duplicate empty lines and comments remover.

Featuring many of the C preprocessor characteristics through JavaScript comments, jspreproc can be used in any source with a JavaScript-like syntax, even C# files with some limitations.

Important:

From version 0.2.1-beta.1 the location of jspp.js is the bin folder.

This is work in progress, so please update jspreproc constantly, I hope the first stable version does not take too long.

Install

You can install jspreproc with npm globally to use as the CLI tool, or locally for your project.
jspreproc works in node.js 0.10.0 or above (tested in Windows 7/8, cmd and sh shells).

Command-line

npm -g install jspreproc

jspreproc name for command-line interface is jspp

jspp [options] file1 file2 ...

Multiple files are concatenated into one, as if it had passed a file with multiple #include directives, one for each of the files listed.

If you don't list files, jspreproc reads from the standard input.
The result is written to the standard output, so it can be redirected.

Find more about the options in the documentation.

node.js

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

Parameter files can be an file name, an array of file names, or an instance of a readable stream. Options is an object with the same options from command-line.
jspp return value is a stream.PassThrough instance.

You can read about this API in the documentation.

There is a package for bower, too.

Documentation

This is a short example of basic syntax in files prepared for jspreproc:

//#include globals.inc
//#set DEBUG = 1

//#ifdef DEBUG
console.log(result)
//#endif

Find more in the Syntax guide.

Read the CHANGELOG for recent additions and fixes.
You can see jspreproc operation in the tests.

Please note: the documentation is very much a work in progress. Contributions are welcome.

Third-party tools & libraries

The following third-party tools and libraries are used by this project:

For regular use (dependencies installed by npm)

Development and code quality (devDependencies)

I'd like to thank all.

Known Issues

process.stdout fails (so jspreproc too) on console emulators for Windows, e.g. ConEmu and others, use clean Windows prompt or MSYS with mintty.

TODO

Maybe some day...

  • [x] 100% coverage (done)
  • [ ] Configuration from the file system. .jspreproc.json?
  • [ ] jspreproc reconfiguration through comments
  • [ ] #emit? for generating output of expression values
  • [ ] Better documentation
  • [ ] Explanatory error messages

Coverity Scan Build Status Code Climate Dependencies Development Dependencies

changelog

Changelog for jspreproc

Version 0.2.7 - 2016-01-05

  • Rewrite of the conditional comments' parser.
  • Enhancement: Multiline comment with if/ifdef/ifndef/elif/else allows hide conditional blocks. See doc/syntax.md
  • Added test with uglify-js

Version 0.2.6 - 2015-12-03

Version 0.2.5 - 2015-12-02

  • Fix: a regex after the return keyword is mistaken for a division operator
  • Enhancement: Most parser error messages include the source file name.

Version 0.2.4 - 2015-10-28

  • Enhancement: Experimental #indent directive (will be #pragma indent)

Version 0.2.3 - 2015-10-20

  • Fixes a bug replacing text that looks like jspp vars in quoted strings and regexes
  • Don't enclose headers in quotes, this is already a javascript string
  • First release with 100% coverage (by istanbul)
  • Makefile & travis.yml rewrite

Version 0.2.2 - 2015-10-16

  • First release with 98% coverage (by istanbul)
  • Updated readme and documentation
  • Fixed changelog

Version 0.2.2 beta 1 - 2015-10-15

  • Deprecated #define and #undef in favor of #set and #unset
  • More refactorization of tests
  • Enhancement: support for Date objects

Version 0.2.1 beta 3 - 2015-10-14

  • Many fixes and refactorization of tests
  • Preparation for coverage reports
  • Enhancement: Added the istanbul filter

Version 0.2.1 beta 2 - 2015-10-11

  • Fix to the options module

Version 0.2.1 beta 1 - 2015-10-11

  • Revision of package.json
  • Changed the location of jspp.js to the bin folder
  • New jspp.cmd for Windows user, local installation
  • Fix: missing doc folder
  • Fix: npm test is generating error

Version 0.2.0 beta 6 - 2015-10-11

Major code refactorization and more tests!

  • Fix: Code is mixed in the same line with files not ending with EOL
  • Fix: some escaped characters are not preserved
  • Fix: empty lines counter working almost perfect :)
  • Changed behavior: default indentation for included files is none

Version 0.2.0 beta 5 - 2015-10-10

  • Fix: substitution for '^' not working in headers

Version 0.2.0 beta 4 - 2015-10-10

  • Minor fix to README.md

Version 0.2.0 beta 3 - 2015-10-10

  • Fix: input stream (pipe) generates TypeError
  • Changes to documentation, I can take no much time on this

Version 0.2.0 beta 1 - 2015-10-09

Important upgrade: Corrections to the implementation of the stream returned and the program logic. This allows for asynchronous use and freedom in handling errors.

  • Fix: probable memory leaks
  • Fix: custom filters are active without using the --filter option
  • Fix: error catching in tests working now

Version 0.1.5 beta 4 - 2015-10-08 (unpublished)

  • Enhancement: added --custom-filters for command-line, customFilters for API
  • Fix? error catching in tests
  • Fix: character '^' not replaced in headers

Version 0.1.5 beta 3 - 2015-10-07

  • Fix: detection of keyword in correct place doesn't works sometimes
  • Enhancement: added detection of unclosed blocks

Version 0.1.5 beta 2 - 2015-10-01

  • Fix: regex for defines not trimming names
  • Fix: empty lines counter is even more precise

Version 0.1.5 beta 1 - 2015-10-01

  • Fix: empty lines counter is more precise now
  • Fix: process.on calling multiple times
  • Enhancement: code refactorization, more debug-friendly
  • Enhancement: use ^n in the header1 and headers values to insert line feeds
  • Enhancement: more clear output with the -h option
  • Added custom filters in TODO
  • More tests
  • Updated readme

Version 0.1.4 beta 4 - 2015-09-29

  • jasmine directory changed to spec
  • format with eslint rules

Version 0.1.4 beta 3 - 2015-09-29

  • Fix: path.isAbsolute does not exists in node 0.10, removed.

Version 0.1.4 beta 2 - 2015-09-28

  • Enhancement: header1 option, for setting the header of the top level file.
  • Enhancement: indent option, adds indentation to included files.
  • Fix: Incorrect value for __FILE - Delay evaluation if expression contains __FILE

Version 0.1.4 beta 1 - 2015-09-28

  • First tests with Jasmine (http://jasmine.github.io/)
  • Defines can include other defines, with immediate evaluation
  • Use of the prefix '$_' for replacement of defined symbols in the entire file.
  • Added "jscs" to comment filters (http://jscs.info/)
  • Added "titles" to comment filters, for markdown titles ('#' markers only)
  • Fix: "jsdocs" filter for comments renamed to "jsdoc"
  • Updated README

Version 0.1.3 beta 3 - 2015-09-25

  • Updated README
  • Fix: "jsdocs" filter for comments renamed to "jsdoc"

Version 0.1.3 beta 2 - 2015-09-24

  • Fix: include_once is lost with further includes

Version 0.1.3 beta 1 - 2015-09-24

  • Updated readme file
  • Minor improvement to main regex.

Version 0.1.3 beta 0 - 2015-09-24

First public release almost ready for production.

  • Fix: filename is restored returning from an included file.

Version 0.1.2 - 2015-09-24

  • Fix: include_once ignore already included files with the "include" keyword.
  • Fix: separator normalization in __FILE
  • Set minimist as dependency

Version 0.1.1 - 2015-09-23

First public release

  • Support for (almost) full C-like preprocessor keywords.
  • Comment filters for linters

Version 0.1.0-beta - 2015-08-21

  • Added very basic conditional compilation