Skip to content

aMarCruz/jspreproc

Repository files navigation

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

  • 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

About

C-Style source file preprocessor and comments remover for JavaScript in JavaScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published