macro

0.0.2 • Public • Published

Information

Packagemacrojs
Description Macros for nodejs
Node Version >= 0.4

Usage

Defining macros - .define

You only need to define your macros and use register() once when your app starts

macro = require 'macro'
 
macro.define 'debug', (node) ->
  out = "console.log('Debug at line #{node.line}');"
  out += "console.trace();"
  return out
 
macro.define 'topload', (path, node) -> return "require('../#{path}');"
macro.define 'lrequire', (path, node) ->  return "require('./#{path}');"
macro.define 'add', (numone, numtwo, node) -> return String(numone + numtwo)
macro.define '//', (comment, node) -> return "console.log('we swaggin');" if comment is '#SWAG'
macro.define '/*', (comments, node) -> return "console.log('swaggin hard bro');" if comments[0] is 'we swaggin'

will replace

//Cool comment
var appcfg = topload('config');
var dbcfg = lrequire('db_config');
 
//#SWAG
var sum = add(1, 2);
debug();
 
/* we swaggin
   swag hard
*/
var coolfn = function () {
  debug();
};
coolfn();

with

var appcfg = require("../config");
var dbcfg = require("./db_config");
 
console.log("we swaggin");
var sum = 3;
console.log("Debug at line 10");
console.trace();
 
console.log("swaggin hard bro");
var coolfn = function() {
    console.log("Debug at line 15");
    console.trace();
};
coolfn();

Registering files - .register()

All files loaded with require() will be passed through the macro transformer before being loaded.

This is what most people should be using.

macro = require 'macro'
macro.register()
# Define your macros here

Raw input - .process()

Pass in a string of JS, get out a string of transformed JS.

This is used behind the scenes with register() and only useful if you want to process your files at build-time.

macro = require 'macro'
fs = require 'fs'
# Define your macros here
 
input = fs.readFileSync "coolinput.js"
output = macro.process input
fs.writefileSync output, "coolinput.out.js"

Examples

You can view further examples in the example folder.

LICENSE

(MIT License)

Copyright (c) 2012 Fractal contact@wearefractal.com

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.

Readme

Keywords

none

Package Sidebar

Install

npm i macro

Weekly Downloads

8

Version

0.0.2

License

none

Last publish

Collaborators

  • fractal
  • yocontra