From 94a505a6bbca6d36332ef723bdf88d3fff3ad72c Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 4 Apr 2016 20:43:21 -0400 Subject: [PATCH] removed symlink logic and using route bridging instead. Also allowed plugins to define modules from their plugin.json --- Gruntfile.js | 1 - public/src/modules/.gitignore | 35 --------------------------------- src/meta.js | 1 - src/meta/js.js | 37 +++++++++++++---------------------- src/plugins/load.js | 25 ++++++++++++++++++++--- src/webserver.js | 2 +- 6 files changed, 37 insertions(+), 64 deletions(-) delete mode 100644 public/src/modules/.gitignore diff --git a/Gruntfile.js b/Gruntfile.js index f02fa6bf19..a6aca2083a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -70,7 +70,6 @@ module.exports = function(grunt) { clientUpdated: { files: [ 'public/src/**/*.js', - '!public/src/modules/*.js', 'node_modules/nodebb-*/*.js', 'node_modules/nodebb-*/**/*.js', '!node_modules/nodebb-*/node_modules/**', 'node_modules/templates.js/lib/templates.js', diff --git a/public/src/modules/.gitignore b/public/src/modules/.gitignore deleted file mode 100644 index d18329c844..0000000000 --- a/public/src/modules/.gitignore +++ /dev/null @@ -1,35 +0,0 @@ -# Warning: This directory contains auto-generated files (symlinked), and should -# not be tracked by git. If you want to specifically have a file tracked by git, -# please include it in this file. - - - -# Ignore all files in this directory... -/* - -# except these folders... -!/settings - -# and these files... -!/.gitignore -!/alerts.js -!/autocomplete.js -!/chat.js -!/components.js -!/coverPhoto.js -!/csrf.js -!/helpers.js -!/iconSelect.js -!/navigator.js -!/notifications.js -!/postSelect.js -!/search.js -!/settings.js -!/share.js -!/sort.js -!/sounds.js -!/string.js -!/taskbar.js -!/topicSelect.js -!/translator.js -!/uploader.js diff --git a/src/meta.js b/src/meta.js index 78251d4996..ffc85c98bf 100644 --- a/src/meta.js +++ b/src/meta.js @@ -61,7 +61,6 @@ var async = require('async'), async.apply(plugins.clearRequireCache), async.apply(plugins.reload), async.apply(plugins.reloadRoutes), - async.apply(Meta.js.symlinkModules), async.apply(Meta.css.minify), async.apply(Meta.js.minify, 'nodebb.min.js'), async.apply(Meta.js.minify, 'acp.min.js'), diff --git a/src/meta/js.js b/src/meta/js.js index eaa7824705..168dce916c 100644 --- a/src/meta/js.js +++ b/src/meta/js.js @@ -6,7 +6,6 @@ var winston = require('winston'), async = require('async'), nconf = require('nconf'), fs = require('fs'), - rimraf = require('rimraf'), file = require('../file'), plugins = require('../plugins'), emitter = require('../emitter'), @@ -79,7 +78,7 @@ module.exports = function(Meta) { 'public/src/modules/string.js' ], - // modules listed below are symlinked to public/src/modules so they can be defined anonymously + // 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', @@ -89,38 +88,30 @@ module.exports = function(Meta) { } }; - Meta.js.symlinkModules = function(callback) { - // Symlink all defined modules to /public/src/modules - var modulesLoaded = 0, - targetPath; + Meta.js.bridgeModules = function(app, callback) { + // Add routes for AMD-type modules to serve those files + console.log('bridging modules:', Meta.js.scripts.modules); + var numBridged = 0; async.series([ function(next) { async.each(Meta.js.scripts.modules, function(localPath, next) { - targetPath = path.join(__dirname, '../../public/src/modules', path.basename(localPath)); - - async.waterfall([ - async.apply(fs.access, localPath, fs.R_OK), - async.apply(rimraf, targetPath), - async.apply(fs.link, localPath, targetPath) - ], function(err) { - if (err) { - winston.error('[meta/js] Could not symlink `' + localPath + '` to modules folder'); - } else { - winston.verbose('[meta/js] Symlinked `' + localPath + '` to modules folder'); - ++modulesLoaded; - } - - next(err); + app.get(path.join('/src/modules/', path.basename(localPath)), function(req, res) { + return res.sendFile(path.join(__dirname, '../../', localPath), { + maxAge: app.enabled('cache') ? 5184000000 : 0 + }); }); + + ++numBridged; + next(); }, next); } ], function(err) { if (err) { - winston.error('[meta/js] Encountered error while symlinking modules:' + err.message); + winston.error('[meta/js] Encountered error while bridging modules:' + err.message); } - winston.verbose('[meta/js] ' + modulesLoaded + ' of ' + Meta.js.scripts.modules.length + ' modules symlinked'); + winston.verbose('[meta/js] ' + numBridged + ' of ' + Meta.js.scripts.modules.length + ' modules bridged'); callback(err); }); }; diff --git a/src/plugins/load.js b/src/plugins/load.js index cf1ff27d9a..676008a85c 100644 --- a/src/plugins/load.js +++ b/src/plugins/load.js @@ -7,8 +7,10 @@ var fs = require('fs'), winston = require('winston'), nconf = require('nconf'), _ = require('underscore'), - file = require('../file'), - utils = require('../../public/src/utils'); + file = require('../file'); + +var utils = require('../../public/src/utils'), + meta = require('../meta'); module.exports = function(Plugins) { @@ -40,6 +42,9 @@ module.exports = function(Plugins) { function(next) { mapClientSideScripts(pluginData, next); }, + function(next) { + mapClientModules(pluginData, next); + }, function(next) { loadLanguages(pluginData, next); } @@ -163,7 +168,21 @@ module.exports = function(Plugins) { } callback(); - } + }; + + function mapClientModules(pluginData, callback) { + if (Array.isArray(pluginData.modules)) { + if (global.env === 'development') { + 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); + })); + } + + callback(); + }; function loadLanguages(pluginData, callback) { if (typeof pluginData.languages !== 'string') { diff --git a/src/webserver.js b/src/webserver.js index 03375c4281..e667862561 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -84,10 +84,10 @@ function initializeNodeBB(callback) { function(next) { plugins.init(app, middleware, next); }, + async.apply(meta.js.bridgeModules, app), function(next) { async.series([ async.apply(meta.templates.compile), - async.apply(meta.js.symlinkModules), async.apply(!skipJS ? meta.js.minify : meta.js.getFromFile, 'nodebb.min.js'), async.apply(!skipJS ? meta.js.minify : meta.js.getFromFile, 'acp.min.js'), async.apply(meta.css.minify),