preproc

Preproc.js is the anything preprocessor for node.js. Preprocess any file performing inclusions and conditional compilation.

Preproc.js

Preproc.js is the anything preprocessor for node.js. Preprocess any file performing inclusions and conditional compilation.

npm install preproc
//= include "mylib.coffee" 
 
MyLib.doSomething();

Define delimiters for each file format and choose how to transform sources before they are included into other files

var preproc = require('preproc');
 
var builder = new preproc.Builder({
  types: {
    coffeescript: {
      delimiters: ["#="],
      extensions: ['.coffee'],
      to: {
        javascriptfunction(content) {
          return coffee.compile(content, {
            bare: true
          });
        }
      }
    },
    javascript: {
      delimiters: ["//="],
      extensions: ['.js'],
      to: {
        javascriptfunction(content) {
          return ";" + content + ";";
        },
        coffeescriptfunction(content) {
          return "\`" + content + "\`\n";
        },
        htmlfunction(content) {
          return "<script>\\\\<![CDATA[\n" + content + "\n\\\\]]></script>";
        }
      }
    },
    html: {
      delimiters: ["<!--=", "-->"],
      extensions: ['.html']
    }
  }
});
 

Note: Preproc.js ships with a default configuration. See https://github.com/mcasimir/preprocjs/blob/master/src/defaults.coffee for more.

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>My App</title>
</head>
<body>
<!--= include "templates/*.hbs" -->
</body>
</html>
//= require "mylib.js" 
//= require "vendor/lib.js" --skip 
builder.setEnv({debug: true});
var myVal = fn();
 
//= if debug 
console.log("myVal is: " + myVal);
//= end 
//= if target == 'client' 
 
$.get("/data", function(data){
  //... 
});
 
//= else if target == 'server' 
 
Db.queryData(function(data){
  //... 
});
 
//= end 
  • ! or not
  • & or and
  • || or or
  • < or lt
  • > or gt
  • <= or lte
  • >= or gte
  • == or eq
  • != or neq
npm install preproc

Read http://mcasimir.github.io/preprocjs/getting_started.html for more


Copyright (c) 2013 mcasimir

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.