[Pkg-javascript-commits] [node-umd] 03/07: Compiles the namespaces at compile time.
Bastien Roucariès
rouca at moszumanska.debian.org
Mon Apr 17 07:38:09 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to annotated tag 2.0.0
in repository node-umd.
commit 771f90b0d7c0eea9203796f432bedf1dcd86d8bc
Author: Quildreen "Sorella" Motta <quildreen at gmail.com>
Date: Thu Nov 28 22:36:55 2013 -0200
Compiles the namespaces at compile time.
---
index.js | 39 +++++++++++++++++++++++++++++++++++++--
template.js | 17 +++++------------
test/index.js | 7 +++++++
3 files changed, 49 insertions(+), 14 deletions(-)
diff --git a/index.js b/index.js
index 9ae97a4..eb582e3 100644
--- a/index.js
+++ b/index.js
@@ -4,7 +4,7 @@ var templateSTR = rfile('./template.js');
var uglify = require('uglify-js');
function template(moduleName, cjs) {
var str = uglify.minify(
- templateSTR.replace(/\{\{name\}\}/g, camelCase(moduleName)),
+ templateSTR.replace(/\{\{defineNamespace\}\}/g, compileNamespace(moduleName)),
{fromString: true}).code
.split('source()')
str[0] = str[0].trim();
@@ -51,5 +51,40 @@ exports.postlude = function (moduleName, cjs) {
function camelCase(name) {
- return name.replace(/\-([a-z])/g, function (_, char) { return char.toUpperCase(); });
+ name = name.replace(/\-([a-z])/g, function (_, char) { return char.toUpperCase(); });
+ return name.replace(/[^a-zA-Z0-9]+/g, '')
+}
+
+
+function compileNamespace(name) {
+ var names = name.split('.')
+
+ // No namespaces, yield the best case 'global.NAME = VALUE'
+ if (names.length === 1) {
+ return 'g.' + camelCase(name) + ' = f()';
+
+ // Acceptable case, with reasonable compilation
+ } else if (names.length === 2) {
+ names = names.map(camelCase);
+ return '(g.' + names[0] + ' || (g.' + names[0] + ' = {})).' + names[1] + ' = f()';
+
+ // Worst case, too many namespaces to care about
+ } else {
+ return names.reduce(compileNamespaceStep, ['ref$ = g'])
+ .join(';\n ');
+ }
+}
+
+function compileNamespaceStep(code, name, i, names) {
+ var value
+
+ if (i === names.length - 1) {
+ value = 'f()';
+ } else {
+ value = '{}';
+ }
+
+ name = camelCase(name);
+ code.push('ref$ = (ref$.' + name + ' || (ref$.' + name + ' = ' + value + '))')
+ return code
}
diff --git a/template.js b/template.js
index 41b2e8d..45eb948 100644
--- a/template.js
+++ b/template.js
@@ -9,22 +9,15 @@
// <script>
} else {
+ var g
if (typeof window !== "undefined") {
- setup(window, "{{name}}", f())
+ g = window;
} else if (typeof global !== "undefined") {
- setup(global, "{{name}}", f())
+ g = global;
} else if (typeof self !== "undefined") {
- setup(self, "{{name}}", f())
- }
- }
-
- function setup(object, name, value) {
- var key
- var names = name.split('.')
- while (key = names.shift()) {
- object[key] = object[key] || (names.length? {} : value)
- object = object[key]
+ g = self;
}
+ {{defineNamespace}};
}
})(function () {
diff --git a/test/index.js b/test/index.js
index ca0e0bc..7b20234 100644
--- a/test/index.js
+++ b/test/index.js
@@ -2,6 +2,7 @@ var assert = require('assert')
var umd = require('../')
var src = umd('sentinel-prime', 'return "sentinel"')
var namespacedSrc = umd('sentinel.prime', 'return "sentinel"')
+var multiNamespaces = umd('a.b.c.d.e', 'return "sentinel"')
describe('with CommonJS', function () {
it('uses module.exports', function () {
@@ -42,4 +43,10 @@ describe('in the absense of a module system', function () {
Function('window', namespacedSrc)(glob)
assert(glob.sentinel.prime === 'sentinel')
})
+ it('creates proper multiple namespaces', function() {
+ var glob = {}
+ Function('window', multiNamespaces)(glob)
+ assert(glob.a.b.c.d.e === 'sentinel')
+ })
+
})
--
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