Skip to content

Commit

Permalink
use path.relative and this.basedir instead of replace() on the id
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Feb 26, 2014
1 parent 37a702f commit 9499ffb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 11 additions & 9 deletions index.js
Expand Up @@ -46,10 +46,6 @@ function hash(what) {
return crypto.createHash('md5').update(what).digest('base64').slice(0, 6);
}

function idHash(id) {
return hash(id.replace(process.cwd(), ''));
}

inherits(Browserify, EventEmitter);

function Browserify (opts) {
Expand Down Expand Up @@ -99,6 +95,11 @@ function Browserify (opts) {
noParse.forEach(this.noParse.bind(this));
}

Browserify.prototype._hash = function (id) {
var basedir = this._basedir || process.cwd();
return hash(path.relative(basedir, id));
}

Browserify.prototype.noParse = function(file) {
var self = this;
var cwd = process.cwd();
Expand Down Expand Up @@ -153,7 +154,7 @@ Browserify.prototype.require = function (id, opts) {
}

if (opts.expose) {
self.exports[file] = idHash(file);
self.exports[file] = self._hash(file);

if (typeof opts.expose === 'string') {
self._expose[file] = opts.expose;
Expand Down Expand Up @@ -454,15 +455,16 @@ Browserify.prototype.deps = function (opts) {
id: row.id,
exposed: self._expose[row.id],
deps: {},
source: 'module.exports=require(\'' + idHash(row.id) + '\');',
source: 'module.exports=require(\''
+ self._hash(row.id) + '\');',
nomap: true
});
}

if (self.exports[row.id]) row.exposed = self.exports[row.id];

if (self._exposeAll) {
row.exposed = idHash(row.id);
row.exposed = self._hash(row.id);
}

// skip adding this file if it is external
Expand Down Expand Up @@ -533,7 +535,7 @@ Browserify.prototype.pack = function (opts) {
var file = row.deps[key];
var index = row.indexDeps && row.indexDeps[key];
if (self._exposeAll) {
index = idHash(file);
index = self._hash(file);
}
deps[key] = getId({ id: file, index: index });
});
Expand All @@ -547,7 +549,7 @@ Browserify.prototype.pack = function (opts) {
return row.exposed;
}
else if (self._external[row.id] || self.exports[row.id]) {
return idHash(row.id);
return self._hash(row.id);
}
else if (self._expose[row.id]) {
return row.id;
Expand Down
6 changes: 3 additions & 3 deletions test/id_hash.js
Expand Up @@ -6,7 +6,7 @@ test('idHash required deps', function (t) {
var b = browserify();
b.require(__dirname + '/id_hash/require.js', { expose: 'idhashdep' });
b.bundle(function (err, src) {
t.ok(/\'UKcog4\'/.test(src), 'Output contains valid hash');
t.ok(/\'GfayEO\'/.test(src), 'Output contains valid hash');
});
});

Expand All @@ -15,14 +15,14 @@ test('idHash external deps', function (t) {
var b = browserify(__dirname + '/id_hash/main.js');
b.external(__dirname + '/id_hash/require.js');
b.bundle(function (err, src) {
t.ok(/UKcog4/.test(src), 'Output contains valid hash');
t.ok(/GfayEO/.test(src), 'Output contains valid hash');
});
});

test('idHash exposeAll', function (t) {
t.plan(1);
var b = browserify(__dirname + '/id_hash/require.js', { exposeAll: true });
b.bundle(function (err, src) {
t.ok(/UKcog4/.test(src), 'Output contains valid hash');
t.ok(/GfayEO/.test(src), 'Output contains valid hash');
});
});

0 comments on commit 9499ffb

Please sign in to comment.