mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-06-23 12:50:05 +02:00
closed #1910
This commit is contained in:
40
minifier.js
40
minifier.js
@@ -34,43 +34,19 @@ Minifier.js.minify = function (scripts, minify, callback) {
|
||||
map: minified.map
|
||||
});
|
||||
} catch(err) {
|
||||
process.send({
|
||||
action: 'error',
|
||||
error: {
|
||||
message: err.message
|
||||
}
|
||||
});
|
||||
process.send(err.message);
|
||||
}
|
||||
};
|
||||
|
||||
// Minifier.js.concatenate = function(scripts, callback) {
|
||||
// async.map(scripts, function(path, next) {
|
||||
// fs.readFile(path, { encoding: 'utf-8' }, next);
|
||||
// }, function(err, contents) {
|
||||
// if (err) {
|
||||
// process.send({
|
||||
// action: 'error',
|
||||
// error: err
|
||||
// });
|
||||
// } else {
|
||||
// callback(contents.reduce(function(output, src) {
|
||||
// return output.length ? output + ';\n' + src : src;
|
||||
// }, ''));
|
||||
// }
|
||||
// });
|
||||
// };
|
||||
|
||||
process.on('message', function(payload) {
|
||||
var executeCallback = function(data) {
|
||||
process.send({
|
||||
action: payload.action,
|
||||
data: data
|
||||
});
|
||||
};
|
||||
|
||||
switch(payload.action) {
|
||||
case 'js':
|
||||
Minifier.js.minify(payload.scripts, payload.minify, executeCallback);
|
||||
Minifier.js.minify(payload.scripts, payload.minify, function(data) {
|
||||
process.stdout.write(data.js);
|
||||
process.send('end.script');
|
||||
process.stderr.write(data.map);
|
||||
process.send('end.mapping');
|
||||
});
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -14,7 +14,8 @@ var winston = require('winston'),
|
||||
module.exports = function(Meta) {
|
||||
|
||||
Meta.js = {
|
||||
cache: undefined,
|
||||
cache: '',
|
||||
map: '',
|
||||
prepared: false,
|
||||
minFile: 'nodebb.min.js',
|
||||
scripts: [
|
||||
@@ -136,20 +137,45 @@ module.exports = function(Meta) {
|
||||
};
|
||||
|
||||
Meta.js.minify = function(minify) {
|
||||
var minifier = Meta.js.minifierProc = fork('minifier.js');
|
||||
var minifier = Meta.js.minifierProc = fork('minifier.js', {
|
||||
silent: true
|
||||
}),
|
||||
minifiedStream = minifier.stdio[1],
|
||||
mapStream = minifier.stdio[2],
|
||||
step = 0,
|
||||
onComplete = function() {
|
||||
if (step === 0) {
|
||||
return step++;
|
||||
}
|
||||
|
||||
winston.info('[meta/js] Compilation complete');
|
||||
emitter.emit('meta:js.compiled');
|
||||
minifier.kill();
|
||||
};
|
||||
|
||||
minifiedStream.on('data', function(buffer) {
|
||||
Meta.js.cache += buffer.toString();
|
||||
});
|
||||
mapStream.on('data', function(buffer) {
|
||||
Meta.js.map += buffer.toString();
|
||||
});
|
||||
|
||||
minifier.on('message', function(payload) {
|
||||
if (payload.action !== 'error') {
|
||||
winston.info('[meta/js] Compilation complete');
|
||||
Meta.js.cache = payload.data.js;
|
||||
Meta.js.map = payload.data.map;
|
||||
minifier.kill();
|
||||
switch(payload) {
|
||||
case 'end.script':
|
||||
winston.info('[meta/js] Successfully minified.');
|
||||
onComplete();
|
||||
break;
|
||||
case 'end.mapping':
|
||||
winston.info('[meta/js] Retrieved Mapping.');
|
||||
onComplete();
|
||||
break;
|
||||
|
||||
emitter.emit('meta:js.compiled');
|
||||
} else {
|
||||
winston.error('[meta/js] Could not compile client-side scripts! ' + payload.error.message);
|
||||
default:
|
||||
winston.error('[meta/js] Could not compile client-side scripts! ' + payload);
|
||||
minifier.kill();
|
||||
process.exit();
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -169,11 +195,4 @@ module.exports = function(Meta) {
|
||||
Meta.js.minifierProc.kill('SIGTERM');
|
||||
}
|
||||
};
|
||||
|
||||
// OS detection and handling
|
||||
// if (os.platform() === 'win32') {
|
||||
// Meta.js.scripts = Meta.js.scripts.map(function(script) {
|
||||
// return script.replace(/\//g, '\\');
|
||||
// });
|
||||
// }
|
||||
};
|
||||
Reference in New Issue
Block a user