Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
feat(angular): disable Angular debug data in production (#1457)
Browse files Browse the repository at this point in the history
Disable Angular debug data in production for a significant performance
boost.

Passes environment variable from template to app config and from there
to
Angular bootstrap config.

https://docs.angularjs.org/guide/production#disabling-debug-data

See #1294
  • Loading branch information
simison authored and lirantal committed Aug 28, 2016
1 parent c35713d commit 07a860f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/lib/express.js
Expand Up @@ -39,6 +39,7 @@ module.exports.initLocalVariables = function (app) {
app.locals.livereload = config.livereload;
app.locals.logo = config.logo;
app.locals.favicon = config.favicon;
app.locals.env = process.env.NODE_ENV;

// Passing the request url to environment locals
app.use(function (req, res, next) {
Expand Down
1 change: 1 addition & 0 deletions modules/core/client/app/config.js
Expand Up @@ -4,6 +4,7 @@
var applicationModuleName = 'mean';

var service = {
applicationEnvironment: window.env,
applicationModuleName: applicationModuleName,
applicationModuleVendorDependencies: ['ngResource', 'ngAnimate', 'ngMessages', 'ui.router', 'ui.bootstrap', 'ngFileUpload', 'ngImgCrop'],
registerModule: registerModule
Expand Down
8 changes: 6 additions & 2 deletions modules/core/client/app/init.js
Expand Up @@ -10,13 +10,17 @@
.module(app.applicationModuleName)
.config(bootstrapConfig);

function bootstrapConfig($locationProvider, $httpProvider) {
function bootstrapConfig($compileProvider, $locationProvider, $httpProvider) {
$locationProvider.html5Mode(true).hashPrefix('!');

$httpProvider.interceptors.push('authInterceptor');

// Disable debug data for production environment
// @link https://docs.angularjs.org/guide/production
$compileProvider.debugInfoEnabled(app.applicationEnvironment !== 'production');
}

bootstrapConfig.$inject = ['$locationProvider', '$httpProvider'];
bootstrapConfig.$inject = ['$compileProvider', '$locationProvider', '$httpProvider'];

// Then define the init function for starting up the application
angular.element(document).ready(init);
Expand Down
3 changes: 2 additions & 1 deletion modules/core/server/views/layout.server.view.html
Expand Up @@ -56,7 +56,8 @@

<!--Embedding The User Object-->
<script type="text/javascript">
var user = {{ user | json | safe }};
var user = {{ user | json | safe }},
env = "{{ env }}";
</script>

<!--Load The Socket.io File-->
Expand Down
39 changes: 37 additions & 2 deletions modules/core/tests/server/core.server.config.tests.js
Expand Up @@ -10,14 +10,18 @@ var _ = require('lodash'),
path = require('path'),
fs = require('fs'),
mock = require('mock-fs'),
request = require('supertest'),
config = require(path.resolve('./config/config')),
logger = require(path.resolve('./config/lib/logger')),
seed = require(path.resolve('./config/lib/seed'));
seed = require(path.resolve('./config/lib/seed')),
express = require(path.resolve('./config/lib/express'));

/**
* Globals
*/
var user1,
var app,
agent,
user1,
admin1,
userFromSeedConfig,
adminFromSeedConfig,
Expand Down Expand Up @@ -507,4 +511,35 @@ describe('Configuration Tests:', function () {
fileTransport.should.be.false();
});
});

describe('Testing exposing environment as a variable to layout', function () {

['development', 'production', 'test'].forEach(function(env) {
it('should expose environment set to ' + env, function (done) {
// Set env to development for this test
process.env.NODE_ENV = env;

// Gget application
app = express.init(mongoose);
agent = request.agent(app);

// Get rendered layout
agent.get('/')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
.end(function (err, res) {
// Set env back to test
process.env.NODE_ENV = 'test';
// Handle errors
if (err) {
return done(err);
}
res.text.should.containEql('env = "' + env + '"');
return done();
});
});
});

});

});

0 comments on commit 07a860f

Please sign in to comment.