human/server/build.js

255 lines
7.1 KiB
JavaScript
Raw Normal View History

2020-11-17 05:58:06 +01:00
#!/usr/bin/env -S node --trace-warnings
2021-03-14 04:31:09 +01:00
const ts = require('typescript');
2020-11-17 05:58:06 +01:00
const log = require('@vladmandic/pilogger');
2021-02-08 18:47:38 +01:00
const esbuild = require('esbuild');
2021-03-14 04:31:09 +01:00
const TypeDoc = require('typedoc');
const changelog = require('./changelog');
2020-11-17 05:58:06 +01:00
2021-02-08 17:39:09 +01:00
let busy = false;
2021-03-14 04:31:09 +01:00
let td = null;
2021-03-10 00:32:35 +01:00
const banner = { js: `
2020-11-20 14:53:40 +01:00
/*
Human library
homepage: <https://github.com/vladmandic/human>
author: <https://github.com/vladmandic>'
*/
2021-03-10 00:32:35 +01:00
` };
2020-11-17 05:58:06 +01:00
2021-02-08 18:47:38 +01:00
// tsc configuration for building types only
const tsconfig = {
noEmitOnError: false,
target: ts.ScriptTarget.ES2018,
module: ts.ModuleKind.ES2020,
outDir: 'types/',
declaration: true,
emitDeclarationOnly: true,
emitDecoratorMetadata: true,
experimentalDecorators: true,
skipLibCheck: true,
importHelpers: true,
noImplicitAny: false,
preserveConstEnums: true,
strictNullChecks: true,
baseUrl: './',
typeRoots: ['node_modules/@types'],
paths: {
tslib: ['node_modules/tslib/tslib.d.ts'],
'@tensorflow/tfjs-node/dist/io/file_system': ['node_modules/@tensorflow/tfjs-node/dist/io/file_system.js'],
},
};
2020-11-17 05:58:06 +01:00
// common configuration
2021-02-21 13:20:58 +01:00
const config = {
common: {
banner,
tsconfig: 'server/tfjs-tsconfig.json',
logLevel: 'error',
},
debug: {
minifyWhitespace: false,
minifyIdentifiers: false,
minifySyntax: false,
sourcemap: true,
2021-03-10 16:30:34 +01:00
bundle: true,
metafile: true,
2021-02-21 13:20:58 +01:00
target: 'es2018',
},
production: {
minifyWhitespace: true,
minifyIdentifiers: true,
minifySyntax: true,
sourcemap: true,
2021-03-10 16:30:34 +01:00
bundle: true,
metafile: true,
2021-02-21 13:20:58 +01:00
target: 'es2018',
},
2020-11-17 05:58:06 +01:00
};
const targets = {
2021-02-21 19:34:26 +01:00
node: {
tfjs: {
platform: 'node',
format: 'cjs',
entryPoints: ['src/tfjs/tf-node.ts'],
outfile: 'dist/tfjs.esm.js',
external: ['@tensorflow'],
},
node: {
platform: 'node',
format: 'cjs',
entryPoints: ['src/human.ts'],
outfile: 'dist/human.node.js',
external: ['@tensorflow'],
},
},
nodeGPU: {
tfjs: {
platform: 'node',
format: 'cjs',
entryPoints: ['src/tfjs/tf-node-gpu.ts'],
outfile: 'dist/tfjs.esm.js',
external: ['@tensorflow'],
},
node: {
platform: 'node',
format: 'cjs',
entryPoints: ['src/human.ts'],
outfile: 'dist/human.node-gpu.js',
external: ['@tensorflow'],
},
},
browserNoBundle: {
tfjs: {
platform: 'browser',
format: 'esm',
2021-02-08 17:39:09 +01:00
entryPoints: ['src/tfjs/tf-browser.ts'],
outfile: 'dist/tfjs.esm.js',
2021-02-25 13:50:13 +01:00
external: ['fs', 'buffer', 'util', 'os', '@tensorflow'],
},
esm: {
platform: 'browser',
format: 'esm',
2021-02-08 17:39:09 +01:00
entryPoints: ['src/human.ts'],
outfile: 'dist/human.esm-nobundle.js',
2021-02-25 13:50:13 +01:00
external: ['fs', 'buffer', 'util', 'os', '@tensorflow'],
},
2020-11-17 05:58:06 +01:00
},
browserBundle: {
tfjs: {
platform: 'browser',
format: 'esm',
2021-02-08 17:39:09 +01:00
entryPoints: ['src/tfjs/tf-browser.ts'],
outfile: 'dist/tfjs.esm.js',
2021-02-25 13:50:13 +01:00
external: ['fs', 'buffer', 'util', 'os'],
},
iife: {
platform: 'browser',
format: 'iife',
globalName: 'Human',
2021-02-08 17:39:09 +01:00
entryPoints: ['src/human.ts'],
2021-03-10 00:32:35 +01:00
outfile: 'dist/human.js',
2021-02-25 13:50:13 +01:00
external: ['fs', 'buffer', 'util', 'os'],
},
esm: {
platform: 'browser',
format: 'esm',
2021-02-08 17:39:09 +01:00
entryPoints: ['src/human.ts'],
outfile: 'dist/human.esm.js',
2021-02-25 13:50:13 +01:00
external: ['fs', 'buffer', 'util', 'os'],
},
demo: {
platform: 'browser',
format: 'esm',
entryPoints: ['demo/browser.js'],
outfile: 'dist/demo-browser-index.js',
2021-02-25 13:50:13 +01:00
external: ['fs', 'buffer', 'util', 'os'],
},
2020-11-17 05:58:06 +01:00
},
};
2021-03-10 00:32:35 +01:00
async function getStats(json) {
2020-11-17 18:38:48 +01:00
const stats = {};
2021-03-10 00:32:35 +01:00
if (json && json.metafile?.inputs && json.metafile?.outputs) {
for (const [key, val] of Object.entries(json.metafile.inputs)) {
2020-11-17 18:38:48 +01:00
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 = [];
2021-03-10 00:32:35 +01:00
for (const [key, val] of Object.entries(json.metafile.outputs)) {
2020-11-17 18:38:48 +01:00
if (!key.endsWith('.map')) {
files.push(key);
stats.outputBytes = (stats.outputBytes || 0) + val.bytes;
}
}
stats.outputFiles = files.join(', ');
}
return stats;
}
2021-02-08 18:47:38 +01:00
// rebuild typings
2021-03-14 04:31:09 +01:00
async function compile(entryPoint, options) {
2021-03-13 17:26:53 +01:00
log.info('Compile typings:', entryPoint);
const program = ts.createProgram(entryPoint, options);
2021-02-08 18:47:38 +01:00
const emit = program.emit();
const diag = ts
.getPreEmitDiagnostics(program)
.concat(emit.diagnostics);
for (const info of diag) {
// @ts-ignore
const msg = info.messageText.messageText || info.messageText;
if (msg.includes('package.json')) continue;
if (info.file) {
const pos = info.file.getLineAndCharacterOfPosition(info.start || 0);
log.error(`TSC: ${info.file.fileName} [${pos.line + 1},${pos.character + 1}]:`, msg);
} else {
log.error('TSC:', msg);
}
}
}
2021-03-14 04:31:09 +01:00
async function typedoc(entryPoint) {
log.info('Generate TypeDocs:', entryPoint);
if (!td) {
td = new TypeDoc.Application();
td.options.addReader(new TypeDoc.TSConfigReader());
2021-03-15 17:29:51 +01:00
td.bootstrap({ entryPoints: entryPoint, theme: 'wiki/theme/' });
2021-03-14 04:31:09 +01:00
}
const project = td.convert();
const result = project ? await td.generateDocs(project, 'typedoc') : null;
if (result) log.warn('TypeDoc:', result);
}
2020-11-17 05:58:06 +01:00
// rebuild on file change
2021-02-21 13:20:58 +01:00
async function build(f, msg, dev = false) {
2021-02-08 17:39:09 +01:00
if (busy) {
log.state('Build: busy...');
setTimeout(() => build(f, msg), 500);
return;
}
busy = true;
2021-02-21 13:20:58 +01:00
log.info('Build: file', msg, f, 'type:', dev ? 'debug' : 'production', 'config:', dev ? config.debug : config.production);
2020-11-17 05:58:06 +01:00
// 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
2021-02-19 14:35:41 +01:00
// if ((require.main !== module) && (targetGroupName !== 'browserBundle')) continue;
2021-03-10 00:32:35 +01:00
const meta = dev
// @ts-ignore
? await esbuild.build({ ...config.common, ...config.debug, ...targetOptions })
// @ts-ignore
: await esbuild.build({ ...config.common, ...config.production, ...targetOptions });
const stats = await getStats(meta);
log.state(`Build for: ${targetGroupName} type: ${targetName}:`, stats);
}
2020-11-17 05:58:06 +01:00
}
2021-03-14 04:31:09 +01:00
if (!dev) {
// generate typings & typedoc only when run as explict build
await compile(targets.browserBundle.esm.entryPoints, tsconfig);
await changelog.update('../CHANGELOG.md');
await typedoc(targets.browserBundle.esm.entryPoints);
}
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
}
2021-02-08 17:39:09 +01:00
busy = false;
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;
}