[Pkg-javascript-commits] [node-umd] 05/10: Use `options` object rather than `commonJS` bool

Bastien Roucariès rouca at moszumanska.debian.org
Mon Apr 17 07:38:13 UTC 2017


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

rouca pushed a commit to annotated tag 3.0.0
in repository node-umd.

commit d2cb52a2eefca22bdf932bfa0712d2b441212abd
Author: Forbes Lindesay <forbes at lindesay.co.uk>
Date:   Wed Feb 4 13:23:57 2015 +0000

    Use `options` object rather than `commonJS` bool
---
 README.md | 12 +++++++-----
 index.js  | 35 ++++++++++++++++++++---------------
 2 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/README.md b/README.md
index dcb8209..4b3a1e9 100644
--- a/README.md
+++ b/README.md
@@ -31,21 +31,23 @@ For examples, see the examples directory.  The CommonJS module format is also su
 
 ## API
 
-### umd(name, [commonJS = false], [source])
+options:
 
-  The `name` should the the name of the module.  Use a string like name, all lower case with hyphens instead of spaces.
+ - `commonJS` (default: `false`) - If commonJS is `true` then it will accept CommonJS source instead of source code which `return`s the module.
+
+### umd(name, [source], [options])
 
-  If CommonJS is `true` then it will accept CommonJS source instead of source code which `return`s the module.
+  The `name` should the the name of the module.  Use a string like name, all lower case with hyphens instead of spaces.
 
   If `source` is provided and is a string, then it is wrapped in umd and returned as a string.  If it is not provided, a duplex stream is returned which wraps the modules (see examples/build.js).
 
   Both commonJS and source are optional and can be provided in either order.
 
-### umd.prelude(module, [commonJS = false])
+### umd.prelude(module, [options])
 
   return the text which will be inserted before a module.
 
-### umd.postlude(module, [commonJS = false])
+### umd.postlude(module, [options])
 
   return the text which will be inserted after a module.
 
diff --git a/index.js b/index.js
index e221657..1917cac 100644
--- a/index.js
+++ b/index.js
@@ -5,7 +5,12 @@ var rfile = require('rfile');
 var uglify = require('uglify-js');
 var templateSTR = rfile('./template.js');
 
-function template(moduleName, cjs) {
+function template(moduleName, options) {
+  if (typeof options === 'boolean') {
+    options = {commonJS: options};
+  } else if (!options) {
+    options = {};
+  }
   var str = uglify.minify(
     templateSTR.replace(/\{\{defineNamespace\}\}/g, compileNamespace(moduleName)),
     {fromString: true}).code
@@ -13,42 +18,42 @@ function template(moduleName, cjs) {
   str[0] = str[0].trim();
   //make sure these are undefined so as to not get confused if modules have inner UMD systems
   str[0] += 'var define,module,exports;';
-  if (cjs) str[0] += 'module={exports:(exports={})};';
+  if (options.commonJS) str[0] += 'module={exports:(exports={})};';
   str[0] += '\n';
-  if (cjs) str[1] = 'return module.exports;' + str[1];
+  if (options.commonJS) str[1] = 'return module.exports;' + str[1];
   str[1] = '\n' + str[1];
   return str;
 }
 
-exports = module.exports = function (name, cjs, src) {
-  if (typeof cjs === 'string') {
-    var tmp = cjs;
-    cjs = src;
+exports = module.exports = function (name, options, src) {
+  if (typeof options === 'string') {
+    var tmp = options;
+    options = src;
     src = tmp;
   }
   if (src) {
-    return exports.prelude(name, cjs) + src + exports.postlude(name, cjs);
+    return exports.prelude(name, options) + src + exports.postlude(name, options);
   }
   var strm = through(write, end);
   var first = true;
   function write(chunk) {
-    if (first) strm.queue(exports.prelude(name, cjs));
+    if (first) strm.queue(exports.prelude(name, options));
     first = false;
     strm.queue(chunk);
   }
   function end() {
-    if (first) strm.queue(exports.prelude(name, cjs));
-    strm.queue(exports.postlude(name, cjs));
+    if (first) strm.queue(exports.prelude(name, options));
+    strm.queue(exports.postlude(name, options));
     strm.queue(null);
   }
   return strm;
 };
 
-exports.prelude = function (moduleName, cjs) {
-  return template(moduleName, cjs)[0];
+exports.prelude = function (moduleName, options) {
+  return template(moduleName, options)[0];
 };
-exports.postlude = function (moduleName, cjs) {
-  return template(moduleName, cjs)[1];
+exports.postlude = function (moduleName, options) {
+  return template(moduleName, options)[1];
 };
 
 

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



More information about the Pkg-javascript-commits mailing list