From 8a538896bf268f8760eb35b628abac0eca62cd3f Mon Sep 17 00:00:00 2001 From: Joseph Frazier Date: Tue, 7 Oct 2014 00:37:57 -0400 Subject: [PATCH] Split src/environment.js into Node/browser versions 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 --- Gruntfile.js | 3 +- package.json | 3 +- src/environment.js | 34 +++-------------- src/environment_browser.js | 37 +++++++++++++++++++ src/polyfills/console.js | 7 ---- .../polyfills/promiscuous-browser.js | 2 +- 6 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 src/environment_browser.js delete mode 100644 src/polyfills/console.js rename src/polyfills/promiscuous.js => test/polyfills/promiscuous-browser.js (63%) diff --git a/Gruntfile.js b/Gruntfile.js index faef451ec..01fd380e9 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -48,7 +48,7 @@ module.exports = function(grunt) { }, browserify: { devel: { - src: 'src/index.js', + src: pkg.main, dest: 'dist/<%= name %>-<%= pkg.version %>.js' }, options: { @@ -98,6 +98,7 @@ module.exports = function(grunt) { options: { specs: 'test/spec/*.js', keepRunner : true, + vendor: 'test/polyfills/*.js', helpers: 'test/helpers/*.js' } } diff --git a/package.json b/package.json index d030f93ae..93a7792cc 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/environment.js b/src/environment.js index ff6a43d10..45b9111e5 100644 --- a/src/environment.js +++ b/src/environment.js @@ -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') +}); diff --git a/src/environment_browser.js b/src/environment_browser.js new file mode 100644 index 000000000..eb4194fe7 --- /dev/null +++ b/src/environment_browser.js @@ -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 +}; diff --git a/src/polyfills/console.js b/src/polyfills/console.js deleted file mode 100644 index 804ca092a..000000000 --- a/src/polyfills/console.js +++ /dev/null @@ -1,7 +0,0 @@ -// Console is not defined in ECMAScript, so just in case... -module.exports = global.console || { - debug: function () {}, - log: function () {}, - warn: function () {}, - error: function () {} -}; diff --git a/src/polyfills/promiscuous.js b/test/polyfills/promiscuous-browser.js similarity index 63% rename from src/polyfills/promiscuous.js rename to test/polyfills/promiscuous-browser.js index b93a34155..0f44db178 100644 --- a/src/polyfills/promiscuous.js +++ b/test/polyfills/promiscuous-browser.js @@ -1,2 +1,2 @@ /**@license MIT-promiscuous-©Ruben Verborgh*/ -(function(n,t){function c(n,t){return(typeof t)[0]==n}function u(o,e){return e=function f(i,h,l,a,p,s){function y(n){return function(t){p&&(p=0,f(c,n,t))}}if(a=f.q,i!=c)return u(function(n,t){a.push({p:this,r:n,j:t,1:i,0:h})});if(l&&c(n,l)|c(t,l))try{p=l.then}catch(j){h=0,l=j}if(c(n,p))try{p.call(l,y(1),h=y(0))}catch(j){h(j)}else for(e=function(t,e){return c(n,t=h?t:e)?u(function(n,c){r(this,n,c,l,t)}):o},s=0;a.length>s;)p=a[s++],c(n,i=p[h])?r(p.p,p.r,p.j,l,i):(h?p.r:p.j)(l)},e.q=[],o.call(o={then:function(n,t){return e(n,t)},"catch":function(n){return e(0,n)}},function(n){e(c,1,n)},function(n){e(c,0,n)}),o}function r(u,r,o,e,f){setTimeout(function(){try{e=f(e),f=e&&c(t,e)|c(n,e)&&e.then,c(n,f)?e==u?o(TypeError()):f.call(e,r,o):r(e)}catch(i){o(i)}})}function o(n){return u(function(t){t(n)})}module.exports=u,u.resolve=o,u.reject=function(n){return u(function(t,c){c(n)})},u.all=function(n){return u(function(t,c,u,r){r=[],u=n.length||t(r),n.map(function(n,e){o(n).then(function(n){r[e]=n,--u||t(r)},c)})})}})("f","o"); +!function(n,t){function c(n,t){return(typeof t)[0]==n}function u(o,e){return e=function f(i,h,l,a,p,s){function y(n){return function(t){p&&(p=0,f(c,n,t))}}if(a=f.q,i!=c)return u(function(n,t){a.push({p:this,r:n,j:t,1:i,0:h})});if(l&&c(n,l)|c(t,l))try{p=l.then}catch(j){h=0,l=j}if(c(n,p))try{p.call(l,y(1),h=y(0))}catch(j){h(j)}else for(e=function(t,e){return c(n,t=h?t:e)?u(function(n,c){r(this,n,c,l,t)}):o},s=0;s