Comparing version 1.3.1 to 2.0.0
39
index.js
@@ -7,6 +7,3 @@ var through = require('through'); | ||
var str = uglify.minify( | ||
templateSTR | ||
.replace(/\{\{name\}\}/g, moduleName.toLowerCase()) | ||
.replace(/\{\{pascalcase\}\}/g, pascalCase(moduleName)) | ||
.replace(/\{\{camelcase\}\}/g, camelCase(moduleName)), | ||
templateSTR.replace(/\{\{defineNamespace\}\}/g, compileNamespace(moduleName)), | ||
{fromString: true}).code | ||
@@ -57,9 +54,33 @@ .split('source()') | ||
function pascalCase(name) { | ||
return camelCase(name).replace(/^[a-z]/, function (char) { return char.toUpperCase(); }); | ||
} | ||
function camelCase(name) { | ||
name = name.replace(/\-([a-z])/g, function (_, char) { return char.toUpperCase(); }); | ||
return name.replace(/[^a-zA-Z0-9]+/g, ''); | ||
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 { | ||
var valueContainer = names.pop() | ||
return names.reduce(compileNamespaceStep, ['var ref$ = g']) | ||
.concat(['ref$.' + camelCase(valueContainer) + ' = f()']) | ||
.join(';\n '); | ||
} | ||
} | ||
function compileNamespaceStep(code, name, i, names) { | ||
name = camelCase(name); | ||
code.push('ref$ = (ref$.' + name + ' || (ref$.' + name + ' = {}))') | ||
return code | ||
} |
{ | ||
"name": "umd", | ||
"version": "1.3.1", | ||
"version": "2.0.0", | ||
"description": "Universal Module Definition for use in automated build systems", | ||
@@ -5,0 +5,0 @@ "bin": "./bin/cli.js", |
@@ -0,0 +0,0 @@ # umd |
@@ -12,9 +12,11 @@ ;(function (f) { | ||
} else { | ||
var g | ||
if (typeof window !== "undefined") { | ||
window.{{camelcase}} = f(); | ||
g = window; | ||
} else if (typeof global !== "undefined") { | ||
global.{{camelcase}} = f(); | ||
g = global; | ||
} else if (typeof self !== "undefined") { | ||
self.{{camelcase}} = f(); | ||
g = self; | ||
} | ||
{{defineNamespace}}; | ||
} | ||
@@ -21,0 +23,0 @@ |
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"') | ||
@@ -39,2 +41,13 @@ describe('with CommonJS', function () { | ||
}) | ||
}) | ||
it('creates the proper namespaces', function() { | ||
var glob = {} | ||
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') | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9281
178
7