[Pkg-javascript-commits] [node-module-deps] 47/444: function transforms
Bastien Roucariès
rouca at moszumanska.debian.org
Fri Dec 15 09:47:42 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-module-deps.
commit 03b41bf8715569f622f3b595091268c56f268549
Author: James Halliday <mail at substack.net>
Date: Wed Feb 27 17:09:27 2013 -0800
function transforms
---
index.js | 1 +
readme.markdown | 10 ++++++----
test/tr_fn.js | 29 +++++++++++++++++++++++++++++
3 files changed, 36 insertions(+), 4 deletions(-)
diff --git a/index.js b/index.js
index 34d0ca0..9477a0b 100644
--- a/index.js
+++ b/index.js
@@ -121,6 +121,7 @@ module.exports = function (mains, opts) {
}
function makeTransform (file, tr) {
+ if (typeof tr === 'function') return tr(file);
var tout = through(), tin = through();
tin.pause();
diff --git a/readme.markdown b/readme.markdown
index 2c863b2..d7b978b 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -82,10 +82,12 @@ compile a language like [coffeescript](http://coffeescript.org/) on the fly or
if you want to load static assets into your bundle by parsing the AST for
`fs.readFileSync()` calls.
-Transforms are just strings. If the transform string doesn't have any
-whitespace in it, the transform is treated as a module name and if the name
-resolves rooted at the current file path, the module is expected to follow this
-format:
+If the transform is a function, it should take the `file` name as an argument
+and return a through stream that will be written file contents and should output
+the new transformed file contents.
+
+If the transform is a string, it is treated as a module name that will resolve
+to a module that is expected to follow this format:
``` js
var through = require('through');
diff --git a/test/tr_fn.js b/test/tr_fn.js
new file mode 100644
index 0000000..1a41e6a
--- /dev/null
+++ b/test/tr_fn.js
@@ -0,0 +1,29 @@
+var mdeps = require('../');
+var test = require('tap').test;
+var JSONStream = require('JSONStream');
+var packer = require('browser-pack');
+var through = require('through');
+
+test('transform', function (t) {
+ t.plan(3);
+ var p = mdeps(__dirname + '/files/tr_sh/main.js', {
+ transform: function (file) {
+ return through(function (buf) {
+ this.queue(String(buf)
+ .replace(/AAA/g, '5')
+ .replace(/BBB/g, '50')
+ );
+ });
+ },
+ transformKey: [ 'browserify', 'transform' ]
+ });
+ var pack = packer();
+
+ p.pipe(JSONStream.stringify()).pipe(pack);
+
+ var src = '';
+ pack.on('data', function (buf) { src += buf });
+ pack.on('end', function () {
+ Function('t', src)(t);
+ });
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-module-deps.git
More information about the Pkg-javascript-commits
mailing list