Skip to content

Node.js based forum software built for the modern web

Build plugin `staticDirs`

Peter Jaszkowiak186209a0a3e7
diff --git a/build.js b/build.js
index a8ad5007d1..73013231d5 100644
--- a/build.js
+++ b/build.js
@@ -62,6 +62,7 @@ exports.buildTargets = function (targets, callback) {
var startTime = Date.now();
async.series([
meta.js.linkModules,
+ meta.js.linkStatics,
async.apply(meta.js.minify, 'nodebb.min.js'),
async.apply(meta.js.minify, 'acp.min.js')
], step.bind(this, startTime, 'js', next));
diff --git a/src/file.js b/src/file.js
index 65f86a0bdd..d8fa128e5e 100644
--- a/src/file.js
+++ b/src/file.js
@@ -110,4 +110,9 @@ file.link = function link(filePath, destPath, cb) {
}
};
+file.linkDirs = function linkDirs(sourceDir, destDir, callback) {
+ var type = (process.platform === 'win32') ? 'junction' : 'dir';
+ fs.symlink(sourceDir, destDir, type, callback);
+};
+
module.exports = file;
diff --git a/src/meta/js.js b/src/meta/js.js
index d479e9161c..a44d18baba 100644
--- a/src/meta/js.js
+++ b/src/meta/js.js
@@ -94,7 +94,7 @@ module.exports = function (Meta) {
if (err) {
return callback(err);
}
- async.each(Object.keys(Meta.js.scripts.modules), function (relPath, next) {
+ async.eachLimit(Object.keys(Meta.js.scripts.modules), 1000, function (relPath, next) {
var filePath = path.join(__dirname, '../../', Meta.js.scripts.modules[relPath]);
var destPath = path.join(__dirname, '../../build/public/src/modules', relPath);
@@ -109,6 +109,26 @@ module.exports = function (Meta) {
});
};
+ Meta.js.linkStatics = function (callback) {
+ rimraf(path.join(__dirname, '../../build/public/plugins'), function (err) {
+ if (err) {
+ return callback(err);
+ }
+ async.eachLimit(Object.keys(plugins.staticDirs), 1000, function (mappedPath, next) {
+ var sourceDir = plugins.staticDirs[mappedPath];
+ var destDir = path.join(__dirname, '../../build/public/plugins', mappedPath);
+
+ mkdirp(path.dirname(destDir), function (err) {
+ if (err) {
+ return next(err);
+ }
+
+ file.linkDirs(sourceDir, destDir, next);
+ });
+ }, callback);
+ });
+ };
+
Meta.js.minify = function (target, callback) {
winston.verbose('[meta/js] Minifying ' + target);
diff --git a/src/plugins/load.js b/src/plugins/load.js
index cf6e0b4780..01dedc4e45 100644
--- a/src/plugins/load.js
+++ b/src/plugins/load.js
@@ -56,6 +56,7 @@ module.exports = function (Plugins) {
async.apply(mapFiles, pluginData, 'less', 'lessFiles'),
async.apply(mapClientSideScripts, pluginData),
async.apply(mapClientModules, pluginData),
+ async.apply(mapStaticDirectories, pluginData, pluginData.path),
], next);
}, next);
}
@@ -299,6 +300,7 @@ module.exports = function (Plugins) {
pluginData.version = packageData.version;
pluginData.repository = packageData.repository;
pluginData.nbbpm = packageData.nbbpm;
+ pluginData.path = pluginPath;
} catch(err) {
var pluginDir = pluginPath.split(path.sep);
pluginDir = pluginDir[pluginDir.length - 1];
diff --git a/src/routes/index.js b/src/routes/index.js
index 506a0063ec..0b9e6fce9a 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -15,7 +15,6 @@ var metaRoutes = require('./meta');
var apiRoutes = require('./api');
var adminRoutes = require('./admin');
var feedRoutes = require('./feeds');
-var pluginRoutes = require('./plugins');
var authRoutes = require('./authentication');
var helpers = require('./helpers');
@@ -123,7 +122,6 @@ module.exports = function (app, middleware, hotswapIds) {
metaRoutes(router, middleware, controllers);
apiRoutes(router, middleware, controllers);
feedRoutes(router, middleware, controllers);
- pluginRoutes(router, middleware, controllers);
mainRoutes(router, middleware, controllers);
topicRoutes(router, middleware, controllers);
@@ -152,6 +150,10 @@ module.exports = function (app, middleware, hotswapIds) {
app.use(relativePath + '/assets', express.static(path.join(__dirname, '../../public'), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));
+ // TODO: deprecate?
+ app.use(relativePath + '/plugins', express.static(path.join(__dirname, '../../build/public/plugins'), {
+ maxAge: app.enabled('cache') ? 5184000000 : 0
+ }));
// DEPRECATED
var deprecatedPaths = [
diff --git a/src/routes/plugins.js b/src/routes/plugins.js
deleted file mode 100644
index 37e23c5ee6..0000000000
--- a/src/routes/plugins.js
+++ /dev/null
@@ -1,40 +0,0 @@
-"use strict";
-
-var _ = require('underscore');
-var path = require('path');
-
-var plugins = require('../plugins');
-
-module.exports = function (app, middleware, controllers) {
- // Static Assets
- app.get('/plugins/:id/*', middleware.addExpiresHeaders, function (req, res, next) {
-
- var relPath = req._parsedUrl.pathname.replace('/plugins/', '');
-
- var matches = _.map(plugins.staticDirs, function (realPath, mappedPath) {
- if (relPath.match(mappedPath)) {
- var pathToFile = path.join(plugins.staticDirs[mappedPath], decodeURIComponent(relPath.slice(mappedPath.length)));
- if (pathToFile.startsWith(plugins.staticDirs[mappedPath])) {
- return pathToFile;
- }
- }
-
- return null;
- }).filter(Boolean);
-
- if (!matches || !matches.length) {
- return next();
- }
-
- res.sendFile(matches[0], {}, function (err) {
- if (err) {
- if (err.code === 'ENOENT') {
- // File doesn't exist, this isn't an error, to send to 404 handler
- return next();
- } else {
- return next(err);
- }
- }
- });
- });
-};
\ No newline at end of file