2012-02-24 20:45:02 +01:00
|
|
|
var path = require('path');
|
2012-02-25 17:23:44 +01:00
|
|
|
var minify = require('../../utils/Minify');
|
2012-03-01 19:00:58 +01:00
|
|
|
var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
|
2012-03-04 23:45:33 +01:00
|
|
|
var CachingMiddleware = require('../../utils/caching_middleware');
|
|
|
|
var settings = require("../../utils/Settings");
|
|
|
|
var Yajsml = require('yajsml');
|
|
|
|
var fs = require("fs");
|
|
|
|
var ERR = require("async-stacktrace");
|
2012-02-24 20:45:02 +01:00
|
|
|
|
2012-02-25 16:53:15 +01:00
|
|
|
exports.expressCreateServer = function (hook_name, args, cb) {
|
2012-02-26 15:34:03 +01:00
|
|
|
/* Handle static files for plugins:
|
|
|
|
paths like "/static/plugins/ep_myplugin/js/test.js"
|
|
|
|
are rewritten into ROOT_PATH_OF_MYPLUGIN/static/js/test.js,
|
2012-02-26 13:54:32 +01:00
|
|
|
commonly ETHERPAD_ROOT/node_modules/ep_myplugin/static/js/test.js
|
2012-02-26 00:45:06 +01:00
|
|
|
*/
|
2012-03-04 23:45:33 +01:00
|
|
|
args.app.get(/^\/minified\/plugins\/([^\/]+)\/static\/(.*)/, function(req, res, next) {
|
2012-02-26 15:34:03 +01:00
|
|
|
var plugin_name = req.params[0];
|
2012-03-04 23:45:33 +01:00
|
|
|
var modulePath = req.url.split("?")[0].substr("/minified/plugins/".length);
|
|
|
|
var fullPath = require.resolve(modulePath);
|
2012-02-25 19:43:00 +01:00
|
|
|
|
2012-02-26 15:34:03 +01:00
|
|
|
if (plugins.plugins[plugin_name] == undefined) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2012-03-04 23:45:33 +01:00
|
|
|
fs.readFile(fullPath, "utf8", function(err, data){
|
|
|
|
if(ERR(err)) return;
|
|
|
|
|
|
|
|
res.send("require.define('" + modulePath + "', function (require, exports, module) {" + data + "})");
|
|
|
|
})
|
2012-02-26 22:01:52 +01:00
|
|
|
|
2012-03-04 23:45:33 +01:00
|
|
|
//require.define("/plugins.js", function (require, exports, module) {
|
|
|
|
|
|
|
|
//res.sendfile(fullPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Cache both minified and static.
|
|
|
|
var assetCache = new CachingMiddleware;
|
|
|
|
args.app.all('/(minified|static)/*', assetCache.handle);
|
|
|
|
|
|
|
|
// Minify will serve static files compressed (minify enabled). It also has
|
|
|
|
// file-specific hacks for ace/require-kernel/etc.
|
|
|
|
args.app.all('/static/:filename(*)', minify.minify);
|
|
|
|
|
|
|
|
// Setup middleware that will package JavaScript files served by minify for
|
|
|
|
// CommonJS loader on the client-side.
|
|
|
|
var jsServer = new (Yajsml.Server)({
|
|
|
|
rootPath: 'minified/'
|
|
|
|
, rootURI: 'http://localhost:' + settings.port + '/static/js/'
|
2012-02-25 19:43:00 +01:00
|
|
|
});
|
|
|
|
|
2012-03-04 23:45:33 +01:00
|
|
|
var StaticAssociator = Yajsml.associators.StaticAssociator;
|
|
|
|
var associations =
|
|
|
|
Yajsml.associators.associationsForSimpleMapping(minify.tar);
|
|
|
|
var associator = new StaticAssociator(associations);
|
|
|
|
jsServer.setAssociator(associator);
|
|
|
|
args.app.use(jsServer);
|
|
|
|
|
|
|
|
// serve plugin definitions
|
|
|
|
// not very static, but served here so that client can do require("pluginfw/static/js/plugin-definitions.js");
|
|
|
|
args.app.get('/pluginfw/plugin-definitions.json', function (req, res, next) {
|
|
|
|
res.header("Content-Type","application/json; charset: utf-8");
|
|
|
|
res.write(JSON.stringify({"plugins": plugins.plugins, "parts": plugins.parts}));
|
|
|
|
res.end();
|
2012-02-24 20:45:02 +01:00
|
|
|
});
|
|
|
|
}
|