Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

alias test works yay

  • Loading branch information...
commit 7135a2f9b86504bb80372fb0d7d5f86d1a4a5e74 1 parent aef0bab
@substack authored
View
26 lib/wrap.js
@@ -13,6 +13,7 @@ function Wrap () {
this.files = {};
this.filters = [];
this.postFilters = [];
+ this.aliases = {};
this.ignoring = {};
this.extensions = [ '.js' ];
@@ -77,6 +78,11 @@ Wrap.prototype.readFile = function (file) {
return body;
};
+Wrap.prototype.alias = function (from, to) {
+ this.aliases[from] = to;
+ return this;
+};
+
Wrap.prototype.addEntry = function (file) {
var body = this.readFile(file);
@@ -158,6 +164,21 @@ Wrap.prototype.bundle = function () {
})
;
}))
+ .concat(Object.keys(self.aliases).map(function (from) {
+ var to = self.aliases[from];
+ if (!to.match(/^(\.\.?)?\//)) {
+ to = '/node_modules/' + to;
+ }
+
+ return wrappers.alias
+ .replace(/\$from/, function () {
+ return JSON.stringify(from);
+ })
+ .replace(/\$to/, function () {
+ return JSON.stringify(to);
+ })
+ ;
+ }))
.concat(this.appends)
.join('\n')
;
@@ -181,12 +202,13 @@ Wrap.prototype.require = function (startFiles, withRoot) {
startFiles = [ startFiles ];
}
- startFiles.forEach(function (file) {
+ startFiles.forEach(function include (file) {
var dir = process.cwd();
if (typeof file === 'object') {
Object.keys(file).forEach(function (key) {
- walker(file[key], dir, withRoot || dir, key);
+ include(file[key]);
+ self.alias(file[key], key);
});
return;
}
View
5 test/alias.js
@@ -32,7 +32,10 @@ exports.alias = function () {
assert.fail('seq chain never finished');
}, 5000);
- var context = { setTimeout : setTimeout };
+ var context = {
+ setTimeout : setTimeout,
+ console : console
+ };
var src = '';
res.on('data', function (buf) {
View
2  test/bundle.js
@@ -10,7 +10,7 @@ exports.bundle = function () {
var to = setTimeout(function () {
assert.fail('never ran');
- }, 10000);
+ }, 15000);
var c = {
setTimeout : setTimeout,
View
1  wrappers/alias.js
@@ -0,0 +1 @@
+require.alias($from, $to);
View
26 wrappers/prelude.js
@@ -22,6 +22,8 @@ require.resolve = (function () {
;
return function (x, cwd) {
+ if (!cwd) cwd = '/';
+
if (core[x]) return x;
var path = require.modules.path();
var y = cwd || '.';
@@ -78,7 +80,7 @@ require.resolve = (function () {
}
var m = loadAsFileSync(x);
- if (x) return x;
+ if (m) return m;
}
function nodeModulesPathsSync (start) {
@@ -94,3 +96,25 @@ require.resolve = (function () {
}
};
})();
+
+require.alias = function (from, to) {
+ var path = require.modules.path();
+ var res = null;
+ try {
+ res = require.resolve(from + '/package.json', '/');
+ }
+ catch (err) {
+ res = require.resolve(from, '/');
+ }
+ var basedir = path.dirname(res);
+
+ Object.keys(require.modules)
+ .filter(function (x) {
+ return x.slice(0, basedir.length + 1) === basedir + '/'
+ })
+ .forEach(function (x) {
+ var f = x.slice(basedir.length);
+ require.modules[to + f] = require.modules[basedir + f];
+ })
+ ;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.