Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/knpwrs/falafelify

A browserify transform which lets you run your JavaScript files through falafel.
https://github.com/knpwrs/falafelify

Last synced: 2 months ago
JSON representation

A browserify transform which lets you run your JavaScript files through falafel.

Awesome Lists containing this project

README

        

# falafelify [![Build Status](https://travis-ci.org/KenPowers/falafelify.svg?branch=master)](https://travis-ci.org/KenPowers/falafelify) [![Coverage Status](https://coveralls.io/repos/KenPowers/falafelify/badge.svg?branch=master)](https://coveralls.io/r/KenPowers/falafelify?branch=master)

Serve up fresh [falafel][f] from your [browserify][b] bundles.

## What?

This is a [browserify][b] [transform][t] that runs your files through
[falafel][f]. Basically you can alter your scripts by modifying the abstract
syntax tree that they are parsed in to.

## Installation

```sh
npm i --save falafelify
```

Or if you want something a little more fresh:

```sh
npm i --save KenPowers/falafelify
```

## Usage

Here are some examples that I ~~stole~~ ~~borrowed~~ adapted from [falafel][f]
as well as an original example (also available under the `examples`
directory):

### Wrap Arrays

This example shows how to wrap arrays (including nested arrays) in a function
call.

`arrays.js`:

```js
(function () {
var xs = [1, 2, [3, 4]];
var ys = [5, 6];
console.dir([xs, ys]);
})();
```

`build.js`:

```js
var browserify = require('browserify'),
falafelify = require('falafelify'),
fs = require('fs');

// Browserify build
browserify('./arrays')
.transform(falafelify(function (node) {
if (node.type === 'ArrayExpression') {
node.update('fn(' + node.source() + ')');
}
}))
.bundle()
.pipe(fs.createWriteStream('out.js'));
```

Run `node build` and get the following:

`out.js`:

```js
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o -1
&& node.left.type === 'Literal'
&& node.right.type === 'Literal';
}

// Async function which, given a BinaryExpression node, performs basic binary
// math expressions and updates the node with the calculated value.
function evaluate(node, done) {
var o = node.operator, left = node.left.value, right = node.right.value;
setTimeout(function () {
if (o === '+') {
node.update(left + right);
} else if (o === '-') {
node.update(left - right);
} else if (o === '*') {
node.update(left * right);
} else if (o === '/') {
node.update(left / right);
} else {
return done(new Error('Invalid operator.'));
}
// Make sure you ALWAYS call done.
done();
}, 250);
}

// Browserify build
browserify('./async')
// First argument is the iterator, second argument is the parallel limit.
.transform(falafelify(function (node, done) {
if (basicMath(node)) {
evaluate(node, done);
} else {
// Make sure you ALWAYS call done.
done();
}
}, 20))
.bundle()
.pipe(fs.createWriteStream('out.js'));
```

Run `node build` and get the following:

`out.js`:

```js
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o