Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

component-builder

Package Overview
Dependencies
Maintainers
31
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

component-builder - npm Package Compare versions

Comparing version 1.1.13 to 1.2.0

build/builders/lookup.js

306

build/builders/scripts.js

@@ -397,19 +397,9 @@

var debug = require('debug')('component-builder:scripts');
var path = pathUtil = require('path');
var path = require('path');
var relative = path.relative;
var requires = require('requires');
var fs = require('graceful-fs');
var url = require('url');
var Lookup = require('./lookup.js');
var Builder = require('./builder');
var utils = require('../utils');
var manifest = require('component-manifest');
// default extension to look up
var extensions = [
'',
'.js',
'.json',
'/index.js',
];
module.exports = Scripts;

@@ -623,3 +613,3 @@ Builder.extend(Scripts);

Scripts.prototype.register = wrapGenerator.mark(function(file) {
var self, js, result, i, require, quote, resolvedPath, resolvedRequire, name;
var js, lookup, result, i, require, quote, resolvedPath, resolvedRequire, name;

@@ -629,4 +619,4 @@ return wrapGenerator(function($ctx1) {

case 0:
self = this;
js = file.string;
lookup = Lookup(file, this);
result = requires(js);

@@ -642,3 +632,3 @@ i = 0;

quote = require.string.match(/"/) ? '"' : "'";
return $ctx1.delegateYield(self.lookup(file, require.path), "t2", 8);
return $ctx1.delegateYield(lookup.exec(require.path), "t2", 8);
case 8:

@@ -761,288 +751,2 @@ resolvedPath = $ctx1.t2;

/**
* From a file, lookup another file within that dep.
* For use within `require()`s.
*
* To do:
*
* - people like @raynos will want to be able to do require('component/lib') or something but F that!
*
* @param {Object} file
* @param {String} target
* @return {String} name
* @api private
*/
Scripts.prototype.lookup = wrapGenerator.mark(function(file, target) {
var currentDir, parentDir, lookup;
return wrapGenerator(function($ctx2) {
while (1) switch ($ctx2.prev = $ctx2.next) {
case 0:
target = target.toLowerCase();
currentDir = target.slice(0, 2) === './';
parentDir = target.slice(0, 3) === '../';
if (!(currentDir || parentDir)) {
$ctx2.next = 10;
break;
}
lookup = this.lookupRelative(file, target);
if (!(lookup != null)) {
$ctx2.next = 7;
break;
}
return $ctx2.abrupt("return", lookup);
case 7:
return $ctx2.abrupt("return", target);
case 10:
return $ctx2.delegateYield(this.lookupDependency(file, target), "t3", 11);
case 11:
return $ctx2.abrupt("return", $ctx2.t3);
case 12:
case "end":
return $ctx2.stop();
}
}, this);
});
/**
* Lookup a relative file.
*
* @param {Object} file
* @param {String} target
* @return {String} name
* @api private
*/
Scripts.prototype.lookupRelative = function (file, target) {
var path = url.resolve(file.path, target);
var files = file.manifest.files;
for (var i = 0; i < files.length; i++) {
var f = files[i];
// we need this fallback to check relatives from a foreign local
var name = f.name || pathUtil.join(f.manifest.name, pathUtil.relative(f.manifest.path, f.filename));
for (var j = 0; j < extensions.length; j++) {
// check by adding extensions
if (f.path.toLowerCase() === path + extensions[j]) return name;
}
// check by removing extensions
if (f.path.replace(/\.\w+$/, '').toLowerCase() === path) return name;
}
var message = 'ignore "' + target + '" , could not resolve from "' + file.branch.name + '"\'s file "' + file.path + '"';
debug(message);
return null;
}
/**
* Look up a remote dependency.
* Valid references:
*
* <repo>
* <user>-<repo>
* <user>~<repo>
*
* or:
*
* <reference>/<filename>
*
* @param {Object} component
* @param {Object} file
* @param {String} target
* @return {String} name
* @api private
*/
Scripts.prototype.lookupDependency = wrapGenerator.mark(function(file, target) {
var frags, reference, tail, branch, deps, names, name, i, localDeps, re, dep, relativeFile, resolvedTail, repo;
return wrapGenerator(function($ctx3) {
while (1) switch ($ctx3.prev = $ctx3.next) {
case 0:
frags = target.split('/');
reference = frags[0];
tail = frags.length > 1
? ('/' + frags.slice(1).join('/'))
: '';
branch = file.branch;
deps = branch.dependencies;
names = Object.keys(deps);
if (!~reference.indexOf('~')) {
$ctx3.next = 10;
break;
}
name = reference.replace('~', '/');
if (!deps[name]) {
$ctx3.next = 10;
break;
}
return $ctx3.abrupt("return", deps[name].canonical + tail);
case 10:
if (!~reference.indexOf('-')) {
$ctx3.next = 19;
break;
}
i = 0;
case 12:
if (!(i < names.length)) {
$ctx3.next = 19;
break;
}
name = names[i];
if (!(reference === name.replace('/', '-'))) {
$ctx3.next = 16;
break;
}
return $ctx3.abrupt("return", deps[name].canonical + tail);
case 16:
i++;
$ctx3.next = 12;
break;
case 19:
localDeps = Object.keys(branch.locals);
i = 0;
case 21:
if (!(i < localDeps.length)) {
$ctx3.next = 37;
break;
}
re = new RegExp("^("+localDeps[i]+")(/.*)?$");
if (!(m = re.exec(target))) {
$ctx3.next = 34;
break;
}
dep = m[1];
tail = m[2] || '';
if (!(tail !== '')) {
$ctx3.next = 33;
break;
}
relativeFile = '.' + tail;
return $ctx3.delegateYield(this.lookupRelativeForLocal(branch.locals[dep], relativeFile), "t4", 29);
case 29:
resolvedTail = $ctx3.t4;
if (!(resolvedTail != null)) {
$ctx3.next = 33;
break;
}
debug('resolved relative file for local "' + dep + '/' + resolvedTail + '"');
return $ctx3.abrupt("return", branch.locals[dep].canonical + '/' + resolvedTail);
case 33:
return $ctx3.abrupt("return", branch.locals[dep].canonical + tail);
case 34:
i++;
$ctx3.next = 21;
break;
case 37:
i = 0;
case 38:
if (!(i < names.length)) {
$ctx3.next = 46;
break;
}
name = names[i];
repo = name.split('/')[1];
if (!(repo === reference)) {
$ctx3.next = 43;
break;
}
return $ctx3.abrupt("return", deps[name].canonical + tail);
case 43:
i++;
$ctx3.next = 38;
break;
case 46:
i = 0;
case 47:
if (!(i < names.length)) {
$ctx3.next = 55;
break;
}
name = names[i];
dep = deps[name];
if (!(dep.node.name.toLowerCase() === reference)) {
$ctx3.next = 52;
break;
}
return $ctx3.abrupt("return", dep.canonical + tail);
case 52:
i++;
$ctx3.next = 47;
break;
case 55:
// to do: look up stuff outside the dependencies
debug('could not resolve "%s" from "%s"', target, file.name);
return $ctx3.abrupt("return", target);
case 57:
case "end":
return $ctx3.stop();
}
}, this);
});
Scripts.prototype.lookupRelativeForLocal = wrapGenerator.mark(function(localBranch, relativeTarget) {
var createManifest, manifestGenerator, manifest, obj, resolved, relative;
return wrapGenerator(function($ctx4) {
while (1) switch ($ctx4.prev = $ctx4.next) {
case 0:
createManifest = require('component-manifest');
manifestGenerator = createManifest(this);
return $ctx4.delegateYield(manifestGenerator(localBranch), "t5", 3);
case 3:
manifest = $ctx4.t5;
obj = {
path: '', // it should simulate a url-relative path
manifest: manifest,
branch: localBranch
};
resolved = this.lookupRelative(obj, relativeTarget);
if (!(resolved == null)) {
$ctx4.next = 8;
break;
}
return $ctx4.abrupt("return", null);
case 8:
relative = pathUtil.relative(manifest.name, resolved);
return $ctx4.abrupt("return", relative);
case 10:
case "end":
return $ctx4.stop();
}
}, this);
});
// private helpers

@@ -1049,0 +753,0 @@

@@ -0,1 +1,15 @@

1.2.0 / 2014-12-19
==================
* refactor require lookups
* improve remote dependency lookups [#86](https://github.com/componentjs/builder2.js/pull/86)
* allow camelCase for components [#87](https://github.com/componentjs/builder2.js/pull/87)
1.1.13 / 2014-09-27
==================
* better tests for locals ordering [#29](https://github.com/componentjs/builder2.js/issues/29)
* [faster CSS parsing](https://github.com/componentjs/builder2.js/pull/84)
* fix [file lookup of locals without file extension](https://github.com/componentjs/builder2.js/pull/82)
* merge [Export AMD module with given external name, not with internal (canonical)](https://github.com/componentjs/builder2.js/pull/83)
1.1.12 / 2014-09-27

@@ -2,0 +16,0 @@ ==================

var debug = require('debug')('component-builder:scripts');
var path = pathUtil = require('path');
var path = require('path');
var relative = path.relative;
var requires = require('requires');
var fs = require('graceful-fs');
var url = require('url');
var Lookup = require('./lookup.js');
var Builder = require('./builder');
var utils = require('../utils');
var manifest = require('component-manifest');
// default extension to look up
var extensions = [
'',
'.js',
'.json',
'/index.js',
]
module.exports = Scripts

@@ -201,4 +192,4 @@

Scripts.prototype.register = function* (file) {
var self = this;
var js = file.string;
var lookup = Lookup(file, this);

@@ -211,3 +202,3 @@ // rewrite all the requires

var quote = require.string.match(/"/) ? '"' : "'";
var resolvedPath = yield* self.lookup(file, require.path);
var resolvedPath = yield* lookup.exec(require.path);
var resolvedRequire = 'require(' + quote + resolvedPath + quote + ')';

@@ -319,172 +310,2 @@ js = js.replace(require.string, resolvedRequire);

/**
* From a file, lookup another file within that dep.
* For use within `require()`s.
*
* To do:
*
* - people like @raynos will want to be able to do require('component/lib') or something but F that!
*
* @param {Object} file
* @param {String} target
* @return {String} name
* @api private
*/
Scripts.prototype.lookup = function* (file, target) {
target = target.toLowerCase();
var currentDir = target.slice(0, 2) === './';
var parentDir = target.slice(0, 3) === '../';
if (currentDir || parentDir ) {
var lookup = this.lookupRelative(file, target);
if (lookup != null) return lookup;
return target;
} else {
return yield* this.lookupDependency(file, target);
}
}
/**
* Lookup a relative file.
*
* @param {Object} file
* @param {String} target
* @return {String} name
* @api private
*/
Scripts.prototype.lookupRelative = function (file, target) {
var path = url.resolve(file.path, target);
var files = file.manifest.files;
for (var i = 0; i < files.length; i++) {
var f = files[i];
// we need this fallback to check relatives from a foreign local
var name = f.name || pathUtil.join(f.manifest.name, pathUtil.relative(f.manifest.path, f.filename));
for (var j = 0; j < extensions.length; j++) {
// check by adding extensions
if (f.path.toLowerCase() === path + extensions[j]) return name;
}
// check by removing extensions
if (f.path.replace(/\.\w+$/, '').toLowerCase() === path) return name;
}
var message = 'ignore "' + target + '" , could not resolve from "' + file.branch.name + '"\'s file "' + file.path + '"';
debug(message);
return null;
}
/**
* Look up a remote dependency.
* Valid references:
*
* <repo>
* <user>-<repo>
* <user>~<repo>
*
* or:
*
* <reference>/<filename>
*
* @param {Object} component
* @param {Object} file
* @param {String} target
* @return {String} name
* @api private
*/
Scripts.prototype.lookupDependency = function* (file, target) {
var frags = target.split('/');
var reference = frags[0];
var tail = frags.length > 1
? ('/' + frags.slice(1).join('/'))
: ''
var branch = file.branch;
var deps = branch.dependencies;
var names = Object.keys(deps);
// <user>~<repo>
if (~reference.indexOf('~')) {
var name = reference.replace('~', '/');
if (deps[name]) return deps[name].canonical + tail;
}
// <user>-<repo>
if (~reference.indexOf('-')) {
for (var i = 0; i < names.length; i++) {
var name = names[i];
if (reference === name.replace('/', '-')) {
return deps[name].canonical + tail;
}
}
}
// local
var localDeps = Object.keys(branch.locals);
for (var i = 0; i < localDeps.length; i++) {
// Find a local dependency that matches as a prefix of the target
// or the whole target, and return the canonical path.
var re = new RegExp("^("+localDeps[i]+")(/.*)?$");
if (m = re.exec(target)) {
var dep = m[1];
var tail = m[2] || '';
if (tail !== '') {
var relativeFile = '.' + tail;
var resolvedTail = yield* this.lookupRelativeForLocal(branch.locals[dep], relativeFile);
if (resolvedTail != null) {
debug('resolved relative file for local "' + dep + '/' + resolvedTail + '"');
return branch.locals[dep].canonical + '/' + resolvedTail;
}
}
return branch.locals[dep].canonical + tail;
}
}
// <repo>
for (var i = 0; i < names.length; i++) {
var name = names[i];
var repo = name.split('/')[1];
if (repo === reference) {
return deps[name].canonical + tail;
}
}
// component.json name, if different than repo
for (var i = 0; i < names.length; i++) {
var name = names[i];
var dep = deps[name];
if (dep.node.name.toLowerCase() === reference) {
return dep.canonical + tail;
}
}
// to do: look up stuff outside the dependencies
debug('could not resolve "%s" from "%s"', target, file.name)
return target
}
Scripts.prototype.lookupRelativeForLocal = function* (localBranch, relativeTarget) {
var createManifest = require('component-manifest');
var manifestGenerator = createManifest(this);
var manifest = yield* manifestGenerator(localBranch);
var obj = {
path: '', // it should simulate a url-relative path
manifest: manifest,
branch: localBranch
}
// resolve the file (if extension is not provided)
var resolved = this.lookupRelative(obj, relativeTarget);
if (resolved == null) return null;
var relative = pathUtil.relative(manifest.name, resolved);
return relative;
}
// private helpers

@@ -491,0 +312,0 @@

{
"name": "component-builder",
"description": "builder for component",
"version": "1.1.13",
"version": "1.2.0",
"author": {

@@ -6,0 +6,0 @@ "name": "Jonathan Ong",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc