[Pkg-javascript-commits] [node-browserify-lite] 02/05: Imported Upstream version 0.3.0

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Tue Jul 7 21:12:19 UTC 2015


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

andrewrk-guest pushed a commit to branch master
in repository node-browserify-lite.

commit 910bbcc05f498c567fac512b93ce81e08498cbbd
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Tue Jul 7 14:04:22 2015 -0700

    Imported Upstream version 0.3.0
---
 README.md    | 11 ++++++++---
 cli.js       | 35 ++++++++++++++++++++---------------
 index.js     | 30 +++++++++++++++++-------------
 package.json |  2 +-
 test.js      |  8 +++++---
 5 files changed, 51 insertions(+), 35 deletions(-)

diff --git a/README.md b/README.md
index 5481f53..e33444e 100644
--- a/README.md
+++ b/README.md
@@ -4,8 +4,8 @@ browserify, minus some of the advanced features and heavy dependencies.
 
  * No builtin Node.js shims.
  * Naive AST tokenization for require instead of true AST parsing.
-   - Require statements must be outside all braces. This includes functions,
-     control statements, and objects.
+   - All require statements are found regardless of if they are in an `if`
+     statement or a function body that is never called.
  * Only supports a single entry file and the `--outfile` parameter,
    nothing else.
  * No source maps.
@@ -14,7 +14,12 @@ browserify, minus some of the advanced features and heavy dependencies.
 ## Usage
 
 ```
-browserify-lite ./src/app.js --outfile public/app.js
+browserify-lite ./entry-file.js --outfile bundle.js
+
+Standard Options:
+
+        --outfile         Write the browserify bundle to this file.
+        --standalone xyz  Export as a window global.
 ```
 
 ./src/app.js can depend on any modules using Node.js's
diff --git a/cli.js b/cli.js
index 9fcf3a6..a927460 100755
--- a/cli.js
+++ b/cli.js
@@ -3,25 +3,29 @@
 var fs = require('fs');
 var browserifyLite = require('./');
 
-var entrySourcePath = process.argv[2];
-var outputFileParam = process.argv[3];
-var outBundlePath = process.argv[4];
-
-if (entrySourcePath === '--help') {
-  usage();
-}
-
-if (outputFileParam !== '--outfile') {
-  console.error("Expected second param to be --outfile\n");
-  usage();
+var options = {};
+for (var i = 2; i < process.argv.length; i += 1) {
+  var arg = process.argv[i];
+  if (arg === '--help') {
+    usage();
+  } else if (arg === '--outfile') {
+    if (++i >= process.argv.length) usage();
+    options.outBundlePath = process.argv[i];
+  } else if (arg === '--standalone') {
+    if (++i >= process.argv.length) usage();
+    options.standalone = process.argv[i];
+  } else if (!options.entrySourcePath) {
+    options.entrySourcePath = arg;
+  } else {
+    usage();
+  }
 }
 
-if (!outBundlePath || !entrySourcePath) {
-  console.error("Expected first arg to be source path and third arg to be out bundle path.\n");
+if (!options.outBundlePath || !options.entrySourcePath) {
   usage();
 }
 
-browserifyLite.createBundle(entrySourcePath, outBundlePath, function(err) {
+browserifyLite.createBundle(options, function(err) {
   if (err) throw err;
 });
 
@@ -32,6 +36,7 @@ function usage() {
       "\n" +
       "Standard Options:\n" +
       "\n" +
-      "        --outfile  Write the browserify bundle to this file.");
+      "        --outfile         Write the browserify bundle to this file\n" +
+      "        --standalone xyz  Export as window.xyz");
   process.exit(1);
 }
diff --git a/index.js b/index.js
index 6371050..8572b2c 100644
--- a/index.js
+++ b/index.js
@@ -5,7 +5,11 @@ var Pend = require('pend');
 exports.extractRequires = extractRequires;
 exports.createBundle = createBundle;
 
-function createBundle(entrySourcePath, outBundlePath, cb) {
+function createBundle(options, cb) {
+  var entrySourcePath = options.entrySourcePath;
+  var outBundlePath = options.outBundlePath;
+  var standalone = options.standalone;
+
   // data structure that is filled up with canonical source path as the key,
   // source code as the value.
   var sources = {};
@@ -78,13 +82,17 @@ function createBundle(entrySourcePath, outBundlePath, cb) {
       }
     });
 
+    var standaloneLine = standalone ?
+      "  window." + standalone + " = req(entry);\n" :
+      "  req(entry);\n";
+
     var out =
       "(function(modules, cache, entry) {\n" +
-      "  req(entry);\n" +
+      standaloneLine +
       "  function req(name) {\n" +
       "    if (cache[name]) return cache[name].exports;\n" +
       "    var m = cache[name] = {exports: {}};\n" +
-      "    modules[name][0].call(m.exports, modRequire, m, m.exports);\n" +
+      "    modules[name][0].call(m.exports, modRequire, m, m.exports, window);\n" +
       "    return m.exports;\n" +
       "    function modRequire(alias) {\n" +
       "      var id = modules[name][1][alias];\n" +
@@ -95,7 +103,10 @@ function createBundle(entrySourcePath, outBundlePath, cb) {
       "})({";
 
     modules.forEach(function(canonicalSourcePath) {
-      out += aliases[canonicalSourcePath] + ": [function(require,module,exports){\n";
+      out += aliases[canonicalSourcePath] + ": [function(require,module,exports,global){\n";
+      if (canonicalSourcePath.match(/\.json$/)) {
+        out += "module.exports = ";
+      }
       out += sources[canonicalSourcePath];
       out += "\n}, " + JSON.stringify(depMap[canonicalSourcePath]) + "],";
     });
@@ -110,7 +121,6 @@ function createBundle(entrySourcePath, outBundlePath, cb) {
 function tokenizeSource(source) {
   var tokens = [];
   var inQuote = false;
-  var braceCount = 0;
   var quoteType;
   var qEscape = false;
   var token = "";
@@ -128,9 +138,7 @@ function tokenizeSource(source) {
         qEscape = true;
       } else if (c === quoteType) {
         inQuote = false;
-        if (braceCount === 0) {
-          tokens.push(token);
-        }
+        tokens.push(token);
         token = "";
       } else {
         token += c;
@@ -161,10 +169,8 @@ function tokenizeSource(source) {
       startComment = false;
       if (token) tokens.push(token);
       token = "";
-      braceCount += 1;
     } else if (c === '}') {
       startComment = false;
-      braceCount -= 1;
     } else if (c === '/') {
       if (startComment) {
         if (token) tokens.push(token);
@@ -179,7 +185,7 @@ function tokenizeSource(source) {
       token = "";
       inMultiLineComment = true;
       startComment = false;
-    } else if (braceCount === 0) {
+    } else {
       if (/\W/.test(c)) {
         if (token) tokens.push(token);
         token = "";
@@ -187,8 +193,6 @@ function tokenizeSource(source) {
       if (/\S/.test(c)) {
         token += c;
       }
-    } else {
-      startComment = false;
     }
   }
   if (token) tokens.push(token);
diff --git a/package.json b/package.json
index 4a24bf3..dfca706 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "browserify-lite",
-  "version": "0.2.4",
+  "version": "0.3.0",
   "description": "browserify, minus some of the advanced features and heavy dependencies.",
   "main": "index.js",
   "scripts": {
diff --git a/test.js b/test.js
index a791fbb..2e0832d 100644
--- a/test.js
+++ b/test.js
@@ -52,20 +52,22 @@ var extractRequiresTests = [
   },
   {
     name: "ignore braces",
-    source: "var foo = require('derp'); { require('ignore-this'); } require('this-ok')\n",
+    source: "var foo = require('derp'); { require('dont-ignore-this'); } require('this-ok')\n",
     output: [
       "derp",
+      "dont-ignore-this",
       "this-ok",
     ],
   },
   {
     name: "ignore comments",
-    source: "/* var foo = require('derp');*/ { require('ignore-this'); } require('this-ok') // require('also-ignore-this'); \n require('this-also-ok')",
+    source: "/* var foo = require('derp');*/ { require('dont-ignore-this'); } require('this-ok') // require('also-ignore-this'); \n require('this-also-ok')",
     output: [
+      "dont-ignore-this",
       "this-ok",
       "this-also-ok",
     ],
-  },
+  }
 ];
 
 process.stderr.write("extract requires tests:\n");

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



More information about the Pkg-javascript-commits mailing list