browser-amd
Advanced tools
Comparing version 1.0.0 to 1.0.1
51
amd.js
@@ -5,3 +5,5 @@ | ||
var definitions = {}, | ||
on = {}; | ||
on = {}, | ||
functionSignature = /^function[^(]+\((.*?)\)/, | ||
waitingFor = null; | ||
@@ -15,3 +17,3 @@ function trim (value) { | ||
factory = dependencies; | ||
if( typeof id === 'array' ) { | ||
if( id instanceof Array ) { | ||
dependencies = id; | ||
@@ -32,9 +34,16 @@ id = undefined; | ||
if( !dependencies ) { | ||
dependencies = factory.toString().match(/^function[^(]+\((.*?)\)/)[1].split(',').map(trim); | ||
dependencies = factory.toString().match(functionSignature)[1].split(',').map(trim); | ||
} | ||
require(dependencies, function () { | ||
if( id === undefined && waitingFor ) { | ||
id = waitingFor; | ||
waitingFor = null; | ||
} | ||
var module = { exports : {} }; | ||
require.call({ locals: { module: module, exports: module.exports } }, dependencies, function () { | ||
var result = factory.apply(arguments); | ||
if( id ) { | ||
definitions[id] = result; | ||
definitions[id] = result === undefined ? module.exports : result; | ||
if( on[id] ) { | ||
@@ -64,2 +73,5 @@ on[id].forEach(function (listener) { | ||
script.src = libUrl(dependence); | ||
script.onload = function () { | ||
waitingFor = dependence; | ||
}; | ||
document.head.appendChild(script); | ||
@@ -73,2 +85,22 @@ } | ||
function require (dependencies, callback) { | ||
var locals = this === root ? {} : this.locals || {}; | ||
if( typeof dependencies === 'string' ) { | ||
if( callback === undefined ) { | ||
return locals[dependencies] || definitions[dependencies]; | ||
} | ||
dependencies = [dependencies]; | ||
} else if( typeof dependencies === 'function' ) { | ||
callback = dependencies; | ||
dependencies = ['require', 'exports', 'module']; | ||
} | ||
if( !(dependencies instanceof Array) ) { | ||
throw new Error('missing list of dependencies'); | ||
} | ||
if( typeof callback !== 'function' ) { | ||
throw new Error('missing callback'); | ||
} | ||
var resolutions = [], | ||
@@ -78,5 +110,5 @@ resolved = []; | ||
dependencies.forEach(function (id, i, dependencies) { | ||
if( definitions[id] ) { | ||
if( locals[i] || definitions[id] ) { | ||
resolutions[i] = definitions[id]; | ||
resolutions[i] = locals[i] || definitions[id]; | ||
resolved[i] = true; | ||
@@ -93,2 +125,3 @@ | ||
}); | ||
loadLib(id); | ||
@@ -103,2 +136,6 @@ } | ||
require.toUrl = libUrl; | ||
definitions.require = require; | ||
root.define = define; | ||
@@ -105,0 +142,0 @@ root.require = require; |
{ | ||
"name": "browser-amd", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "simple browser amd loader", | ||
@@ -5,0 +5,0 @@ "main": "amd.js", |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
4308
3
133
3