From 07a860f6b503a2916739e5f323589b1a03904473 Mon Sep 17 00:00:00 2001 From: Mikael Korpela Date: Sun, 28 Aug 2016 14:52:05 +0300 Subject: [PATCH] feat(angular): disable Angular debug data in production (#1457) 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 --- config/lib/express.js | 1 + modules/core/client/app/config.js | 1 + modules/core/client/app/init.js | 8 +++- .../core/server/views/layout.server.view.html | 3 +- .../tests/server/core.server.config.tests.js | 39 ++++++++++++++++++- 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/config/lib/express.js b/config/lib/express.js index b620896753..d166d40e29 100644 --- a/config/lib/express.js +++ b/config/lib/express.js @@ -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) { diff --git a/modules/core/client/app/config.js b/modules/core/client/app/config.js index 3231e35e97..754a6cbcd6 100644 --- a/modules/core/client/app/config.js +++ b/modules/core/client/app/config.js @@ -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 diff --git a/modules/core/client/app/init.js b/modules/core/client/app/init.js index dbfe916f95..45c5b06b38 100644 --- a/modules/core/client/app/init.js +++ b/modules/core/client/app/init.js @@ -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); diff --git a/modules/core/server/views/layout.server.view.html b/modules/core/server/views/layout.server.view.html index 2003e92c27..6018dd53d8 100644 --- a/modules/core/server/views/layout.server.view.html +++ b/modules/core/server/views/layout.server.view.html @@ -56,7 +56,8 @@ diff --git a/modules/core/tests/server/core.server.config.tests.js b/modules/core/tests/server/core.server.config.tests.js index 9eeeb9e715..4e8a3882dc 100644 --- a/modules/core/tests/server/core.server.config.tests.js +++ b/modules/core/tests/server/core.server.config.tests.js @@ -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, @@ -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(); + }); + }); + }); + + }); + });