From 2bb20c49fd10bef46a7cc76d91714b0a98b3c4d7 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Thu, 11 Sep 2014 12:33:44 +0200 Subject: [PATCH] Removed {mangle: false} requirement to uglify-js Improves solution to #4 for javasript files by 12K (in current build). --- gruntfile.js | 1 - public_html/js/Api.js | 2 +- public_html/js/Auth.js | 2 +- public_html/js/Bootstrap.js | 2 +- public_html/js/BrowsingSettings.js | 2 +- public_html/js/DI.js | 25 +++++-------------- .../js/Presenters/CommentListPresenter.js | 2 +- public_html/js/Presenters/HelpPresenter.js | 2 +- public_html/js/Presenters/HomePresenter.js | 2 +- public_html/js/Presenters/LoginPresenter.js | 2 +- public_html/js/Presenters/LogoutPresenter.js | 2 +- public_html/js/Presenters/MessagePresenter.js | 2 +- .../js/Presenters/PagedCollectionPresenter.js | 2 +- .../js/Presenters/PostListPresenter.js | 2 +- .../js/Presenters/PostUploadPresenter.js | 2 +- .../js/Presenters/RegistrationPresenter.js | 2 +- public_html/js/Presenters/TagListPresenter.js | 2 +- .../js/Presenters/TopNavigationPresenter.js | 2 +- .../Presenters/UserAccountRemovalPresenter.js | 2 +- .../UserAccountSettingsPresenter.js | 2 +- .../js/Presenters/UserActivationPresenter.js | 2 +- .../UserBrowsingSettingsPresenter.js | 2 +- .../js/Presenters/UserListPresenter.js | 2 +- public_html/js/Presenters/UserPresenter.js | 2 +- public_html/js/Promise.js | 2 +- public_html/js/Router.js | 2 +- public_html/js/State.js | 2 +- public_html/js/Util.js | 2 +- 28 files changed, 32 insertions(+), 46 deletions(-) diff --git a/gruntfile.js b/gruntfile.js index d308186c..9c41e4c2 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -95,7 +95,6 @@ module.exports = function(grunt) { uglify: { dist: { options: { - mangle: false, //breaks dependency injection sourceMap: true, }, files: { diff --git a/public_html/js/Api.js b/public_html/js/Api.js index a7e33d55..cdb26836 100644 --- a/public_html/js/Api.js +++ b/public_html/js/Api.js @@ -57,4 +57,4 @@ App.API = function(jQuery, promise, appState) { }; -App.DI.registerSingleton('api', App.API); +App.DI.registerSingleton('api', ['jQuery', 'promise', 'appState'], App.API); diff --git a/public_html/js/Auth.js b/public_html/js/Auth.js index 631447b0..fdbe202e 100644 --- a/public_html/js/Auth.js +++ b/public_html/js/Auth.js @@ -158,4 +158,4 @@ App.Auth = function(_, jQuery, util, api, appState, promise) { }; -App.DI.registerSingleton('auth', App.Auth); +App.DI.registerSingleton('auth', ['_', 'jQuery', 'util', 'api', 'appState', 'promise'], App.Auth); diff --git a/public_html/js/Bootstrap.js b/public_html/js/Bootstrap.js index 785f5b66..dce924b8 100644 --- a/public_html/js/Bootstrap.js +++ b/public_html/js/Bootstrap.js @@ -25,7 +25,7 @@ App.Bootstrap = function(auth, router, util, promise) { }; -App.DI.registerSingleton('bootstrap', App.Bootstrap); +App.DI.registerSingleton('bootstrap', ['auth', 'router', 'util', 'promise'], App.Bootstrap); App.DI.registerManual('jQuery', function() { return window.$; }); App.DI.registerManual('pathJs', function() { return window.Path; }); App.DI.registerManual('_', function() { return window._; }); diff --git a/public_html/js/BrowsingSettings.js b/public_html/js/BrowsingSettings.js index 048472aa..3872c8b8 100644 --- a/public_html/js/BrowsingSettings.js +++ b/public_html/js/BrowsingSettings.js @@ -87,4 +87,4 @@ App.BrowsingSettings = function( }; -App.DI.registerSingleton('browsingSettings', App.BrowsingSettings); +App.DI.registerSingleton('browsingSettings', ['promise', 'auth', 'api'], App.BrowsingSettings); diff --git a/public_html/js/DI.js b/public_html/js/DI.js index 3ee32825..ebd03234 100644 --- a/public_html/js/DI.js +++ b/public_html/js/DI.js @@ -2,9 +2,6 @@ var App = App || {}; App.DI = (function() { - var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; - var ARGUMENT_NAMES = /([^\s,]+)/g; - var factories = {}; var instances = {}; @@ -17,7 +14,7 @@ App.DI = (function() { } var objectInitializer = factory.initializer; var singleton = factory.singleton; - var deps = resolveDependencies(objectInitializer); + var deps = resolveDependencies(objectInitializer, factory.dependencies); instance = {}; instance = objectInitializer.apply(instance, deps); if (singleton) { @@ -27,36 +24,26 @@ App.DI = (function() { return instance; } - function resolveDependencies(objectIntializer) { + function resolveDependencies(objectIntializer, depKeys) { var deps = []; - var depKeys = getFunctionParameterNames(objectIntializer); for (var i = 0; i < depKeys.length; i ++) { deps[i] = get(depKeys[i]); } return deps; } - function register(key, objectInitializer) { - factories[key] = {initializer: objectInitializer, singleton: false}; + function register(key, dependencies, objectInitializer) { + factories[key] = {initializer: objectInitializer, singleton: false, dependencies: dependencies}; } - function registerSingleton(key, objectInitializer) { - factories[key] = {initializer: objectInitializer, singleton: true}; + function registerSingleton(key, dependencies, objectInitializer) { + factories[key] = {initializer: objectInitializer, singleton: true, dependencies: dependencies}; } function registerManual(key, objectInitializer) { instances[key] = objectInitializer(); } - function getFunctionParameterNames(func) { - var fnStr = func.toString().replace(STRIP_COMMENTS, ''); - var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES); - if (result === null) { - result = []; - } - return result; - } - return { get: get, register: register, diff --git a/public_html/js/Presenters/CommentListPresenter.js b/public_html/js/Presenters/CommentListPresenter.js index 315ccec8..dc1e4d11 100644 --- a/public_html/js/Presenters/CommentListPresenter.js +++ b/public_html/js/Presenters/CommentListPresenter.js @@ -23,4 +23,4 @@ App.Presenters.CommentListPresenter = function( }; -App.DI.register('commentListPresenter', App.Presenters.CommentListPresenter); +App.DI.register('commentListPresenter', ['jQuery', 'topNavigationPresenter'], App.Presenters.CommentListPresenter); diff --git a/public_html/js/Presenters/HelpPresenter.js b/public_html/js/Presenters/HelpPresenter.js index c83862a6..1978db0d 100644 --- a/public_html/js/Presenters/HelpPresenter.js +++ b/public_html/js/Presenters/HelpPresenter.js @@ -23,4 +23,4 @@ App.Presenters.HelpPresenter = function( }; -App.DI.register('helpPresenter', App.Presenters.HelpPresenter); +App.DI.register('helpPresenter', ['jQuery', 'topNavigationPresenter'], App.Presenters.HelpPresenter); diff --git a/public_html/js/Presenters/HomePresenter.js b/public_html/js/Presenters/HomePresenter.js index c9fe07bc..85bf08d2 100644 --- a/public_html/js/Presenters/HomePresenter.js +++ b/public_html/js/Presenters/HomePresenter.js @@ -23,4 +23,4 @@ App.Presenters.HomePresenter = function( }; -App.DI.register('homePresenter', App.Presenters.HomePresenter); +App.DI.register('homePresenter', ['jQuery', 'topNavigationPresenter'], App.Presenters.HomePresenter); diff --git a/public_html/js/Presenters/LoginPresenter.js b/public_html/js/Presenters/LoginPresenter.js index 04fc7b56..5bf93e72 100644 --- a/public_html/js/Presenters/LoginPresenter.js +++ b/public_html/js/Presenters/LoginPresenter.js @@ -77,4 +77,4 @@ App.Presenters.LoginPresenter = function( }; -App.DI.register('loginPresenter', App.Presenters.LoginPresenter); +App.DI.register('loginPresenter', ['_', 'jQuery', 'util', 'promise', 'router', 'auth', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.LoginPresenter); diff --git a/public_html/js/Presenters/LogoutPresenter.js b/public_html/js/Presenters/LogoutPresenter.js index 1651f936..c3631951 100644 --- a/public_html/js/Presenters/LogoutPresenter.js +++ b/public_html/js/Presenters/LogoutPresenter.js @@ -33,4 +33,4 @@ App.Presenters.LogoutPresenter = function( }; -App.DI.register('logoutPresenter', App.Presenters.LogoutPresenter); +App.DI.register('logoutPresenter', ['jQuery', 'promise', 'router', 'auth', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.LogoutPresenter); diff --git a/public_html/js/Presenters/MessagePresenter.js b/public_html/js/Presenters/MessagePresenter.js index cbdb4cee..c71439e5 100644 --- a/public_html/js/Presenters/MessagePresenter.js +++ b/public_html/js/Presenters/MessagePresenter.js @@ -38,4 +38,4 @@ App.Presenters.MessagePresenter = function(jQuery) { }; -App.DI.register('messagePresenter', App.Presenters.MessagePresenter); +App.DI.register('messagePresenter', ['jQuery'], App.Presenters.MessagePresenter); diff --git a/public_html/js/Presenters/PagedCollectionPresenter.js b/public_html/js/Presenters/PagedCollectionPresenter.js index 3f441a69..5fb2fd69 100644 --- a/public_html/js/Presenters/PagedCollectionPresenter.js +++ b/public_html/js/Presenters/PagedCollectionPresenter.js @@ -116,4 +116,4 @@ App.Presenters.PagedCollectionPresenter = function(_, util, promise, api) { }; -App.DI.register('pagedCollectionPresenter', App.Presenters.PagedCollectionPresenter); +App.DI.register('pagedCollectionPresenter', ['_', 'util', 'promise', 'api'], App.Presenters.PagedCollectionPresenter); diff --git a/public_html/js/Presenters/PostListPresenter.js b/public_html/js/Presenters/PostListPresenter.js index 4aa25ed9..e62ffae5 100644 --- a/public_html/js/Presenters/PostListPresenter.js +++ b/public_html/js/Presenters/PostListPresenter.js @@ -23,4 +23,4 @@ App.Presenters.PostListPresenter = function( }; -App.DI.register('postListPresenter', App.Presenters.PostListPresenter); +App.DI.register('postListPresenter', ['jQuery', 'topNavigationPresenter'], App.Presenters.PostListPresenter); diff --git a/public_html/js/Presenters/PostUploadPresenter.js b/public_html/js/Presenters/PostUploadPresenter.js index 19da5f17..8949fbec 100644 --- a/public_html/js/Presenters/PostUploadPresenter.js +++ b/public_html/js/Presenters/PostUploadPresenter.js @@ -23,4 +23,4 @@ App.Presenters.PostUploadPresenter = function( }; -App.DI.register('postUploadPresenter', App.Presenters.PostUploadPresenter); +App.DI.register('postUploadPresenter', ['jQuery', 'topNavigationPresenter'], App.Presenters.PostUploadPresenter); diff --git a/public_html/js/Presenters/RegistrationPresenter.js b/public_html/js/Presenters/RegistrationPresenter.js index a12ef8f1..16c7b7bc 100644 --- a/public_html/js/Presenters/RegistrationPresenter.js +++ b/public_html/js/Presenters/RegistrationPresenter.js @@ -94,4 +94,4 @@ App.Presenters.RegistrationPresenter = function( }; -App.DI.register('registrationPresenter', App.Presenters.RegistrationPresenter); +App.DI.register('registrationPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.RegistrationPresenter); diff --git a/public_html/js/Presenters/TagListPresenter.js b/public_html/js/Presenters/TagListPresenter.js index 6706c24f..5bb9902e 100644 --- a/public_html/js/Presenters/TagListPresenter.js +++ b/public_html/js/Presenters/TagListPresenter.js @@ -23,4 +23,4 @@ App.Presenters.TagListPresenter = function( }; -App.DI.register('tagListPresenter', App.Presenters.TagListPresenter); +App.DI.register('tagListPresenter', ['jQuery', 'topNavigationPresenter'], App.Presenters.TagListPresenter); diff --git a/public_html/js/Presenters/TopNavigationPresenter.js b/public_html/js/Presenters/TopNavigationPresenter.js index c9db932b..2dc099df 100644 --- a/public_html/js/Presenters/TopNavigationPresenter.js +++ b/public_html/js/Presenters/TopNavigationPresenter.js @@ -52,4 +52,4 @@ App.Presenters.TopNavigationPresenter = function( }; -App.DI.registerSingleton('topNavigationPresenter', App.Presenters.TopNavigationPresenter); +App.DI.registerSingleton('topNavigationPresenter', ['_', 'jQuery', 'util', 'promise', 'auth'], App.Presenters.TopNavigationPresenter); diff --git a/public_html/js/Presenters/UserAccountRemovalPresenter.js b/public_html/js/Presenters/UserAccountRemovalPresenter.js index c2716309..da3ba4ac 100644 --- a/public_html/js/Presenters/UserAccountRemovalPresenter.js +++ b/public_html/js/Presenters/UserAccountRemovalPresenter.js @@ -78,4 +78,4 @@ App.Presenters.UserAccountRemovalPresenter = function( }; -App.DI.register('userAccountRemovalPresenter', App.Presenters.UserAccountRemovalPresenter); +App.DI.register('userAccountRemovalPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'router', 'messagePresenter'], App.Presenters.UserAccountRemovalPresenter); diff --git a/public_html/js/Presenters/UserAccountSettingsPresenter.js b/public_html/js/Presenters/UserAccountSettingsPresenter.js index 8f670c39..32fd2da0 100644 --- a/public_html/js/Presenters/UserAccountSettingsPresenter.js +++ b/public_html/js/Presenters/UserAccountSettingsPresenter.js @@ -166,4 +166,4 @@ App.Presenters.UserAccountSettingsPresenter = function( }; -App.DI.register('userAccountSettingsPresenter', App.Presenters.UserAccountSettingsPresenter); +App.DI.register('userAccountSettingsPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'messagePresenter'], App.Presenters.UserAccountSettingsPresenter); diff --git a/public_html/js/Presenters/UserActivationPresenter.js b/public_html/js/Presenters/UserActivationPresenter.js index 81add48b..55b1d1fb 100644 --- a/public_html/js/Presenters/UserActivationPresenter.js +++ b/public_html/js/Presenters/UserActivationPresenter.js @@ -109,4 +109,4 @@ App.Presenters.UserActivationPresenter = function( }; -App.DI.register('userActivationPresenter', App.Presenters.UserActivationPresenter); +App.DI.register('userActivationPresenter', ['_', 'jQuery', 'promise', 'util', 'auth', 'api', 'router', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.UserActivationPresenter); diff --git a/public_html/js/Presenters/UserBrowsingSettingsPresenter.js b/public_html/js/Presenters/UserBrowsingSettingsPresenter.js index 7592ec64..bd12bdc5 100644 --- a/public_html/js/Presenters/UserBrowsingSettingsPresenter.js +++ b/public_html/js/Presenters/UserBrowsingSettingsPresenter.js @@ -67,4 +67,4 @@ App.Presenters.UserBrowsingSettingsPresenter = function( }; -App.DI.register('userBrowsingSettingsPresenter', App.Presenters.UserBrowsingSettingsPresenter); +App.DI.register('userBrowsingSettingsPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'browsingSettings', 'messagePresenter'], App.Presenters.UserBrowsingSettingsPresenter); diff --git a/public_html/js/Presenters/UserListPresenter.js b/public_html/js/Presenters/UserListPresenter.js index 742fc8e9..4fe33b74 100644 --- a/public_html/js/Presenters/UserListPresenter.js +++ b/public_html/js/Presenters/UserListPresenter.js @@ -73,4 +73,4 @@ App.Presenters.UserListPresenter = function( }; -App.DI.register('userListPresenter', App.Presenters.UserListPresenter); +App.DI.register('userListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'router', 'pagedCollectionPresenter', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.UserListPresenter); diff --git a/public_html/js/Presenters/UserPresenter.js b/public_html/js/Presenters/UserPresenter.js index 1064ae4b..d224bc4a 100644 --- a/public_html/js/Presenters/UserPresenter.js +++ b/public_html/js/Presenters/UserPresenter.js @@ -87,4 +87,4 @@ App.Presenters.UserPresenter = function( }; -App.DI.register('userPresenter', App.Presenters.UserPresenter); +App.DI.register('userPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'topNavigationPresenter', 'userBrowsingSettingsPresenter', 'userAccountSettingsPresenter', 'userAccountRemovalPresenter', 'messagePresenter'], App.Presenters.UserPresenter); diff --git a/public_html/js/Promise.js b/public_html/js/Promise.js index c0bbb462..f4d864b2 100644 --- a/public_html/js/Promise.js +++ b/public_html/js/Promise.js @@ -25,4 +25,4 @@ App.Promise = function(jQuery) { }; -App.DI.registerSingleton('promise', App.Promise); +App.DI.registerSingleton('promise', ['jQuery'], App.Promise); diff --git a/public_html/js/Router.js b/public_html/js/Router.js index 921f98d8..533f6554 100644 --- a/public_html/js/Router.js +++ b/public_html/js/Router.js @@ -59,4 +59,4 @@ App.Router = function(pathJs, _, jQuery, util, appState) { }; -App.DI.registerSingleton('router', App.Router); +App.DI.registerSingleton('router', ['pathJs', '_', 'jQuery', 'util', 'appState'], App.Router); diff --git a/public_html/js/State.js b/public_html/js/State.js index 94888205..62d61890 100644 --- a/public_html/js/State.js +++ b/public_html/js/State.js @@ -49,4 +49,4 @@ App.State = function() { }; -App.DI.registerSingleton('appState', App.State); +App.DI.registerSingleton('appState', [], App.State); diff --git a/public_html/js/Util.js b/public_html/js/Util.js index 66389c24..a01593f2 100644 --- a/public_html/js/Util.js +++ b/public_html/js/Util.js @@ -160,4 +160,4 @@ App.Util = function(_, jQuery, promise) { }; -App.DI.registerSingleton('util', App.Util); +App.DI.registerSingleton('util', ['_', 'jQuery', 'promise'], App.Util);