Skip to content

Commit

Permalink
Added transformation support (closes #1).
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Jan 30, 2014
1 parent 5180493 commit 0e9b227
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ cjs.transform({
input: 'superLib/topModule.js' /* String|Function(): input file */,
output: function (input) { return input.replace(/(\.js)?$/, '.out.js') } /* [?] String|Function(input): output file */,
map: function (input, output) { return output + '.map' } /* [?] String|Function(input, output): source map file */,
exports: 'window.SuperLib' /* [?] String|Function(input, output): Object to wrap and put exports from top module into */
exports: 'window.SuperLib' /* [?] String|Function(input, output): Object to wrap and put exports from top module into */,
transform: [] /* [?] Array|Function(input): Array of or single function that returns transformation [through](https://github.com/dominictarr/through)-stream to be used against input files.
}).then(function (options) {
// handle successful result with calculated paths
}, function (err) {
// handle error
});
```
Expand Down
42 changes: 32 additions & 10 deletions lib/pure-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ var fs = require('fs'),
pathUtils = require('path'),
nodeResolve = require('resolve'),
Promise = require('davy'),
es = require('event-stream'),

types = recast.types,
b = types.builders,
n = types.namedTypes,

whenReadFile = Promise.wrap(fs.readFile),
whenWriteFile = Promise.wrap(fs.writeFile),

jsExtRegExp = /(\.js)?$/,
Expand All @@ -34,9 +34,10 @@ function toValue(value) {
return value instanceof Function ? value.apply(null, Array.prototype.slice.call(arguments, 1)) : value;
}

function ReplacerMap() {
function ReplacerMap(options) {
this.replacers = {};
this.promises = [];
this.transform = options.transform;
}

ReplacerMap.prototype.get = function (path) {
Expand All @@ -45,7 +46,11 @@ ReplacerMap.prototype.get = function (path) {
}

var id = this.promises.length++,
replacer = this.replacers[path] = new Replacer(path, this, id);
replacer = this.replacers[path] = new Replacer({
id: id,
map: this,
path: path
});

this.promises[id] = replacer.promise;

Expand All @@ -67,15 +72,29 @@ ReplacerMap.prototype.whenAll = function (startIndex) {
};

var Replacer = recast.Visitor.extend({
init: function (path, map, id) {
this.id = id;
this.map = map;
this.path = path;
this.promise = whenReadFile(path, {encoding: 'utf-8'}).then(function (js) {
var ast = recast.parse(js, {sourceFileName: path}).program;
init: function (options) {
this.id = options.id;
this.map = options.map;
this.path = options.path;

var pipeline = [fs.createReadStream(this.path, {encoding: 'utf-8'})];

this.map.transform.forEach(function (transform) {
pipeline.push(transform(this.path));
}, this);

var promise = new Promise();
pipeline.push(es.wait(function (err, js) {
err ? promise.reject(err) : promise.fulfill(js);
}));

this.promise = promise.then(function (js) {
var ast = recast.parse(js, {sourceFileName: this.path}).program;
this.visit(ast);
return b.functionExpression(null, astModuleArgs, b.blockStatement(ast.body));
}.bind(this));

es.pipeline.apply(es, pipeline);
},

getDependency: function (path) {
Expand Down Expand Up @@ -127,7 +146,10 @@ exports.transform = function (inOptions) {

options.exports = toValue(inOptions.exports, options.input, options.output);

var map = new ReplacerMap(),
var transform = inOptions.transform;
options.transform = transform ? (transform instanceof Array ? transform : [transform]) : [];

var map = new ReplacerMap(options),
replacer = map.get(options.input);

return map.whenAll().then(function (modules) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "pure-cjs",
"version": "1.6.0",
"version": "1.7.0",
"description": "Pure minimalistic CommonJS builder",
"bin": "./bin/pure-cjs",
"main": "./lib/pure-cjs",
"preferGlobal": true,
"scripts": {
"test": "node ./bin/pure-cjs -i sample/a.js -m -e window.A"
},
Expand All @@ -21,6 +20,7 @@
"commander": "~2.1.0",
"resolve": "~0.6.1",
"davy": "0.0.8",
"recast": "~0.5.7"
"recast": "~0.5.7",
"event-stream": "~3.1.0"
}
}

0 comments on commit 0e9b227

Please sign in to comment.