2022-10-04 17:46:12 -04:00
|
|
|
import loconfig from '../helpers/config.js';
|
|
|
|
|
import message from '../helpers/message.js';
|
|
|
|
|
import notification from '../helpers/notification.js';
|
|
|
|
|
import resolve from '../helpers/template.js';
|
|
|
|
|
import { merge } from '../utils/index.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-21 17:33:32 -04:00
|
|
|
/**
|
|
|
|
|
* @const {object} defaultESBuildOptions - The default shared ESBuild options.
|
|
|
|
|
* @const {object} developmentESBuildOptions - The predefined ESBuild options for development.
|
|
|
|
|
* @const {object} productionESBuildOptions - The predefined ESBuild options for production.
|
|
|
|
|
*/
|
|
|
|
|
export const defaultESBuildOptions = {
|
|
|
|
|
bundle: true,
|
|
|
|
|
color: true,
|
|
|
|
|
sourcemap: true,
|
|
|
|
|
target: [
|
|
|
|
|
'es2015',
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
export const developmentESBuildOptions = Object.assign({}, defaultESBuildOptions);
|
|
|
|
|
export const productionESBuildOptions = Object.assign({}, defaultESBuildOptions, {
|
|
|
|
|
logLevel: 'warning',
|
|
|
|
|
minify: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @const {object} developmentScriptsArgs - The predefined `compileScripts()` options for development.
|
|
|
|
|
* @const {object} productionScriptsArgs - The predefined `compileScripts()` options for production.
|
|
|
|
|
*/
|
|
|
|
|
export const developmentScriptsArgs = [
|
|
|
|
|
developmentESBuildOptions,
|
|
|
|
|
];
|
|
|
|
|
export const productionScriptsArgs = [
|
|
|
|
|
productionESBuildOptions,
|
|
|
|
|
];
|
|
|
|
|
|
2021-09-16 14:22:20 -04:00
|
|
|
/**
|
|
|
|
|
* Bundles and minifies main JavaScript files.
|
2021-09-21 16:01:47 -04:00
|
|
|
*
|
|
|
|
|
* @async
|
2021-09-21 17:33:32 -04:00
|
|
|
* @param {object} [esBuildOptions=null] - Customize the ESBuild build API options.
|
|
|
|
|
* If `null`, default production options are used.
|
2021-09-21 16:01:47 -04:00
|
|
|
* @return {Promise}
|
2021-09-16 14:22:20 -04:00
|
|
|
*/
|
2021-09-21 17:33:32 -04:00
|
|
|
export default async function compileScripts(esBuildOptions = null) {
|
|
|
|
|
if (esBuildOptions == null) {
|
|
|
|
|
esBuildOptions = productionESBuildOptions;
|
|
|
|
|
} else if (
|
|
|
|
|
esBuildOptions !== developmentESBuildOptions &&
|
|
|
|
|
esBuildOptions !== productionESBuildOptions
|
|
|
|
|
) {
|
2022-10-04 17:46:12 -04:00
|
|
|
esBuildOptions = merge({}, defaultESBuildOptions, esBuildOptions);
|
2021-09-21 17:33:32 -04:00
|
|
|
}
|
|
|
|
|
|
2022-03-29 18:47:53 -04:00
|
|
|
/**
|
|
|
|
|
* @async
|
|
|
|
|
* @param {object} entry - The entrypoint to process.
|
|
|
|
|
* @param {string[]} entry.includes - One or more paths to process.
|
|
|
|
|
* @param {string} [entry.outdir] - The directory to write to.
|
|
|
|
|
* @param {string} [entry.outfile] - The file to write to.
|
|
|
|
|
* @param {?string} [entry.label] - The task label.
|
|
|
|
|
* Defaults to the outdir or outfile name.
|
|
|
|
|
* @throws {TypeError} If outdir and outfile are missing.
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
2023-08-08 14:49:47 -04:00
|
|
|
loconfig.tasks.scripts?.forEach(async ({
|
2021-09-17 00:26:18 -04:00
|
|
|
includes,
|
|
|
|
|
outdir = '',
|
2021-12-03 13:22:59 -05:00
|
|
|
outfile = '',
|
|
|
|
|
label = null
|
2021-09-17 00:26:18 -04:00
|
|
|
}) => {
|
2021-12-03 13:22:59 -05:00
|
|
|
if (!label) {
|
|
|
|
|
label = basename(outdir || outfile || 'undefined');
|
|
|
|
|
}
|
2020-11-27 16:01:54 -05:00
|
|
|
|
2021-12-03 13:22:59 -05:00
|
|
|
const timeLabel = `${label} compiled in`;
|
2021-09-16 14:22:20 -04:00
|
|
|
console.time(timeLabel);
|
2020-12-11 16:00:36 -05:00
|
|
|
|
2021-09-17 00:26:18 -04:00
|
|
|
try {
|
2022-03-29 18:46:24 -04:00
|
|
|
if (!Array.isArray(includes)) {
|
|
|
|
|
includes = [ includes ];
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 16:08:29 -04:00
|
|
|
includes = resolve(includes);
|
2021-09-17 00:26:18 -04:00
|
|
|
|
|
|
|
|
if (outdir) {
|
2022-03-22 16:08:29 -04:00
|
|
|
outdir = resolve(outdir);
|
2021-09-17 00:26:18 -04:00
|
|
|
} else if (outfile) {
|
2022-03-22 16:08:29 -04:00
|
|
|
outfile = resolve(outfile);
|
2021-09-17 00:26:18 -04:00
|
|
|
} else {
|
|
|
|
|
throw new TypeError(
|
|
|
|
|
'Expected \'outdir\' or \'outfile\''
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 17:33:32 -04:00
|
|
|
await esbuild.build(Object.assign({}, esBuildOptions, {
|
2021-09-17 00:26:18 -04:00
|
|
|
entryPoints: includes,
|
|
|
|
|
outdir,
|
2021-09-21 17:33:32 -04:00
|
|
|
outfile,
|
|
|
|
|
}));
|
2021-09-17 00:26:18 -04:00
|
|
|
|
2021-12-03 13:22:59 -05:00
|
|
|
message(`${label} compiled`, 'success', timeLabel);
|
2021-09-17 00:26:18 -04:00
|
|
|
} catch (err) {
|
2021-09-16 14:22:20 -04:00
|
|
|
// errors managments (already done in esbuild)
|
|
|
|
|
notification({
|
2021-12-03 13:22:59 -05:00
|
|
|
title: `${label} compilation failed 🚨`,
|
2021-09-16 14:22:20 -04:00
|
|
|
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
|
|
|
};
|