Skip to content

Commit

Permalink
Hotfix via browserify#1101
Browse files Browse the repository at this point in the history
  • Loading branch information
epd committed Feb 6, 2015
1 parent a18657c commit 74b8305
Showing 1 changed file with 50 additions and 50 deletions.
100 changes: 50 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ function Browserify (files, opts) {
var self = this;
if (!(this instanceof Browserify)) return new Browserify(files, opts);
if (!opts) opts = {};

if (typeof files === 'string' || isarray(files) || isStream(files)) {
opts = xtend(opts, { entries: [].concat(opts.entries || [], files) });
}
else opts = xtend(files, opts);

self._options = opts;
if (opts.noparse) opts.noParse = opts.noparse;

if (opts.basedir !== undefined && typeof opts.basedir !== 'string') {
throw new Error('opts.basedir must be either undefined or a string.');
}

self._external = [];
self._exclude = [];
self._ignore = [];
Expand All @@ -65,21 +65,21 @@ function Browserify (files, opts) {
}
return ignoreTransform.indexOf(tr) === -1;
}

self.pipeline = self._createPipeline(opts);

[].concat(opts.transform).filter(Boolean).forEach(function (tr) {
self.transform(tr);
});

[].concat(opts.entries).filter(Boolean).forEach(function (file) {
self.add(file, { basedir: opts.basedir });
});

[].concat(opts.require).filter(Boolean).forEach(function (file) {
self.require(file, { basedir: opts.basedir });
});

[].concat(opts.plugin).filter(Boolean).forEach(function (p) {
self.plugin(p, { basedir: opts.basedir });
});
Expand All @@ -96,7 +96,7 @@ Browserify.prototype.require = function (file, opts) {
});
return this;
}

if (!opts) opts = {};
var basedir = defined(opts.basedir, self._options.basedir, process.cwd());
var expose = opts.expose;
Expand All @@ -109,7 +109,7 @@ Browserify.prototype.require = function (file, opts) {
if (expose === true) {
expose = '/' + path.relative(basedir, file);
}

if (isStream(file)) {
self._pending ++;
var order = self._entryOrder ++;
Expand All @@ -134,12 +134,12 @@ Browserify.prototype.require = function (file, opts) {
if (rec.entry) rec.order = order;
if (rec.transform === false) rec.transform = false;
self.pipeline.write(rec);

if (-- self._pending === 0) self.emit('_ready');
}));
return this;
}

var row = typeof file === 'object'
? xtend(file, opts)
: (isExternalModule(file)
Expand All @@ -160,15 +160,15 @@ Browserify.prototype.require = function (file, opts) {
});
}
else write();

function write () {
if (opts.external) return self.external(file, opts);
if (row.entry === undefined) row.entry = false;

if (!row.entry && self._options.exports === undefined) {
self._bpack.hasExports = true;
}

if (row.entry) row.order = self._entryOrder ++;
if (opts.transform === false) row.transform = false;
self.pipeline.write(row);
Expand Down Expand Up @@ -236,7 +236,7 @@ Browserify.prototype.external = function (file, opts) {
});
return this;
}

if (!opts) opts = {};
var basedir = defined(opts.basedir, process.cwd());
this._external.push(file);
Expand Down Expand Up @@ -275,7 +275,7 @@ Browserify.prototype.transform = function (tr, opts) {
opts = tr[1];
tr = tr[0];
}

//if the bundler is ignoring this transform
if (typeof tr === 'string' && !self._filterTransform(tr)) {
return this;
Expand All @@ -293,10 +293,10 @@ Browserify.prototype.transform = function (tr, opts) {
}
}
}

if (!opts) opts = {};
opts._flags = '_flags' in opts ? opts._flags : self._options;

var basedir = defined(opts.basedir, this._options.basedir, process.cwd());
var order = self._transformOrder ++;
self._pending ++;
Expand Down Expand Up @@ -368,14 +368,14 @@ Browserify.prototype._createPipeline = function (opts) {
pipeline.emit('transform', tr, file);
self.emit('transform', tr, file);
});

var dopts = {
index: !opts.fullPaths && !opts.exposeAll,
dedupe: true,
expose: this._expose
};
this._bpack = bpack(xtend(opts, { raw: true }));

var pipeline = splicer.obj([
'record', [ this._recorder() ],
'deps', [ this._mdeps ],
Expand All @@ -396,7 +396,7 @@ Browserify.prototype._createPipeline = function (opts) {
pipeline.get('deps').push(through.obj(function (row, enc, next) {
if (self._external.indexOf(row.id) >= 0) return next();
if (self._external.indexOf(row.file) >= 0) return next();

if (isAbsolutePath(row.id)) {
row.id = '/' + path.relative(basedir, row.file);
}
Expand All @@ -414,16 +414,16 @@ Browserify.prototype._createDeps = function (opts) {
var self = this;
var mopts = copy(opts);
var basedir = defined(opts.basedir, process.cwd());

mopts.extensions = [ '.js', '.json' ].concat(mopts.extensions || []);
self._extensions = mopts.extensions;

//filter transforms on top-level
mopts.transform = [].concat(opts.transform)
.filter(Boolean)
.filter(self._filterTransform)
;

mopts.transformKey = [ 'browserify', 'transform' ];
mopts.postFilter = function (id, file, pkg) {
if (opts.postFilter && !opts.postFilter(id, file, pkg)) return false;
Expand All @@ -450,7 +450,7 @@ Browserify.prototype._createDeps = function (opts) {
};
mopts.resolve = function (id, parent, cb) {
if (self._ignore.indexOf(id) >= 0) return cb(null, paths.empty, {});

bresolve(id, parent, function (err, file, pkg) {
if (file && self._ignore.indexOf(file) >= 0) {
return cb(null, paths.empty, {});
Expand All @@ -464,7 +464,7 @@ Browserify.prototype._createDeps = function (opts) {
}
}
}

if (file) {
var ex = '/' + path.relative(basedir, file);
if (self._external.indexOf(ex) >= 0) {
Expand All @@ -480,7 +480,7 @@ Browserify.prototype._createDeps = function (opts) {
cb(err, file, pkg);
});
};

if (opts.builtins === false) {
mopts.modules = {};
self._exclude.push.apply(self._exclude, Object.keys(builtins));
Expand All @@ -495,11 +495,11 @@ Browserify.prototype._createDeps = function (opts) {
mopts.modules = opts.builtins;
}
else mopts.modules = builtins;

Object.keys(builtins).forEach(function (key) {
if (!has(mopts.modules, key)) self._exclude.push(key);
});

mopts.globalTransform = [];
if (!this._bundled) {
this.once('bundle', function () {
Expand All @@ -510,17 +510,17 @@ Browserify.prototype._createDeps = function (opts) {
});
});
}

function globalTr (file) {
if (opts.detectGlobals === false) return through();

if (opts.noParse === true) return through();
var no = [].concat(opts.noParse).filter(Boolean);
if (no.indexOf(file) >= 0) return through();
if (no.map(function (x){return path.resolve(x)}).indexOf(file)>=0){
return through();
}

var parts = file.split('/node_modules/');
for (var i = 0; i < no.length; i++) {
if (typeof no[i] === 'function' && no[i](file)) {
Expand All @@ -533,16 +533,16 @@ Browserify.prototype._createDeps = function (opts) {
return through();
}
}

var vars = xtend({
process: function () { return "require('_process')" },
}, opts.insertGlobalVars);

if (opts.bundleExternal === false) {
delete vars.process;
delete vars.buffer;
}

return insertGlobals(file, xtend(opts, {
debug: opts.debug,
always: opts.insertGlobals,
Expand All @@ -560,7 +560,7 @@ Browserify.prototype._recorder = function (opts) {
var self = this;
var ended = false;
this._recorded = [];

if (!this._ticked) {
process.nextTick(function () {
self._ticked = true;
Expand All @@ -570,10 +570,10 @@ Browserify.prototype._recorder = function (opts) {
if (ended) stream.push(null);
});
}

var stream = through.obj(write, end);
return stream;

function write (row, enc, next) {
self._recorded.push(row);
if (self._ticked) this.push(row);
Expand All @@ -587,7 +587,7 @@ Browserify.prototype._recorder = function (opts) {

Browserify.prototype._json = function () {
return through.obj(function (row, enc, next) {
if (/\.json$/.test(row.file)) {
if (/\.json$/.test(row.file) && !/^arguments\[4\]/.test(row.source)) {
row.source = 'module.exports=' + row.source;
}
this.push(row);
Expand Down Expand Up @@ -651,22 +651,22 @@ Browserify.prototype._dedupe = function () {
Browserify.prototype._label = function (opts) {
var self = this;
var basedir = defined(opts.basedir, process.cwd());

return through.obj(function (row, enc, next) {
var prev = row.id;

var relf = '/' + path.relative(basedir, row.id);
var reli = '/' + path.relative(basedir, row.id);
if (self._external.indexOf(row.id) >= 0) return next();
if (self._external.indexOf(reli) >= 0) return next();
if (self._external.indexOf(row.file) >= 0) return next();
if (self._external.indexOf(relf) >= 0) return next();

if (row.index) row.id = row.index;

self.emit('label', prev, row.id);
if (row.indexDeps) row.deps = row.indexDeps || {};

Object.keys(row.deps).forEach(function (key) {
if (self._expose[key]) {
row.deps[key] = key;
Expand All @@ -685,7 +685,7 @@ Browserify.prototype._label = function (opts) {
row.deps[key] = key;
return;
}

for (var i = 0; i < self._extensions.length; i++) {
var ex = self._extensions[i];
if (self._external.indexOf(rfile + ex) >= 0) {
Expand All @@ -694,7 +694,7 @@ Browserify.prototype._label = function (opts) {
}
}
});

if (row.entry || row.expose) {
self._bpack.standaloneModule = row.id;
}
Expand Down Expand Up @@ -756,7 +756,7 @@ Browserify.prototype.bundle = function (cb) {
cb(null, body);
}));
}

if (this._pending === 0) {
this.emit('bundle', this.pipeline);
this.pipeline.end();
Expand All @@ -765,7 +765,7 @@ Browserify.prototype.bundle = function (cb) {
self.emit('bundle', self.pipeline);
self.pipeline.end();
});

this._bundled = true;
return this.pipeline;
};
Expand Down

0 comments on commit 74b8305

Please sign in to comment.