2021-09-17 00:26:18 -04:00
|
|
|
import loconfig from '../../loconfig.json';
|
2021-09-17 00:04:56 -04:00
|
|
|
import message from '../utils/message.js';
|
|
|
|
|
import notification from '../utils/notification.js';
|
2021-09-17 00:26:18 -04:00
|
|
|
import template from '../utils/template.js';
|
2021-09-16 14:22:20 -04:00
|
|
|
import esbuild from 'esbuild';
|
2021-09-17 00:26:18 -04:00
|
|
|
import { basename } from 'node:path';
|
2020-11-27 16:01:54 -05:00
|
|
|
|
2021-09-16 14:22:20 -04:00
|
|
|
/**
|
|
|
|
|
* Bundles and minifies main JavaScript files.
|
|
|
|
|
*/
|
2021-09-15 17:22:39 -04:00
|
|
|
export async function compileScripts() {
|
2021-09-17 00:26:18 -04:00
|
|
|
loconfig.tasks.scripts.forEach(async ({
|
|
|
|
|
includes,
|
|
|
|
|
outdir = '',
|
|
|
|
|
outfile = ''
|
|
|
|
|
}) => {
|
|
|
|
|
const filename = basename(outdir || outfile || 'undefined');
|
2020-11-27 16:01:54 -05:00
|
|
|
|
2021-09-16 14:22:20 -04:00
|
|
|
const timeLabel = `${filename} compiled in`;
|
|
|
|
|
console.time(timeLabel);
|
2020-12-11 16:00:36 -05:00
|
|
|
|
2021-09-17 00:26:18 -04:00
|
|
|
try {
|
|
|
|
|
includes = includes.map((path) => template(path));
|
|
|
|
|
|
|
|
|
|
if (outdir) {
|
|
|
|
|
outdir = template(outdir);
|
|
|
|
|
} else if (outfile) {
|
|
|
|
|
outfile = template(outfile);
|
|
|
|
|
} else {
|
|
|
|
|
throw new TypeError(
|
|
|
|
|
'Expected \'outdir\' or \'outfile\''
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await esbuild.build({
|
|
|
|
|
entryPoints: includes,
|
|
|
|
|
bundle: true,
|
|
|
|
|
minify: true,
|
|
|
|
|
sourcemap: true,
|
|
|
|
|
color: true,
|
|
|
|
|
logLevel: 'error',
|
|
|
|
|
target: [
|
|
|
|
|
'es2015',
|
|
|
|
|
],
|
|
|
|
|
outdir,
|
|
|
|
|
outfile
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
message(`${filename} compiled`, 'success', timeLabel);
|
|
|
|
|
} catch (err) {
|
2021-09-16 14:22:20 -04:00
|
|
|
// errors managments (already done in esbuild)
|
|
|
|
|
notification({
|
|
|
|
|
title: `${filename} compilation failed 🚨`,
|
|
|
|
|
message: `${err.errors[0].text} in ${err.errors[0].location.file} line ${err.errors[0].location.line}`
|
|
|
|
|
});
|
2021-09-17 00:26:18 -04:00
|
|
|
}
|
2021-09-16 14:22:20 -04:00
|
|
|
});
|
2021-09-15 17:24:33 -04:00
|
|
|
};
|