Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jaawerth/755b9f4d9dc0daeab70f1a64fafd4d78 to your computer and use it in GitHub Desktop.
Save jaawerth/755b9f4d9dc0daeab70f1a64fafd4d78 to your computer and use it in GitHub Desktop.
/*
Usage:
1. Place loader in a webpack loader directory
2. Add the following to webpack loader configuration:
{ test: /angular(\.min)?\.js/, loader: 'angular-remove-di' },
*/
function removeAngularDI(angular) {
var ANGULAR_PROVIDER_TYPES = [
'factory',
'service',
'constant',
'provider',
'value'
];
var ANGULAR_PROVIDERS = [
'$scope', '$WINDOW', '$DOCUMENT', '$SCOPE', '$provide', '$rootElement', '$compileProvider', '$rootScope',
'$compile', '$injector', '$destroy', '$locationProvider', '$animateProvider', '$filterProvider',
'$controllerProvider', '$isolateScope', '$isolateScopeNoTemplate', '$route', '$inject', '$http', '$log',
'$delegate', '$window', '$location', '$anchorScroll', '$animate', '$q', '$sniffer', '$document',
'$cacheFactory', '$node', '$element', '$exceptionHandler', '$interpolate', '$templateRequest',
'$parse', '$controller', '$sce', '$binding', '$destroyed', '$httpParamSerializerJQLike',
'$httpParamSerializer', '$httpBackend', '$templateCache', '$xhrFactory', '$browser', '$interval',
'$1', '$locationChangeStart', '$locationChangeSuccess', '$filter', '$timeout', '$digest',
'$apply', '$sanitize', '$sceDelegate', '$locale', '$attrs', '$index', '$classCounts', '$includeContentLoaded',
'$includeContentError', '$includeContentRequested', '$asyncValidators', '$pending', '$selectController'
];
var LAZY_PROVIDER_REGISTRARS = {
provider: '$provide',
factory: '$provide',
service: '$provide',
constant: '$provide',
value: '$provide',
decorator: '$provide',
controller: '$controllerProvider',
directive: '$compileProvider',
filter: '$filterProvider',
animation: '$animationProvider'
};
var DEFAULT_PROVIDERS = [
{
type: 'constant',
name: 'uiAliasConfig',
value: {}
}
];
var LAZY_PROVIDERS = [
'$exceptionHandler',
'$sanitize'
];
function createMonolith(angular) {
if (angular.monolith) {
return;
}
var monolith = angular.module('app', []);
registerMonolithSingleton(angular, monolith);
registerAngularExports(angular, monolith);
registerSupplementaryAngularFactories(angular, monolith);
registerLazyProviderRegistrars(angular, monolith);
registerShimProviders(angular, monolith);
registerProviderExporter(angular, monolith);
monolith.config(function() {
registerLazyProviders(angular, monolith);
});
registerBootstrapShim(angular, monolith, function() {
registerProviderExporter(angular, monolith);
});
angular.monolith = monolith;
return monolith;
}
function registerMonolithSingleton(angular, monolith) {
angular.module = function(name) {
return monolith;
};
}
function registerAngularExports(angular, monolith) {
ANGULAR_PROVIDERS.forEach(function(providerName) {
registerExport(monolith, {exports: angular}, providerName, 'angular');
});
}
function registerSupplementaryAngularFactories(angular, monolith) {
angular['$registerDirective'] = function(tag, definition) {
var directiveName = tag.replace(/-([a-z])/g, function (g) {
return g[1].toUpperCase();
});
return monolith.directive(directiveName, definition);
};
}
function registerLazyProviderRegistrars(angular, monolith) {
monolith.config(function($injector) {
monolith.injector = monolith.configInjector = $injector;
Object.keys(LAZY_PROVIDER_REGISTRARS).forEach(function(providerType) {
monolith[providerType] = function(name) {
var provider = $injector.get(LAZY_PROVIDER_REGISTRARS[providerType]);
var register = provider[providerType] || provider.register;
register.apply(provider, arguments);
return this;
}
});
});
monolith.run(function($injector) {
monolith.injector = monolith.runInjector = $injector;
});
}
function registerShimProviders(angular, monolith) {
DEFAULT_PROVIDERS.forEach(function(shimProvider) {
monolith[shimProvider.type].call(monolith, shimProvider.name, shimProvider.value);
});
}
function registerBootstrapShim(angular, monolith, callback) {
var bootstrapped = false;
var bootstrap = angular.bootstrap.bind(angular);
angular.bootstrap = function() {
if (bootstrapped) {
return;
}
bootstrapped = true;
bootstrap.apply(this, arguments);
monolith.run = function(handler) {
monolith.runInjector.invoke(handler);
return this;
};
monolith.config = function(handler) {
monolith.configInjector.invoke(handler);
return this;
};
callback();
};
}
function registerProviderExporter(angular, monolith) {
var registerProvider = {};
ANGULAR_PROVIDER_TYPES.forEach(function(providerType) {
registerProvider[providerType] = monolith[providerType].bind(monolith);
});
angular.exportProviders = function(module, exports, dirname, filename) {
ANGULAR_PROVIDER_TYPES.forEach(function(providerType) {
monolith[providerType] = function(providerName, provider) {
registerExport(monolith, module, providerName, filename);
if (providerType === 'provider') {
registerExport(monolith, module, providerName + 'Provider', filename);
}
if (~LAZY_PROVIDERS.indexOf(providerName)) {
providerName += 'Lazy';
}
return registerProvider[providerType].apply(this, arguments);
};
});
};
}
function registerLazyProviders(angular, monolith) {
LAZY_PROVIDERS.forEach(function(providerName) {
monolith.factory(providerName, function($injector) {
return function() {
var provider;
try {
provider = $injector.get(providerName + 'Lazy');
} catch (err) {
provider = $injector.get(providerName);
}
return provider.apply(this, arguments);
}
});
});
}
function registerExport(monolith, module, name, filename) {
if (!(module.exports instanceof Object)) {
module.exports = {};
}
Object.defineProperty(module.exports, name, {
get: function() {
angular.bootstrap(document.body, ['app']);
var injector = (name === '$provide' || ~name.indexOf('Provider')) ? monolith.configInjector : monolith.runInjector;
var value = injector.get(name);
delete module.exports[name];
module.exports[name] = value;
return value;
},
configurable: true
});
}
createMonolith(angular);
}
module.exports = function (content) {
this.cacheable && this.cacheable();
return content + '; (' + removeAngularDI.toString() + ')(window.angular);';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment