[Pkg-javascript-commits] [node-umd] 02/11: Release 1.0.0

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


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

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

commit c5eea7981d1660a9719d46cdfea549b5dcb56183
Author: ForbesLindesay <forbes at lindesay.co.uk>
Date:   Sat Mar 16 23:33:53 2013 +0000

    Release 1.0.0
---
 .gitignore             |   2 ++
 .npmignore             |   4 +++
 README.md              |  46 +++++++++++++++++++++++++++++++---
 UMD.png                | Bin 0 -> 21163 bytes
 examples/build.js      |  20 +++++++++++++++
 examples/cjs/bundle.js |   3 +++
 examples/cjs/index.js  |   1 +
 examples/raw/bundle.js |   3 +++
 examples/raw/index.js  |   1 +
 index.js               |  66 +++++++++++++++++++++++++++++++++++++++++++++++++
 package.json           |  27 ++++++++++++++++++++
 template.js            |  33 +++++++++++++++++++++++++
 12 files changed, 203 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore
index f356293..699b5d4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,5 @@ logs
 results
 
 npm-debug.log
+
+node_modules
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..b0a0925
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,4 @@
+node_modules
+examples
+.gitignore
+UMD.png
diff --git a/README.md b/README.md
index 18d9277..eb4bf55 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,44 @@
-umd
-===
+<img src="http://i.imgur.com/ypw29XY.png" align="right"/>
+# umd
 
-Universal Module Definition for use in automated build systems
\ No newline at end of file
+Universal Module Definition for use in automated build systems
+
+## Source Format
+
+In order for the UMD wrapper to work the source code for your module should `return` the export, e.g.
+
+```javascript
+function method() {
+  //code
+}
+method.helper = function () {
+  //code
+}
+return method;
+```
+
+For examples, see the examples directory.  The CommonJS module format is also supported by passing true as the second argument to methods.
+
+## API
+
+### umd(name, [commonJS = false], [source = Stream])
+
+  The `name` should the the name of the module.  Use a string like name, all lower case with hyphens instead of spaces.
+
+  If CommonJS is `true` then it will accept CommonJS source instead of source code which `return`s the module.
+
+  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])
+
+  return the text which will be inserted before a module.
+
+### umd.postlude(module, [commonJS = false])
+
+  return the text which will be inserted after a module.
+
+## License
+
+  MIT
\ No newline at end of file
diff --git a/UMD.png b/UMD.png
new file mode 100644
index 0000000..94f7298
Binary files /dev/null and b/UMD.png differ
diff --git a/examples/build.js b/examples/build.js
new file mode 100644
index 0000000..971dcaf
--- /dev/null
+++ b/examples/build.js
@@ -0,0 +1,20 @@
+var umd = require('../');
+var fs = require('fs');
+var join = require('path').join;
+
+fs.createReadStream(join(__dirname, 'cjs', 'index.js'))
+  .pipe(umd('common-js-module', true))
+  .pipe(fs.createWriteStream(join(__dirname, 'cjs', 'bundle.js')))
+  .on('close', end);
+
+fs.createReadStream(join(__dirname, 'raw', 'index.js'))
+  .pipe(umd('common-js-module', false))
+  .pipe(fs.createWriteStream(join(__dirname, 'raw', 'bundle.js')))
+  .on('close', end);
+
+var remaining = 2;
+function end() {
+  if (0 !== --remaining) return;
+  console.log(require('./cjs/bundle'));
+  console.log(require('./raw/bundle'));
+}
\ No newline at end of file
diff --git a/examples/cjs/bundle.js b/examples/cjs/bundle.js
new file mode 100644
index 0000000..4ceada7
--- /dev/null
+++ b/examples/cjs/bundle.js
@@ -0,0 +1,3 @@
+(function(e){if("function"==typeof bootstrap)bootstrap("common-js-module",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeCommonJsModule=e}else"undefined"!=typeof window?window.commonJsModule=e():global.commonJsModule=e()})(function(){var define,ses,bootstrap,module,exports;module={exports:(exports={})};
+module.exports = "common JS example";
+return module.exports;});
\ No newline at end of file
diff --git a/examples/cjs/index.js b/examples/cjs/index.js
new file mode 100644
index 0000000..d6ff481
--- /dev/null
+++ b/examples/cjs/index.js
@@ -0,0 +1 @@
+module.exports = "common JS example";
\ No newline at end of file
diff --git a/examples/raw/bundle.js b/examples/raw/bundle.js
new file mode 100644
index 0000000..cc2afd9
--- /dev/null
+++ b/examples/raw/bundle.js
@@ -0,0 +1,3 @@
+(function(e){if("function"==typeof bootstrap)bootstrap("common-js-module",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeCommonJsModule=e}else"undefined"!=typeof window?window.commonJsModule=e():global.commonJsModule=e()})(function(){var define,ses,bootstrap,module,exports;
+return "Raw example";
+});
\ No newline at end of file
diff --git a/examples/raw/index.js b/examples/raw/index.js
new file mode 100644
index 0000000..365e225
--- /dev/null
+++ b/examples/raw/index.js
@@ -0,0 +1 @@
+return "Raw example";
\ No newline at end of file
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..a388e07
--- /dev/null
+++ b/index.js
@@ -0,0 +1,66 @@
+var through = require('through');
+var rfile = require('rfile');
+var templateSTR = rfile('./template.js');
+var uglify = require('uglify-js');
+function template(moduleName, cjs) {
+  var str = uglify.minify(
+    templateSTR
+      .replace(/\{\{name\}\}/g, moduleName.toLowerCase())
+      .replace(/\{\{pascalcase\}\}/g, pascalCase(moduleName))
+      .replace(/\{\{camelcase\}\}/g, camelCase(moduleName)),
+    {fromString: true}).code
+    .split('source()')
+  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,ses,bootstrap,module,exports;';
+  if (cjs) str[0] += 'module={exports:(exports={})};';
+  str[0] += '\n';
+  if (cjs) 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;
+    src = tmp;
+  }
+  if (src) {
+    return exports.prelude(name, cjs) + src + exports.postlude(name, cjs);
+  } else {
+    var strm = through(write, end);
+    var first = true;
+    function write(chunk) {
+      if (first) strm.queue(exports.prelude(name, cjs));
+      first = false;
+      strm.queue(chunk);
+    }
+    function end() {
+      if (first) strm.queue(exports.prelude(name, cjs));
+      strm.queue(exports.postlude(name, cjs));
+      strm.queue(null);
+    }
+    return strm;
+  }
+};
+
+exports.prelude = function (moduleName, cjs) {
+  return template(moduleName, cjs)[0];
+};
+exports.postlude = function (moduleName, cjs) {
+  return template(moduleName, cjs)[1];
+};
+
+
+
+function pascalCase(name) {
+  name = name.replace(/\-([a-z])/g, function (_, char) { return char.toUpperCase(); });
+  name = name.replace(/[^a-zA-Z0-9]+/g, '');
+  return name.replace(/^[a-z]/, function (char) { return char.toUpperCase(); });
+}
+function camelCase(name) {
+  name = name.replace(/\-([a-z])/g, function (_, char) { return char.toUpperCase(); });
+  name = name.replace(/[^a-zA-Z0-9]+/g, '');
+  return name.replace(/^[A-Z]/, function (char) { return char.toLowerCase(); });
+}
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..a70dbef
--- /dev/null
+++ b/package.json
@@ -0,0 +1,27 @@
+{
+  "name": "umd",
+  "version": "1.0.0",
+  "description": "Universal Module Definition for use in automated build systems",
+  "main": "index.js",
+  "directories": {
+    "example": "examples"
+  },
+  "dependencies": {
+    "rfile": "~1.0.0",
+    "ruglify": "~1.0.0",
+    "through": "~2.2.7",
+    "uglify-js": "~2.2.5"
+  },
+  "devDependencies": {},
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/ForbesLindesay/umd.git"
+  },
+  "author": "ForbesLindesay",
+  "license": "MIT",
+  "readmeFilename": "README.md",
+  "gitHead": "7f34f679e59a5051e6d4fe7e6be9657a440bbc86"
+}
diff --git a/template.js b/template.js
new file mode 100644
index 0000000..adcd61b
--- /dev/null
+++ b/template.js
@@ -0,0 +1,33 @@
+;(function (f) {
+  // Montage Require
+  if (typeof bootstrap === "function") {
+    bootstrap("{{name}}", f);
+
+  // CommonJS
+  } else if (typeof exports === "object") {
+    module.exports = f();
+
+  // RequireJS
+  } else if (typeof define === "function" && define.amd) {
+    define(f);
+
+  // SES (Secure EcmaScript)
+  } else if (typeof ses !== "undefined") {
+    if (!ses.ok()) {
+      return;
+    } else {
+      ses.make{{pascalcase}} = f;
+    }
+
+  // <script>
+  } else {
+    if (typeof window !== "undefined") {
+      window.{{camelcase}} = f();
+    } else {
+      global.{{camelcase}} = f();
+    }
+  }
+
+})(function () {
+  source()//trick uglify-js into not minifying
+});
\ No newline at end of file

-- 
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