eeprom/build.js

103 lines
2.7 KiB
JavaScript

// IMPORTS
const fs = require('fs');
const swc = require('@swc/core');
const path = require('path');
const utils = require('./scripts/utils.js');
const assproc = require('./scripts/assproc.js');
// CONFIG
const rootDir = __dirname;
const srcDir = path.join(rootDir, 'scripts');
const pubDir = path.join(rootDir, 'public');
const pubAssetsDir = path.join(pubDir, 'scripts');
const isDebugBuild = fs.existsSync(path.join(rootDir, '.debug'));
const buildTasks = {
js: [
{ source: 'eepromv1.js', target: '/scripts', name: 'eepromv1.js', es: 'es5' },
{ source: 'eepromv1a.js', target: '/scripts', name: 'eepromv1a.js' },
],
};
// PREP
const swcJscOptions = {
target: 'es2021',
loose: false,
externalHelpers: false,
keepClassNames: true,
preserveAllComments: false,
transform: {},
parser: {
syntax: 'ecmascript',
jsx: false,
dynamicImport: false,
privateMethod: false,
functionBind: false,
exportDefaultFrom: false,
exportNamespaceFrom: false,
decorators: false,
decoratorsBeforeExport: false,
topLevelAwait: true,
importMeta: false,
},
};
// BUILD
(async () => {
const files = {};
console.log('Ensuring assets directory exists...');
fs.mkdirSync(pubAssetsDir, { recursive: true });
console.log();
console.log('JS assets');
for(const info of buildTasks.js) {
console.log(`=> Building ${info.source}...`);
let origTarget = undefined;
if('es' in info) {
origTarget = swcJscOptions.target;
swcJscOptions.target = info.es;
}
const assprocOpts = {
prefix: '#',
entry: info.entry || 'main.js',
};
const swcOpts = {
filename: info.source,
sourceMaps: false,
isModule: false,
minify: !isDebugBuild,
jsc: swcJscOptions,
};
const pubName = await assproc.process(path.join(srcDir, info.source), assprocOpts)
.then(output => swc.transform(output, swcOpts))
.then(output => {
const name = utils.strtr(info.name, { hash: utils.shortHash(output.code) });
const pubName = path.join(info.target || '', name);
console.log(` Saving to ${pubName}...`);
fs.writeFileSync(path.join(pubDir, pubName), output.code);
return pubName;
});
if(origTarget !== undefined)
swcJscOptions.target = origTarget;
files[info.source] = pubName;
}
console.log();
console.log('Cleaning up old builds...');
assproc.housekeep(pubAssetsDir);
})();