mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
refactor: meta/minifier use async/await
This commit is contained in:
@@ -73,36 +73,37 @@ function removeChild(proc) {
|
||||
}
|
||||
}
|
||||
|
||||
function forkAction(action, callback) {
|
||||
function forkAction(action) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const proc = getChild();
|
||||
|
||||
proc.on('message', (message) => {
|
||||
freeChild(proc);
|
||||
|
||||
if (message.type === 'error') {
|
||||
return callback(message.message);
|
||||
return reject(new Error(message.message));
|
||||
}
|
||||
|
||||
if (message.type === 'end') {
|
||||
callback(null, message.result);
|
||||
resolve(message.result);
|
||||
}
|
||||
});
|
||||
proc.on('error', (err) => {
|
||||
proc.kill();
|
||||
removeChild(proc);
|
||||
callback(err);
|
||||
reject(err);
|
||||
});
|
||||
|
||||
proc.send({
|
||||
type: 'action',
|
||||
action: action,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const actions = {};
|
||||
|
||||
if (process.env.minifier_child) {
|
||||
process.on('message', (message) => {
|
||||
process.on('message', async (message) => {
|
||||
if (message.type === 'action') {
|
||||
const { action } = message;
|
||||
if (typeof actions[action.act] !== 'function') {
|
||||
@@ -112,69 +113,43 @@ if (process.env.minifier_child) {
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
actions[action.act](action, (err, result) => {
|
||||
if (err) {
|
||||
process.send({
|
||||
type: 'error',
|
||||
message: err.stack || err.message || 'unknown error',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await actions[action.act](action);
|
||||
process.send({
|
||||
type: 'end',
|
||||
result: result,
|
||||
});
|
||||
} catch (err) {
|
||||
process.send({
|
||||
type: 'error',
|
||||
message: err.stack || err.message || 'unknown error',
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function executeAction(action, fork, callback) {
|
||||
async function executeAction(action, fork) {
|
||||
if (fork && (pool.length - free.length) < Minifier.maxThreads) {
|
||||
forkAction(action, callback);
|
||||
} else {
|
||||
return await forkAction(action);
|
||||
}
|
||||
if (typeof actions[action.act] !== 'function') {
|
||||
return callback(Error('Unknown action'));
|
||||
}
|
||||
actions[action.act](action, callback);
|
||||
throw new Error('Unknown action');
|
||||
}
|
||||
return await actions[action.act](action);
|
||||
}
|
||||
|
||||
function concat(data, callback) {
|
||||
actions.concat = async function concat(data) {
|
||||
if (data.files && data.files.length) {
|
||||
async.mapLimit(data.files, 1000, (ref, next) => {
|
||||
fs.readFile(ref.srcPath, 'utf8', (err, file) => {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
next(null, file);
|
||||
});
|
||||
}, (err, files) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
const files = await async.mapLimit(data.files, 1000, async ref => await fs.promises.readFile(ref.srcPath, 'utf8'));
|
||||
const output = files.join('\n;');
|
||||
fs.writeFile(data.destPath, output, callback);
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
callback();
|
||||
}
|
||||
actions.concat = concat;
|
||||
|
||||
function minifyJS_batch(data, callback) {
|
||||
async.eachLimit(data.files, 100, (fileObj, next) => {
|
||||
fs.readFile(fileObj.srcPath, 'utf8', (err, source) => {
|
||||
if (err) {
|
||||
return next(err);
|
||||
await fs.promises.writeFile(data.destPath, output);
|
||||
}
|
||||
};
|
||||
|
||||
actions.minifyJS_batch = async function minifyJS_batch(data) {
|
||||
await async.eachLimit(data.files, 100, async (fileObj) => {
|
||||
const source = await fs.promises.readFile(fileObj.srcPath, 'utf8');
|
||||
const filesToMinify = [
|
||||
{
|
||||
srcPath: fileObj.srcPath,
|
||||
@@ -182,51 +157,37 @@ function minifyJS_batch(data, callback) {
|
||||
source: source,
|
||||
},
|
||||
];
|
||||
minifyAndSave({
|
||||
|
||||
await minifyAndSave({
|
||||
files: filesToMinify,
|
||||
destPath: fileObj.destPath,
|
||||
filename: fileObj.filename,
|
||||
}, next);
|
||||
});
|
||||
}, callback);
|
||||
}
|
||||
actions.minifyJS_batch = minifyJS_batch;
|
||||
});
|
||||
};
|
||||
|
||||
function minifyJS(data, callback) {
|
||||
async.mapLimit(data.files, 1000, (fileObj, next) => {
|
||||
fs.readFile(fileObj.srcPath, 'utf8', (err, source) => {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
next(null, {
|
||||
actions.minifyJS = async function minifyJS(data) {
|
||||
const filesToMinify = await async.mapLimit(data.files, 1000, async (fileObj) => {
|
||||
const source = await fs.promises.readFile(fileObj.srcPath, 'utf8');
|
||||
return {
|
||||
srcPath: fileObj.srcPath,
|
||||
filename: fileObj.filename,
|
||||
source: source,
|
||||
};
|
||||
});
|
||||
});
|
||||
}, (err, filesToMinify) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
minifyAndSave({
|
||||
await minifyAndSave({
|
||||
files: filesToMinify,
|
||||
destPath: data.destPath,
|
||||
filename: data.filename,
|
||||
}, callback);
|
||||
});
|
||||
}
|
||||
actions.minifyJS = minifyJS;
|
||||
};
|
||||
|
||||
function minifyAndSave(data, callback) {
|
||||
async function minifyAndSave(data) {
|
||||
const scripts = {};
|
||||
data.files.forEach((ref) => {
|
||||
if (!ref) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ref && ref.filename && ref.source) {
|
||||
scripts[ref.filename] = ref.source;
|
||||
}
|
||||
});
|
||||
|
||||
const minified = uglify.minify(scripts, {
|
||||
@@ -239,66 +200,57 @@ function minifyAndSave(data, callback) {
|
||||
});
|
||||
|
||||
if (minified.error) {
|
||||
return callback({ stack: `Error minifying ${minified.error.filename}\n${minified.error.stack}` });
|
||||
throw new Error(`Error minifying ${minified.error.filename}\n${minified.error.stack}`);
|
||||
}
|
||||
|
||||
async.parallel([
|
||||
async.apply(fs.writeFile, data.destPath, minified.code),
|
||||
async.apply(fs.writeFile, `${data.destPath}.map`, minified.map),
|
||||
], callback);
|
||||
await Promise.all([
|
||||
fs.promises.writeFile(data.destPath, minified.code),
|
||||
fs.promises.writeFile(`${data.destPath}.map`, minified.map),
|
||||
]);
|
||||
}
|
||||
|
||||
Minifier.js = {};
|
||||
Minifier.js.bundle = function (data, minify, fork, callback) {
|
||||
executeAction({
|
||||
Minifier.js.bundle = async function (data, minify, fork) {
|
||||
return await executeAction({
|
||||
act: minify ? 'minifyJS' : 'concat',
|
||||
files: data.files,
|
||||
filename: data.filename,
|
||||
destPath: data.destPath,
|
||||
}, fork, callback);
|
||||
}, fork);
|
||||
};
|
||||
|
||||
Minifier.js.minifyBatch = function (scripts, fork, callback) {
|
||||
executeAction({
|
||||
Minifier.js.minifyBatch = async function (scripts, fork) {
|
||||
return await executeAction({
|
||||
act: 'minifyJS_batch',
|
||||
files: scripts,
|
||||
}, fork, callback);
|
||||
}, fork);
|
||||
};
|
||||
|
||||
function buildCSS(data, callback) {
|
||||
less.render(data.source, {
|
||||
actions.buildCSS = async function buildCSS(data) {
|
||||
const lessOutput = await less.render(data.source, {
|
||||
paths: data.paths,
|
||||
javascriptEnabled: true,
|
||||
}, (err, lessOutput) => {
|
||||
if (err) {
|
||||
// display less parser errors properly
|
||||
return callback(new Error(String(err)));
|
||||
}
|
||||
});
|
||||
|
||||
postcss(data.minify ? [
|
||||
autoprefixer,
|
||||
clean({
|
||||
const postcssArgs = [autoprefixer];
|
||||
if (data.minify) {
|
||||
postcssArgs.push(clean({
|
||||
processImportFrom: ['local'],
|
||||
}),
|
||||
] : [autoprefixer]).process(lessOutput.css, {
|
||||
}));
|
||||
}
|
||||
const result = await postcss(postcssArgs).process(lessOutput.css, {
|
||||
from: undefined,
|
||||
}).then((result) => {
|
||||
process.nextTick(callback, null, { code: result.css });
|
||||
}).catch((err) => {
|
||||
process.nextTick(callback, err);
|
||||
});
|
||||
});
|
||||
}
|
||||
actions.buildCSS = buildCSS;
|
||||
return { code: result.css };
|
||||
};
|
||||
|
||||
Minifier.css = {};
|
||||
Minifier.css.bundle = function (source, paths, minify, fork, callback) {
|
||||
executeAction({
|
||||
Minifier.css.bundle = async function (source, paths, minify, fork) {
|
||||
return await executeAction({
|
||||
act: 'buildCSS',
|
||||
source: source,
|
||||
paths: paths,
|
||||
minify: minify,
|
||||
}, fork, callback);
|
||||
}, fork);
|
||||
};
|
||||
|
||||
require('../promisify')(exports);
|
||||
|
||||
Reference in New Issue
Block a user