[Pkg-javascript-commits] [node-browser-pack] 01/141: working example input and output

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:23:19 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-browser-pack.

commit d0df289e4a873ad632977e31578e4e9feaf933bc
Author: James Halliday <mail at substack.net>
Date:   Sun Feb 10 23:49:11 2013 -0800

    working example input and output
---
 cmd.js             |  6 ++++++
 example/input.json | 13 +++++++++++++
 example/output.js  |  1 +
 index.js           | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 74 insertions(+)

diff --git a/cmd.js b/cmd.js
new file mode 100644
index 0000000..da28f01
--- /dev/null
+++ b/cmd.js
@@ -0,0 +1,6 @@
+#!/usr/bin/env node
+
+var pack = require('./')();
+
+process.stdin.pipe(pack).pipe(process.stdout);
+process.stdin.resume();
diff --git a/example/input.json b/example/input.json
new file mode 100644
index 0000000..82eb21e
--- /dev/null
+++ b/example/input.json
@@ -0,0 +1,13 @@
+[
+  {
+    "id": "a1b5af78",
+    "source": "console.log(require('./foo')(5))",
+    "deps": { "./foo": "b8f69fa5" },
+    "entry": true
+  },
+  {
+    "id": "b8f69fa5",
+    "source": "module.exports = function (n) { return n * 111 }",
+    "deps": {}
+  }
+]
diff --git a/example/output.js b/example/output.js
new file mode 100644
index 0000000..386405b
--- /dev/null
+++ b/example/output.js
@@ -0,0 +1 @@
+(function(p,c,e){function r(n){if(!c[n]){c[n]={exports:{}};p[n][0](function(x){return r(p[n][1][x])},c[n],c[n].exports);}return c[n].exports}for(var i=0;i<e.length;i++)r(e[i]);return r})({"a1b5af78":[function(require,module,exports){console.log(require('./foo')(5))},{"./foo":"b8f69fa5"}],"b8f69fa5":[function(require,module,exports){module.exports = function (n) { return n * 111 }},{}]},{},["a1b5af78","b8f69fa5"])
\ No newline at end of file
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..6667595
--- /dev/null
+++ b/index.js
@@ -0,0 +1,54 @@
+var JSONStream = require('JSONStream');
+var duplexer = require('duplexer');
+var through = require('through');
+
+var prelude = [
+    '(function(p,c,e){',
+        'function r(n){',
+            'if(!c[n]){',
+                'c[n]={exports:{}};',
+                'p[n][0](function(x){',
+                    'return r(p[n][1][x])',
+                '},c[n],c[n].exports);',
+            '}',
+            'return c[n].exports',
+        '}',
+        'for(var i=0;i<e.length;i++)r(e[i]);',
+        'return r',
+    '})({'
+].join('');
+
+module.exports = function () {
+    var parser = JSONStream.parse([ true ]);
+    var output = through(write, end);
+    parser.pipe(output);
+    
+    var first = true;
+    var entries = [];
+    
+    return duplexer(parser, output);
+    
+    function write (row) {
+        if (first) output.emit('data', prelude);
+        
+        this.emit('data', [
+            (first ? '' : ','),
+            JSON.stringify(row.id),
+            ':[',
+            'function(require,module,exports){' + row.source + '}',
+            ',',
+            JSON.stringify(row.deps || {}),
+            ']'
+        ].join(''));
+        
+        first = false;
+        entries.push(row.id);
+    }
+    
+    function end () {
+        if (first) output.emit('data', prelude);
+        
+        this.emit('data', '},{},' + JSON.stringify(entries) + ')');
+        this.emit('end');
+    }
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-browser-pack.git



More information about the Pkg-javascript-commits mailing list