From 90da6d889d92c9e2e1400a24d5d09f5af0b55d61 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Thu, 18 May 2017 01:32:20 -0600 Subject: [PATCH] Only load necessary plugin data Fix tests to work in this case Add more verbose messages to plugins/data --- src/meta/build.js | 12 ++++++-- src/plugins/data.js | 27 ++++++++-------- src/plugins/load.js | 31 ++++++++++++++----- test/build.js | 8 +++++ test/mocks/databasemock.js | 63 ++++++++++++++++++++++---------------- test/plugins.js | 1 - 6 files changed, 92 insertions(+), 50 deletions(-) diff --git a/src/meta/build.js b/src/meta/build.js index e88bbb17e9..dfeb4d7825 100644 --- a/src/meta/build.js +++ b/src/meta/build.js @@ -93,7 +93,7 @@ aliases = Object.keys(aliases).reduce(function (prev, key) { return prev; }, {}); -function beforeBuild(callback) { +function beforeBuild(targets, callback) { var db = require('../database'); var plugins = require('../plugins'); meta = require('../meta'); @@ -101,7 +101,9 @@ function beforeBuild(callback) { async.series([ db.init, meta.themes.setupPaths, - plugins.prepareForBuild, + function (next) { + plugins.prepareForBuild(targets, next); + }, ], function (err) { if (err) { winston.error('[build] Encountered error preparing for build: ' + err.message); @@ -160,6 +162,8 @@ function build(targets, callback) { return arr.indexOf(target) === i; }); + winston.verbose('[build] building the following targets: ' + targets.join(', ')); + if (typeof callback !== 'function') { callback = function (err) { if (err) { @@ -179,7 +183,9 @@ function build(targets, callback) { var startTime; var totalTime; async.series([ - beforeBuild, + function (next) { + beforeBuild(targets, next); + }, function (next) { var parallel = os.cpus().length > 1 && !nconf.get('series'); if (parallel) { diff --git a/src/plugins/data.js b/src/plugins/data.js index d2f1278c9d..965ad07bfc 100644 --- a/src/plugins/data.js +++ b/src/plugins/data.js @@ -125,7 +125,12 @@ function getStaticDirectories(pluginData, callback) { next(); }); }, function (err) { - callback(err, staticDirs); + if (err) { + return callback(err); + } + winston.verbose('[plugins] found ' + Object.keys(staticDirs).length + + ' static directories for ' + pluginData.id); + callback(null, staticDirs); }); } Data.getStaticDirectories = getStaticDirectories; @@ -135,9 +140,7 @@ function getFiles(pluginData, type, callback) { return callback(); } - if (global.env === 'development') { - winston.verbose('[plugins] Found ' + pluginData[type].length + ' ' + type + ' file(s) for plugin ' + pluginData.id); - } + winston.verbose('[plugins] Found ' + pluginData[type].length + ' ' + type + ' file(s) for plugin ' + pluginData.id); var files = pluginData[type].map(function (file) { return path.join(pluginData.id, file); @@ -202,7 +205,7 @@ function getScripts(pluginData, target, callback) { return callback(err); } - if (scripts.length && global.env === 'development') { + if (scripts.length) { winston.verbose('[plugins] Found ' + scripts.length + ' js file(s) for plugin ' + pluginData.id); } callback(err, scripts); @@ -250,10 +253,9 @@ function getModules(pluginData, callback) { return callback(err); } - if (global.env === 'development') { - var len = Object.keys(modules).length; - winston.verbose('[plugins] Found ' + len + ' AMD-style module(s) for plugin ' + pluginData.id); - } + var len = Object.keys(modules).length; + winston.verbose('[plugins] Found ' + len + ' AMD-style module(s) for plugin ' + pluginData.id); + callback(null, modules); }); } @@ -290,10 +292,9 @@ function getSoundpack(pluginData, callback) { return callback(err); } - if (global.env === 'development') { - var len = Object.keys(soundpack).length; - winston.verbose('[plugins] Found ' + len + ' sound file(s) for plugin ' + pluginData.id); - } + var len = Object.keys(soundpack.sounds).length; + winston.verbose('[plugins] Found ' + len + ' sound file(s) for plugin ' + pluginData.id); + callback(null, soundpack); }); } diff --git a/src/plugins/load.js b/src/plugins/load.js index b939769588..b0575670cf 100644 --- a/src/plugins/load.js +++ b/src/plugins/load.js @@ -67,19 +67,39 @@ module.exports = function (Plugins) { }); } - Plugins.prepareForBuild = function (callback) { + Plugins.prepareForBuild = function (targets, callback) { Plugins.cssFiles.length = 0; Plugins.lessFiles.length = 0; Plugins.clientScripts.length = 0; Plugins.acpScripts.length = 0; Plugins.soundpacks.length = 0; + var map = { + 'plugin static dirs': ['staticDirs'], + 'requirejs modules': ['modules'], + 'client js bundle': ['clientScripts'], + 'admin js bundle': ['acpScripts'], + 'client side styles': ['cssFiles', 'lessFiles'], + 'admin control panel styles': ['cssFiles', 'lessFiles'], + sounds: ['soundpack'], + }; + + var fields = targets.reduce(function (prev, target) { + if (!map[target]) { + return prev; + } + return prev.concat(map[target]); + }, []).filter(function (field, i, arr) { + return arr.indexOf(field) === i; + }); + + winston.verbose('[plugins] loading the following fields from plugin data: ' + fields.join(', ')); + async.waterfall([ Plugins.data.getActive, function (plugins, next) { async.each(plugins, function (pluginData, next) { - // TODO: only load the data that's needed for the build - registerPluginAssets(pluginData, true, next); + registerPluginAssets(pluginData, fields, next); }, next); }, ], callback); @@ -101,10 +121,7 @@ module.exports = function (Plugins) { registerHooks(pluginData, next); }, function (next) { - // TODO: change this from `true` to `['soundpack']` - // this will skip several build-only plugin loading methods - // and only load soundpacks, which will speed up startup - registerPluginAssets(pluginData, true, next); + registerPluginAssets(pluginData, ['soundpack'], next); }, ], function (err) { if (err) { diff --git a/test/build.js b/test/build.js index 3b6890ebe6..1f73f5746b 100644 --- a/test/build.js +++ b/test/build.js @@ -5,6 +5,10 @@ var assert = require('assert'); var db = require('./mocks/databasemock'); describe('Build', function () { + before(function (done) { + db.setupMockDefaults(done); + }); + it('should build all assets', function (done) { this.timeout(50000); var build = require('../src/meta/build'); @@ -13,4 +17,8 @@ describe('Build', function () { done(); }); }); + + after(function (done) { + db.emptydb(done); + }); }); diff --git a/test/mocks/databasemock.js b/test/mocks/databasemock.js index 4432f944f1..ca280dda9e 100644 --- a/test/mocks/databasemock.js +++ b/test/mocks/databasemock.js @@ -83,42 +83,19 @@ before(function (done) { this.timeout(30000); - var meta; - async.waterfall([ + async.series([ function (next) { db.init(next); }, function (next) { - db.emptydb(next); - }, - function (next) { - winston.info('test_database flushed'); - meta = require('../../src/meta'); - setupDefaultConfigs(meta, next); - }, - function (next) { - meta.configs.init(next); + setupMockDefaults(next); }, function (next) { db.initSessionStore(next); }, function (next) { - meta.dependencies.check(next); - }, - function (next) { - meta.config.postDelay = 0; - meta.config.initialPostDelay = 0; - meta.config.newbiePostDelay = 0; + var meta = require('../../src/meta'); - enableDefaultPlugins(next); - }, - function (next) { - meta.themes.set({ - type: 'local', - id: 'nodebb-theme-persona', - }, next); - }, - function (next) { // nconf defaults, if not set in config if (!nconf.get('sessionKey')) { nconf.set('sessionKey', 'express.sid'); @@ -154,6 +131,40 @@ ], done); }); + function setupMockDefaults(callback) { + var meta = require('../../src/meta'); + + async.series([ + function (next) { + db.emptydb(next); + }, + function (next) { + winston.info('test_database flushed'); + setupDefaultConfigs(meta, next); + }, + function (next) { + meta.configs.init(next); + }, + function (next) { + meta.dependencies.check(next); + }, + function (next) { + meta.config.postDelay = 0; + meta.config.initialPostDelay = 0; + meta.config.newbiePostDelay = 0; + + enableDefaultPlugins(next); + }, + function (next) { + meta.themes.set({ + type: 'local', + id: 'nodebb-theme-persona', + }, next); + }, + ], callback); + } + db.setupMockDefaults = setupMockDefaults; + function setupDefaultConfigs(meta, next) { winston.info('Populating database with default configs, if not already set...\n'); diff --git a/test/plugins.js b/test/plugins.js index 97a9c1908b..caccbe2851 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -16,7 +16,6 @@ describe('Plugins', function () { assert.ifError(err); assert(plugins.libraries[pluginId]); assert(plugins.loadedHooks['static:app.load']); - assert(plugins.staticDirs['nodebb-plugin-markdown/js']); done(); });