on-demand reloading of client-side assets

v1.18.x
Julian Lam 11 years ago
parent 5886ccc419
commit 3e033043f3

@ -19,35 +19,37 @@ module.exports = function(Meta) {
hash: +new Date(), hash: +new Date(),
prepared: false, prepared: false,
minFile: 'nodebb.min.js', minFile: 'nodebb.min.js',
scripts: [ scripts: {
'vendor/jquery/js/jquery.js', base: [
'vendor/jquery/js/jquery-ui-1.10.4.custom.js', 'public/vendor/jquery/js/jquery.js',
'vendor/jquery/timeago/jquery.timeago.min.js', 'public/vendor/jquery/js/jquery-ui-1.10.4.custom.js',
'vendor/jquery/js/jquery.form.min.js', 'public/vendor/jquery/timeago/jquery.timeago.min.js',
'vendor/jquery/serializeObject/jquery.ba-serializeobject.min.js', 'public/vendor/jquery/js/jquery.form.min.js',
'vendor/jquery/deserialize/jquery.deserialize.min.js', 'public/vendor/jquery/serializeObject/jquery.ba-serializeobject.min.js',
'vendor/bootstrap/js/bootstrap.min.js', 'public/vendor/jquery/deserialize/jquery.deserialize.min.js',
'vendor/jquery/bootstrap-tagsinput/bootstrap-tagsinput.min.js', 'public/vendor/bootstrap/js/bootstrap.min.js',
'vendor/requirejs/require.js', 'public/vendor/jquery/bootstrap-tagsinput/bootstrap-tagsinput.min.js',
'vendor/bootbox/bootbox.min.js', 'public/vendor/requirejs/require.js',
'vendor/tinycon/tinycon.js', 'public/vendor/bootbox/bootbox.min.js',
'vendor/xregexp/xregexp.js', 'public/vendor/tinycon/tinycon.js',
'vendor/xregexp/unicode/unicode-base.js', 'public/vendor/xregexp/xregexp.js',
'vendor/buzz/buzz.min.js', 'public/vendor/xregexp/unicode/unicode-base.js',
'../node_modules/templates.js/lib/templates.js', 'public/vendor/buzz/buzz.min.js',
'src/utils.js', './node_modules/templates.js/lib/templates.js',
'src/app.js', 'public/src/utils.js',
'src/ajaxify.js', 'public/src/app.js',
'src/variables.js', 'public/src/ajaxify.js',
'src/widgets.js', 'public/src/variables.js',
'src/translator.js', 'public/src/widgets.js',
'src/helpers.js', 'public/src/translator.js',
'src/overrides.js' 'public/src/helpers.js',
] 'public/src/overrides.js'
]
}
}; };
Meta.js.loadRJS = function(callback) { Meta.js.loadRJS = function(callback) {
var rjsPath = path.join(__dirname, '../..', '/public/src'); var rjsPath = path.join(__dirname, '../../public/src');
async.parallel({ async.parallel({
forum: function(next) { forum: function(next) {
@ -65,75 +67,53 @@ module.exports = function(Meta) {
rjsFiles = rjsFiles.filter(function(file) { rjsFiles = rjsFiles.filter(function(file) {
return file.match('admin') === null; return file.match('admin') === null;
}).map(function(file) { }).map(function(file) {
return path.join('src', file.replace(rjsPath, '')); return path.join('public/src', file.replace(rjsPath, ''));
}); });
Meta.js.scripts = Meta.js.scripts.concat(rjsFiles); Meta.js.scripts.rjs = rjsFiles;
callback(); callback();
}); });
}; };
Meta.js.prepare = function (callback) { Meta.js.prepare = function (callback) {
plugins.fireHook('filter:scripts.get', Meta.js.scripts, function(err, scripts) { async.parallel([
var jsPaths = scripts.map(function (jsPath) { async.apply(Meta.js.loadRJS), // Require.js scripts
jsPath = path.normalize(jsPath); async.apply(getPluginScripts), // plugin scripts via filter:scripts.get
function(next) { // client scripts via "scripts" config in plugin.json
if (jsPath.substring(0, 7) === 'plugins') { var pluginsScripts = [],
var matches = _.map(plugins.staticDirs, function(realPath, mappedPath) { pluginDirectories = [],
if (jsPath.match(mappedPath)) { clientScripts = [];
return mappedPath;
} else { pluginsScripts = plugins.clientScripts.filter(function(path) {
return null; if (path.indexOf('.js') !== -1) {
} return true;
}).filter(function(a) { return a; });
if (matches.length) {
var relPath = jsPath.slice(('plugins/' + matches[0]).length),
pluginId = matches[0].split(path.sep)[0];
return plugins.staticDirs[matches[0]] + relPath;
} else {
winston.warn('[meta.scripts.get] Could not resolve mapped path: ' + jsPath + '. Are you sure it is defined by a plugin?');
return null;
}
} else { } else {
return path.join(__dirname, '../..', '/public', jsPath); pluginDirectories.push(path);
return false;
} }
}); });
Meta.js.scripts = jsPaths.filter(function(path) { // Add plugin scripts
return path !== null; Meta.js.scripts.client = pluginsScripts;
});
var pluginDirectories = []; // Add plugin script directories
async.each(pluginDirectories, function(directory, next) {
plugins.clientScripts = plugins.clientScripts.filter(function(path) { utils.walk(directory, function(err, scripts) {
if (path.indexOf('.js') !== -1) { Meta.js.scripts.client = Meta.js.scripts.client.concat(scripts);
return true; next(err);
} else { });
pluginDirectories.push(path); }, next);
return false; }
} ], function(err) {
}); if (err) return callback(err);
// Add plugin scripts
Meta.js.scripts = Meta.js.scripts.concat(plugins.clientScripts);
async.each(pluginDirectories, function(directory, next) {
utils.walk(directory, function(err, scripts) {
Meta.js.scripts = Meta.js.scripts.concat(scripts);
next(err);
});
}, function(err) {
// Translate into relative paths
Meta.js.scripts = Meta.js.scripts.map(function(script) {
return path.relative(path.resolve(__dirname, '../..'), script).replace(/\\/g, '/');
});
Meta.js.prepared = true; // Convert all scripts to paths relative to the NodeBB base directory
callback(err); var basePath = path.resolve(__dirname, '../..');
Meta.js.scripts.all = Meta.js.scripts.base.concat(Meta.js.scripts.rjs, Meta.js.scripts.plugin, Meta.js.scripts.client).map(function(script) {
return path.relative(basePath, script).replace(/\\/g, '/');
}); });
callback();
}); });
}; };
@ -183,13 +163,11 @@ module.exports = function(Meta) {
} }
}); });
Meta.js.loadRJS(function() { Meta.js.prepare(function() {
Meta.js.prepare(function() { minifier.send({
minifier.send({ action: 'js',
action: 'js', minify: minify,
minify: minify, scripts: Meta.js.scripts.all
scripts: Meta.js.scripts
});
}); });
}); });
}; };
@ -199,4 +177,39 @@ module.exports = function(Meta) {
Meta.js.minifierProc.kill('SIGTERM'); Meta.js.minifierProc.kill('SIGTERM');
} }
}; };
function getPluginScripts(callback) {
plugins.fireHook('filter:scripts.get', [], function(err, scripts) {
if (err) callback(err, []);
var jsPaths = scripts.map(function (jsPath) {
jsPath = path.normalize(jsPath);
// if (jsPath.substring(0, 7) === 'plugins') {
var matches = _.map(plugins.staticDirs, function(realPath, mappedPath) {
if (jsPath.match(mappedPath)) {
return mappedPath;
} else {
return null;
}
}).filter(function(a) { return a; });
if (matches.length) {
var relPath = jsPath.slice(('plugins/' + matches[0]).length),
pluginId = matches[0].split(path.sep)[0];
return plugins.staticDirs[matches[0]] + relPath;
} else {
winston.warn('[meta.scripts.get] Could not resolve mapped path: ' + jsPath + '. Are you sure it is defined by a plugin?');
return null;
}
// } else {
// return path.join(__dirname, '../..', jsPath);
// }
});
Meta.js.scripts.plugin = jsPaths.filter(Boolean);
callback();
});
};
}; };

@ -35,8 +35,8 @@ function setupPluginSourceMapping(app) {
development mode (`./nodebb dev`) development mode (`./nodebb dev`)
*/ */
var routes = plugins.clientScripts, var routes = plugins.clientScripts,
mapping, prefix = __dirname.split(path.sep).length - 1,
prefix = __dirname.split(path.sep).length - 1; mapping;
routes.forEach(function(route) { routes.forEach(function(route) {
mapping = '/' + route.split(path.sep).slice(prefix).join('/'); mapping = '/' + route.split(path.sep).slice(prefix).join('/');

Loading…
Cancel
Save