Skip to content

Commit

Permalink
fixed require for external libraries, closes #629, closes #559, closes
Browse files Browse the repository at this point in the history
…#593, closes #365, closes #629
  • Loading branch information
liabru committed Jun 11, 2018
1 parent 511de5b commit 0cf97f5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
7 changes: 5 additions & 2 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,16 @@ gulp.task('watch', function() {
var b = browserify({
entries: ['src/module/main.js'],
standalone: 'Matter',
plugin: [watchify],
transform: ['browserify-shim']
plugin: [watchify]
});

var bundle = function() {
gutil.log('Updated bundle build/matter-dev.js');
b.bundle()
.on('error', function(err) {
gutil.log('ERROR', err.message);
this.emit('end');
})
.pipe(through2({ objectMode: true }, function(chunk, encoding, callback) {
return callback(
null,
Expand Down
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
],
"devDependencies": {
"browserify": "^14.0.0",
"browserify-shim": "^3.8.13",
"cheerio": "^0.22.0",
"connect-livereload": "^0.6.0",
"event-stream": "^3.3.2",
Expand Down Expand Up @@ -59,8 +58,5 @@
"src",
"build",
"CHANGELOG.md"
],
"browserify-shim": {
"poly-decomp": "global:decomp"
}
]
}
14 changes: 14 additions & 0 deletions src/core/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,18 @@ module.exports = Common;
));
};

/**
* Used to require external libraries outside of the bundle.
* It first looks for the `globalName` on the environment's global namespace.
* If the global is not found, it will fall back to using the standard `require` using the `moduleName`.
* @private
* @method _requireGlobal
* @param {string} globalName The global module name
* @param {string} moduleName The fallback CommonJS module name
* @return {} The loaded module
*/
Common._requireGlobal = function(globalName, moduleName) {
var obj = (typeof window !== 'undefined' ? window[globalName] : typeof global !== 'undefined' ? global[globalName] : null);
return obj || require(moduleName);
};
})();
6 changes: 5 additions & 1 deletion src/factory/Bodies.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var Common = require('../core/Common');
var Body = require('../body/Body');
var Bounds = require('../geometry/Bounds');
var Vector = require('../geometry/Vector');
var decomp = require('poly-decomp');
var decomp;

(function() {

Expand Down Expand Up @@ -197,6 +197,10 @@ var decomp = require('poly-decomp');
* @return {body}
*/
Bodies.fromVertices = function(x, y, vertexSets, options, flagInternal, removeCollinear, minimumArea) {
if (!decomp) {
decomp = Common._requireGlobal('decomp', 'poly-decomp');
}

var body,
parts,
isConvex,
Expand Down

0 comments on commit 0cf97f5

Please sign in to comment.