Skip to content

Commit

Permalink
Split src/environment.js into Node/browser versions
Browse files Browse the repository at this point in the history
src/environment_browser.js:
  Use global variables instead of requiring modules. This undoes the effects of
  the following commits, removing ~1KB from the bundle size:
    51003e3: Timers: require('timers') instead of directly using global
    8baf989: LoggerFactory: require('console') instead of directly using global
    b69fef8: Utils.Promise = global.Promise || require('promiscuous');

src/environment.js:
  Export a Node-compatible environment by extending the browser environment
  with the modules listed above.

Gruntfile.js: Tests polyfill Promise using promiscuous-browser.js
  • Loading branch information
joseph-onsip committed Oct 16, 2014
1 parent 9a2b089 commit 8a53889
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 39 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = function(grunt) {
},
browserify: {
devel: {
src: 'src/index.js',
src: pkg.main,
dest: 'dist/<%= name %>-<%= pkg.version %>.js'
},
options: {
Expand Down Expand Up @@ -98,6 +98,7 @@ module.exports = function(grunt) {
options: {
specs: 'test/spec/*.js',
keepRunner : true,
vendor: 'test/polyfills/*.js',
helpers: 'test/helpers/*.js'
}
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"main": "src/index.js",
"browser": {
"./src/Grammar/index.js": "./src/Grammar/dist/Grammar.js",
"promiscuous": "./src/polyfills/promiscuous.js",
"console": "./src/polyfills/console.js"
"./src/environment.js": "./src/environment_browser.js"
},
"homepage": "http://sipjs.com",
"author": "Will Mitchell <[email protected]>",
Expand Down
34 changes: 6 additions & 28 deletions src/environment.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
function getPrefixedProperty (object, name) {
if (object == null) {
return;
}
var capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
var prefixedNames = [name, 'webkit' + capitalizedName, 'moz' + capitalizedName];
for (var i in prefixedNames) {
var property = object[prefixedNames[i]];
if (property) {
return property.bind(object);
}
}
}
var extend = require('util')._extend;

module.exports = {
WebSocket: global.WebSocket,
open: global.open,
extend(exports, require('./environment_browser'));

extend(exports, {
Promise: global.Promise || require('promiscuous'),
console: require('console'),
timers: require('timers'),

MediaStream: getPrefixedProperty(global, 'MediaStream'),
getUserMedia: getPrefixedProperty(global.navigator, 'getUserMedia'),
RTCPeerConnection: getPrefixedProperty(global, 'RTCPeerConnection'),
RTCSessionDescription: getPrefixedProperty(global, 'RTCSessionDescription'),

attachMediaStream: global.attachMediaStream,
// these aren't prefixed, but some platforms don't have global.URL
createObjectURL: getPrefixedProperty(global.URL, 'createObjectURL'),
revokeObjectURL: getPrefixedProperty(global.URL, 'revokeObjectURL')
};
timers: require('timers')
});
37 changes: 37 additions & 0 deletions src/environment_browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function getPrefixedProperty (object, name) {
if (object == null) {
return;
}
var capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
var prefixedNames = [name, 'webkit' + capitalizedName, 'moz' + capitalizedName];
for (var i in prefixedNames) {
var property = object[prefixedNames[i]];
if (property) {
return property.bind(object);
}
}
}

module.exports = {
WebSocket: global.WebSocket,
open: global.open,
Promise: global.Promise,
timers: global,

// Console is not defined in ECMAScript, so just in case...
console: global.console || {
debug: function () {},
log: function () {},
warn: function () {},
error: function () {}
},

MediaStream: getPrefixedProperty(global, 'MediaStream'),
getUserMedia: getPrefixedProperty(global.navigator, 'getUserMedia'),
RTCPeerConnection: getPrefixedProperty(global, 'RTCPeerConnection'),
RTCSessionDescription: getPrefixedProperty(global, 'RTCSessionDescription'),

attachMediaStream: global.attachMediaStream,
createObjectURL: global.URL && global.URL.createObjectURL,
revokeObjectURL: global.URL && global.URL.revokeObjectURL
};
7 changes: 0 additions & 7 deletions src/polyfills/console.js

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8a53889

Please sign in to comment.