browser-require
Advanced tools
Comparing version
@@ -13,10 +13,14 @@ var fs = require('fs') | ||
function fillinTemplate (module, src, deps) { | ||
function fillinTemplate (module, src, deps, isIndex) { | ||
var dir = isIndex | ||
? module.replace(/\.js$/, '').split('/') | ||
: module.split('/').slice(0, -1); | ||
var src = templates.src | ||
.replace('$src', src) | ||
.replace(/\$dir/g, JSON.stringify(module.split('/').slice(0, -1))); | ||
.replace(/\$dir/g, JSON.stringify(dir)); | ||
return templates.response | ||
.replace('$module', JSON.stringify(module)) | ||
.replace('$src', JSON.stringify(src)) | ||
.replace('$deps', JSON.stringify(deps)); | ||
.replace('$deps', JSON.stringify(deps)) | ||
.replace('$isIndex', isIndex); | ||
} | ||
@@ -93,14 +97,18 @@ | ||
var directories = pkg.directory || pkg.directories | ||
, lib = directories.lib | ||
, dest; | ||
if (lib) { | ||
dest = path.join(self.dir, lib, self.relChain.join('/') + '.js'); | ||
if (path.existsSync(dest)) { | ||
fn(null, fs.readFileSync(dest, 'utf8')); | ||
, lib = directories && directories.lib | ||
, chain = [self.dir], direct, index; | ||
if (lib) chain.push(lib); | ||
direct = path.join.apply(this, chain.concat([self.relChain.join('/') + '.js']) ); | ||
index = path.join.apply(this, chain.concat([self.relChain.join('/'), 'index.js']) ); | ||
if (path.existsSync(direct)) { | ||
fn(null, fs.readFileSync(direct, 'utf8'), false); | ||
} else if (path.existsSync(index)) { | ||
fn(null, fs.readFileSync(index, 'utf8'), true); | ||
} else { | ||
if (lib) { | ||
throw new Error("Unimplemented - could not find package " + self.relChain.join('/')); | ||
} else { | ||
throw new Error("Unimplemented"); | ||
self._relSrcErr = 'Missing ' + self.relChain.join('/') + ' in ' + self.pkgName + ' package'; | ||
fn(self._relSrcErr); | ||
} | ||
} else { | ||
self._relSrcErr = 'Missing ' + self.relChain.join('/') + ' in ' + self.pkgName + ' package'; | ||
fn(self._relSrcErr); | ||
} | ||
@@ -114,5 +122,5 @@ }); | ||
if (this.isSubmodule) { // Handle e.g., require("npm-module/sub-module") | ||
this.relSrc( function (err, relSrc) { | ||
this.relSrc( function (err, relSrc, isIndex) { | ||
if (err) return fn(self._srcerr = err); | ||
fn(null, self._src = relSrc); | ||
fn(null, self._src = relSrc, isIndex); | ||
}); | ||
@@ -195,5 +203,5 @@ } else { // Handle e.g., require("npm-module") | ||
if (isInstalled) { | ||
npmModule.src(function (err, body) { | ||
npmModule.src(function (err, body, isIndex) { | ||
var src = | ||
cache[url] = fillinTemplate(url, body, depsFor(body)); | ||
cache[url] = fillinTemplate(url, body, depsFor(body), isIndex); | ||
res.writeHead(200, {'Content-Type': 'text/javascript'}); | ||
@@ -203,3 +211,3 @@ res.end(src); | ||
} else { | ||
console.error("Could not find " + pkgName + ". Make sure it's installed via npm."); | ||
console.error("Could not find " + npmModule.pkgName + ". Make sure it's installed via npm."); | ||
res.writeHead(404); | ||
@@ -218,3 +226,3 @@ res.end(); | ||
} else { | ||
console.log("Could not find " + filepath); | ||
console.error("Could not find " + filepath); | ||
next(); | ||
@@ -221,0 +229,0 @@ } |
@@ -17,4 +17,5 @@ function browserRequire (path) { | ||
* must load before module can be loaded. | ||
* @param {Boolean} whether the module was found as module/index.js on the server or not | ||
*/ | ||
browserRequire.load = function (module, src, deps) { | ||
browserRequire.load = function (module, src, deps, isIndex) { | ||
var i = deps.length | ||
@@ -24,2 +25,8 @@ , depModule | ||
target.src = src; | ||
if (isIndex) { | ||
target.isIndex = isIndex; | ||
// Reset the basedir; prior to reset, basedir is name without trailing lastpart.js | ||
// After the reset, basedir trails with lastpart | ||
target.basedir = target.name.replace(/\.js$/, ''); | ||
} | ||
while (i--) { | ||
@@ -56,3 +63,7 @@ depModule = ModulePromise.from(deps[i], target); | ||
if (!parent) return name; | ||
var pathparts = parent.basedir.split('/'); | ||
var pathparts = parent.isIndex | ||
? parent.basedir.split('/') | ||
: (/^\/NPM\/[^\/]+\.js$/).test(parent.name) // if it's /NPM/1degree.js only, then we want /NPM/1degree/name | ||
? parent.name.replace(/\.js$/, '').split('/') | ||
: parent.basedir.split('/'); | ||
return this.normalizeRelToDir(name, pathparts); | ||
@@ -59,0 +70,0 @@ }, |
{ | ||
"name": "browser-require", | ||
"description": "Use CommonJS and NPM modules from the browser", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"homepage": "https://github.com/bnoguchi/browser-require", | ||
@@ -6,0 +6,0 @@ "repository": "https://github.com/bnoguchi/browser-require.git", |
@@ -1,1 +0,1 @@ | ||
browserRequire.load($module, $src, $deps); | ||
browserRequire.load($module, $src, $deps, $isIndex); |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
26399
4.62%527
3.74%