Decouple static "main" keys in mconfig.json and refactor tasks to support many output files. Added: - scripts.js: Array of output files (such as 'app.js') to iterate over to bundle JS files. - styles.js: Array of input files (such as 'main.css' and 'critical.css') to iterate over to compile Sass files. - svgs.js: Array of output files (such as 'sprite.svg') to iterate over to compile SVG spritesheets. Changed: - mconfig.json: Decouple entry points to individual tasks to allow for more flexibility in projects. - concat.js: Refactor function to use promises to build list of JS files to concatenate. - message.js: Replace if statements with switch for improved readability. - message.js: If timerID provided with "waiting" type, log time. - watch.js: Change CSS and JS reload watch paths to include all files. - Sorted imports by path.
58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
import paths from '../mconfig.json';
|
|
import notification from './notification.js';
|
|
import message from './utils/message.js';
|
|
import concat from 'concat';
|
|
import fs from 'fs';
|
|
|
|
/**
|
|
* Concatenates third-party JavaScript files.
|
|
*/
|
|
export function concatVendors() {
|
|
const filename = 'vendors.js';
|
|
const outfile = paths.scripts.dest + filename;
|
|
const external = [
|
|
// Add files in node_modules example:
|
|
// 'node_modules/gsap/dist/gsap.min.js',
|
|
];
|
|
|
|
const timeLabel = `${filename} concatenated in`;
|
|
console.time(timeLabel);
|
|
|
|
// Get all files in `scripts/vendors/`
|
|
fs.readdir(paths.scripts.vendors.src, (err, files) => {
|
|
if (err) {
|
|
message(`Error preparing ${filename}`, 'error');
|
|
message(err);
|
|
|
|
notification({
|
|
title: `${filename} concatenation failed 🚨`,
|
|
message: `${err.name}: ${e.message}`
|
|
});
|
|
}
|
|
|
|
if (files.length) {
|
|
// Exclude files that are not JavaScript
|
|
files = files.filter((file) => file.includes('.js'));
|
|
|
|
// Prepend absolute path
|
|
files = files.map((file) => paths.scripts.vendors.src + file);
|
|
}
|
|
|
|
if (external.length) {
|
|
files = files.concat(external);
|
|
}
|
|
|
|
concat(files, outfile).then(() => {
|
|
message(`${filename} concatenated'`, 'success', timeLabel);
|
|
}).catch((err) => {
|
|
message(`Error concatenating ${filename}`, 'error');
|
|
message(err);
|
|
|
|
notification({
|
|
title: `${filename} concatenation failed 🚨`,
|
|
message: `${err.name}: ${err.message}`
|
|
});
|
|
});
|
|
});
|
|
}
|