You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
6.9 KiB
JavaScript

11 years ago
'use strict';
9 years ago
var winston = require('winston');
var fork = require('child_process').fork;
var path = require('path');
var async = require('async');
var nconf = require('nconf');
var fs = require('fs');
var mkdirp = require('mkdirp');
9 years ago
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
11 years ago
module.exports = function (Meta) {
11 years ago
Meta.js = {
9 years ago
target: {},
scripts: {
base: [
'./node_modules/jquery/dist/jquery.js',
8 years ago
'./node_modules/socket.io-client/dist/socket.io.js',
10 years ago
'public/vendor/jquery/timeago/jquery.timeago.js',
'public/vendor/jquery/js/jquery.form.min.js',
'public/vendor/visibility/visibility.min.js',
'public/vendor/bootstrap/js/bootstrap.js',
'public/vendor/jquery/bootstrap-tagsinput/bootstrap-tagsinput.min.js',
9 years ago
'public/vendor/jquery/textcomplete/jquery.textcomplete.js',
'public/vendor/requirejs/require.js',
9 years ago
'public/src/require-config.js',
'public/vendor/bootbox/bootbox.js',
'public/vendor/bootbox/wrapper.js',
'public/vendor/tinycon/tinycon.js',
'public/vendor/xregexp/xregexp.js',
'public/vendor/xregexp/unicode/unicode-base.js',
'./node_modules/templates.js/lib/templates.js',
'public/src/utils.js',
'public/src/sockets.js',
'public/src/app.js',
'public/src/ajaxify.js',
10 years ago
'public/src/overrides.js',
'public/src/widgets.js',
8 years ago
"./node_modules/promise-polyfill/promise.js"
],
// files listed below are only available client-side, or are bundled in to reduce # of network requests on cold load
rjs: [
'public/src/client/footer.js',
'public/src/client/chats.js',
'public/src/client/infinitescroll.js',
'public/src/client/pagination.js',
'public/src/client/recent.js',
'public/src/client/unread.js',
'public/src/client/topic.js',
'public/src/client/topic/events.js',
'public/src/client/topic/flag.js',
'public/src/client/topic/fork.js',
'public/src/client/topic/move.js',
'public/src/client/topic/posts.js',
'public/src/client/topic/postTools.js',
'public/src/client/topic/threadTools.js',
'public/src/client/categories.js',
'public/src/client/category.js',
'public/src/client/category/tools.js',
'public/src/modules/translator.js',
'public/src/modules/notifications.js',
'public/src/modules/chat.js',
'public/src/modules/components.js',
'public/src/modules/sort.js',
'public/src/modules/navigator.js',
'public/src/modules/topicSelect.js',
'public/src/modules/share.js',
'public/src/modules/search.js',
'public/src/modules/alerts.js',
'public/src/modules/taskbar.js',
'public/src/modules/helpers.js',
'public/src/modules/sounds.js',
'public/src/modules/string.js'
],
// modules listed below are routed through express (/src/modules) so they can be defined anonymously
9 years ago
modules: {
"Chart.js": './node_modules/chart.js/dist/Chart.min.js',
"mousetrap.js": './node_modules/mousetrap/mousetrap.min.js',
"jqueryui.js": 'public/vendor/jquery/js/jquery-ui.js',
9 years ago
"buzz.js": 'public/vendor/buzz/buzz.js'
}
}
11 years ago
};
Meta.js.linkModules = function (callback) {
function link(filePath, destPath, cb) {
if (process.platform === 'win32') {
fs.link(filePath, destPath, cb);
} else {
fs.symlink(filePath, destPath, 'file', cb);
}
}
plugins.reload(function (err) {
if (err) {
return callback(err);
}
async.each(Object.keys(Meta.js.scripts.modules), function (relPath, next) {
var filePath = path.join(__dirname, '../../', Meta.js.scripts.modules[relPath]);
var destPath = path.join(__dirname, '../../build/public/src/modules', relPath);
mkdirp(path.dirname(destPath), function (err) {
if (err) {
return next(err);
}
link(filePath, destPath, next);
});
}, callback);
});
};
Meta.js.minify = function (target, callback) {
winston.verbose('[meta/js] Minifying ' + target);
var forkProcessParams = setupDebugging();
var minifier = Meta.js.minifierProc = fork('minifier.js', [], forkProcessParams);
9 years ago
Meta.js.target[target] = {};
Meta.js.prepare(target, function (err) {
if (err) {
return callback(err);
}
minifier.send({
action: 'js',
minify: global.env !== 'development',
9 years ago
scripts: Meta.js.target[target].scripts
});
});
minifier.on('message', function (message) {
switch(message.type) {
case 'end':
9 years ago
Meta.js.target[target].cache = message.minified;
Meta.js.target[target].map = message.sourceMap;
winston.verbose('[meta/js] ' + target + ' minification complete');
minifier.kill();
9 years ago
if (nconf.get('local-assets') === undefined || nconf.get('local-assets') !== false) {
return Meta.js.commitToFile(target, callback);
9 years ago
} else {
return callback();
9 years ago
}
break;
case 'error':
9 years ago
winston.error('[meta/js] Could not compile ' + target + ': ' + message.message);
minifier.kill();
callback(new Error(message.message));
break;
}
});
};
Meta.js.prepare = function (target, callback) {
9 years ago
var pluginsScripts = [];
var pluginDirectories = [];
11 years ago
pluginsScripts = plugins[target === 'nodebb.min.js' ? 'clientScripts' : 'acpScripts'].filter(function (path) {
if (path.endsWith('.js')) {
return true;
}
pluginDirectories.push(path);
return false;
});
async.each(pluginDirectories, function (directory, next) {
utils.walk(directory, function (err, scripts) {
pluginsScripts = pluginsScripts.concat(scripts);
next(err);
});
}, function (err) {
if (err) {
return callback(err);
}
11 years ago
var basePath = path.resolve(__dirname, '../..');
9 years ago
9 years ago
Meta.js.target[target].scripts = Meta.js.scripts.base.concat(pluginsScripts);
if (target === 'nodebb.min.js') {
Meta.js.target[target].scripts = Meta.js.target[target].scripts.concat(Meta.js.scripts.rjs);
9 years ago
}
Meta.js.target[target].scripts = Meta.js.target[target].scripts.map(function (script) {
return path.relative(basePath, script).replace(/\\/g, '/');
11 years ago
});
9 years ago
callback();
11 years ago
});
};
Meta.js.killMinifier = function () {
11 years ago
if (Meta.js.minifierProc) {
Meta.js.minifierProc.kill('SIGTERM');
}
};
Meta.js.commitToFile = function (target, callback) {
fs.writeFile(path.join(__dirname, '../../build/public/' + target), Meta.js.target[target].cache, function (err) {
callback(err);
});
};
function setupDebugging() {
/**
* Check if the parent process is running with the debug option --debug (or --debug-brk)
*/
var forkProcessParams = {};
if(global.v8debug || parseInt(process.execArgv.indexOf('--debug'), 10) !== -1) {
/**
* use the line below if you want to debug minifier.js script too (or even --debug-brk option, but
* you'll have to setup your debugger and connect to the forked process)
*/
//forkProcessParams = {execArgv: ['--debug=' + (global.process.debugPort + 1), '--nolazy']};
/**
* otherwise, just clean up --debug/--debug-brk options which are set up by default from the parent one
*/
forkProcessParams = {execArgv: []};
}
return forkProcessParams;
}
};