From 0e9b227114e77fe7bc81cef3195859b614cf0755 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Thu, 30 Jan 2014 20:12:02 +0200 Subject: [PATCH] Added transformation support (closes #1). --- README.md | 7 ++++++- lib/pure-cjs.js | 42 ++++++++++++++++++++++++++++++++---------- package.json | 6 +++--- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 25ee1e7..881897c 100644 --- a/README.md +++ b/README.md @@ -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 }); ``` diff --git a/lib/pure-cjs.js b/lib/pure-cjs.js index a796ee8..1307e39 100644 --- a/lib/pure-cjs.js +++ b/lib/pure-cjs.js @@ -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)?$/, @@ -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) { @@ -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; @@ -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) { @@ -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) { diff --git a/package.json b/package.json index 6af36bc..a026e70 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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" } }