From 1d7a5f58e2b4ff14aed4118cf59a25d8ef6d710d Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 27 Apr 2016 14:14:22 -0400 Subject: [PATCH] fixes #4576 --- src/meta/js.js | 35 ++++++++++++++++++++--------------- src/plugins/load.js | 17 +++++++++++++---- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/meta/js.js b/src/meta/js.js index 2669aed4d4..6f591cc9fd 100644 --- a/src/meta/js.js +++ b/src/meta/js.js @@ -79,31 +79,36 @@ module.exports = function(Meta) { ], // modules listed below are routed through express (/src/modules) so they can be defined anonymously - modules: [ - './node_modules/chart.js/Chart.js', - './node_modules/mousetrap/mousetrap.js', + modules: { + "Chart.js": './node_modules/chart.js/Chart.js', + "mousetrap.js": './node_modules/mousetrap/mousetrap.js', - 'public/vendor/buzz/buzz.js' - ] + "buzz.js": 'public/vendor/buzz/buzz.js' + } } }; Meta.js.bridgeModules = function(app, callback) { // Add routes for AMD-type modules to serve those files - var numBridged = 0; + var numBridged = 0, + addRoute = function(relPath) { + app.get('/src/modules/' + relPath, function(req, res) { + return res.sendFile(path.join(__dirname, '../../', Meta.js.scripts.modules[relPath]), { + maxAge: app.enabled('cache') ? 5184000000 : 0 + }); + }); + }; async.series([ function(next) { - async.each(Meta.js.scripts.modules, function(localPath, next) { - app.get('/src/modules/' + path.basename(localPath), function(req, res) { - return res.sendFile(path.join(__dirname, '../../', localPath), { - maxAge: app.enabled('cache') ? 5184000000 : 0 - }); - }); + for(var relPath in Meta.js.scripts.modules) { + if (Meta.js.scripts.modules.hasOwnProperty(relPath)) { + addRoute(relPath); + ++numBridged; + } + } - ++numBridged; - next(); - }, next); + next(); } ], function(err) { if (err) { diff --git a/src/plugins/load.js b/src/plugins/load.js index 676008a85c..554df14030 100644 --- a/src/plugins/load.js +++ b/src/plugins/load.js @@ -176,10 +176,19 @@ module.exports = function(Plugins) { winston.verbose('[plugins] Found ' + pluginData.modules.length + ' AMD-style module(s) for plugin ' + pluginData.id); } - meta.js.scripts.modules = meta.js.scripts.modules.concat(pluginData.modules.map(function(file) { - return path.join('./node_modules/', pluginData.id, file); - })); - } + var modules = {}, + strip = pluginData.hasOwnProperty('modulesStrip') ? parseInt(pluginData.modulesStrip, 10) : 0; + + pluginData.modules.forEach(function(file) { + if (strip) { + modules[file.replace(new RegExp('\.?(\/[^\/]+){' + strip + '}\/'), '')] = path.join('./node_modules/', pluginData.id, file); + } else { + modules[path.basename(file)] = path.join('./node_modules/', pluginData.id, file); + } + }); + + meta.js.scripts.modules = _.extend(meta.js.scripts.modules, modules); + } /* one could conceivably add an else..if here for plugins to pass modules in as an Object */ callback(); };