human/server/build.js

179 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-11-17 05:58:06 +01:00
#!/usr/bin/env -S node --trace-warnings
2020-11-17 18:38:48 +01:00
const fs = require('fs');
2020-11-17 05:58:06 +01:00
const esbuild = require('esbuild');
const log = require('@vladmandic/pilogger');
// keeps esbuild service instance cached
let es;
2020-11-20 14:53:40 +01:00
const banner = `
/*
Human library
homepage: <https://github.com/vladmandic/human>
author: <https://github.com/vladmandic>'
*/
`;
2020-11-17 05:58:06 +01:00
// common configuration
const common = {
2020-11-20 14:53:40 +01:00
banner,
2020-11-17 05:58:06 +01:00
minifyWhitespace: true,
2021-01-12 15:55:08 +01:00
minifyIdentifiers: true,
minifySyntax: true,
2020-11-17 05:58:06 +01:00
bundle: true,
sourcemap: true,
logLevel: 'error',
target: 'es2018',
2020-11-20 14:53:40 +01:00
tsconfig: 'server/tfjs-tsconfig.json',
2020-11-17 05:58:06 +01:00
};
const targets = {
node: {
tfjs: {
platform: 'node',
format: 'cjs',
metafile: 'dist/tfjs.esm.json',
entryPoints: ['src/tfjs/tf-node.js'],
outfile: 'dist/tfjs.esm.js',
external: ['@tensorflow'],
},
node: {
platform: 'node',
format: 'cjs',
metafile: 'dist/human.node.json',
entryPoints: ['src/human.js'],
outfile: 'dist/human.node.js',
external: ['@tensorflow'],
},
2020-11-17 05:58:06 +01:00
},
nodeGPU: {
tfjs: {
platform: 'node',
format: 'cjs',
metafile: 'dist/tfjs.esm.json',
entryPoints: ['src/tfjs/tf-node-gpu.js'],
outfile: 'dist/tfjs.esm.js',
external: ['@tensorflow'],
},
node: {
platform: 'node',
format: 'cjs',
metafile: 'dist/human.node.json',
entryPoints: ['src/human.js'],
outfile: 'dist/human.node-gpu.js',
external: ['@tensorflow'],
},
2020-11-17 05:58:06 +01:00
},
browserNoBundle: {
tfjs: {
platform: 'browser',
format: 'esm',
metafile: 'dist/tfjs.esm.json',
entryPoints: ['src/tfjs/tf-browser.js'],
outfile: 'dist/tfjs.esm.js',
external: ['fs', 'buffer', 'util', '@tensorflow'],
},
esm: {
platform: 'browser',
format: 'esm',
metafile: 'dist/human.esm.json',
entryPoints: ['src/human.js'],
outfile: 'dist/human.esm-nobundle.js',
external: ['fs', 'buffer', 'util', '@tensorflow'],
},
2020-11-17 05:58:06 +01:00
},
browserBundle: {
tfjs: {
platform: 'browser',
format: 'esm',
metafile: 'dist/tfjs.esm.json',
entryPoints: ['src/tfjs/tf-browser.js'],
outfile: 'dist/tfjs.esm.js',
external: ['fs', 'buffer', 'util'],
},
iife: {
platform: 'browser',
format: 'iife',
globalName: 'Human',
metafile: 'dist/human.json',
entryPoints: ['src/human.js'],
outfile: 'dist/human.js',
external: ['fs', 'buffer', 'util'],
},
esm: {
platform: 'browser',
format: 'esm',
metafile: 'dist/human.esm.json',
entryPoints: ['src/human.js'],
outfile: 'dist/human.esm.js',
external: ['fs', 'buffer', 'util'],
},
demo: {
platform: 'browser',
format: 'esm',
metafile: 'dist/demo-browser-index.json',
entryPoints: ['demo/browser.js'],
outfile: 'dist/demo-browser-index.js',
external: ['fs', 'buffer', 'util'],
},
2020-11-17 05:58:06 +01:00
},
};
2020-11-17 18:38:48 +01:00
async function getStats(metafile) {
const stats = {};
if (!fs.existsSync(metafile)) return stats;
const data = fs.readFileSync(metafile);
2021-01-03 16:23:45 +01:00
const json = JSON.parse(data.toString());
2020-11-17 18:38:48 +01:00
if (json && json.inputs && json.outputs) {
for (const [key, val] of Object.entries(json.inputs)) {
if (key.startsWith('node_modules')) {
stats.modules = (stats.modules || 0) + 1;
stats.moduleBytes = (stats.moduleBytes || 0) + val.bytes;
} else {
stats.imports = (stats.imports || 0) + 1;
stats.importBytes = (stats.importBytes || 0) + val.bytes;
}
}
const files = [];
for (const [key, val] of Object.entries(json.outputs)) {
if (!key.endsWith('.map')) {
files.push(key);
stats.outputBytes = (stats.outputBytes || 0) + val.bytes;
}
}
stats.outputFiles = files.join(', ');
}
return stats;
}
2020-11-17 05:58:06 +01:00
// rebuild on file change
async function build(f, msg) {
2020-11-17 18:38:48 +01:00
log.info('Build: file', msg, f, 'target:', common.target);
2020-11-17 05:58:06 +01:00
if (!es) es = await esbuild.startService();
// common build options
try {
// rebuild all target groups and types
for (const [targetGroupName, targetGroup] of Object.entries(targets)) {
for (const [targetName, targetOptions] of Object.entries(targetGroup)) {
// if triggered from watch mode, rebuild only browser bundle
2020-11-25 15:13:19 +01:00
if ((require.main !== module) && (targetGroupName !== 'browserBundle')) continue;
await es.build({ ...common, ...targetOptions });
2021-01-03 16:23:45 +01:00
const stats = await getStats(targetOptions.metafile);
log.state(`Build for: ${targetGroupName} type: ${targetName}:`, stats);
}
2020-11-17 05:58:06 +01:00
}
2020-11-25 15:13:19 +01:00
if (require.main === module) process.exit(0);
2020-11-17 05:58:06 +01:00
} catch (err) {
// catch errors and print where it occured
log.error('Build error', JSON.stringify(err.errors || err, null, 2));
2020-11-25 15:13:19 +01:00
if (require.main === module) process.exit(1);
2020-11-17 05:58:06 +01:00
}
}
2020-11-25 15:13:19 +01:00
if (require.main === module) {
2020-11-17 05:58:06 +01:00
log.header();
build('all', 'startup');
} else {
exports.build = build;
}