onnxruntime/js/web/script/build.ts
Yulong Wang 4ebc9c3b5e
[JS] onnxruntime-web (#7394)
* add web

* add script and test

* fix lint

* add test/data/ops

* add test/data/node/ to gitignore

* modify scripts

* add onnxjs

* fix tests

* fix test-runner

* fix sourcemap

* fix onnxjs profiling

* update test list

* update README

* resolve comments

* set wasm as default backend

* rename package

* update copyright header

* do not use class "Buffer" in browser context

* revise readme
2021-04-27 00:04:25 -07:00

59 lines
2.3 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {execSync, spawnSync} from 'child_process';
import * as fs from 'fs-extra';
import minimist from 'minimist';
import npmlog from 'npmlog';
import * as path from 'path';
// CMD args
const args = minimist(process.argv);
const MODE = args.config || 'prod'; // prod|dev|test
if (['prod', 'dev', 'test'].indexOf(MODE) === -1) {
throw new Error(`unknown build mode: ${MODE}`);
}
// Path variables
const WASM_BINDING_FOLDER = path.join(__dirname, '..', 'lib', 'wasm', 'binding');
const WASM_JS_PATH = path.join(WASM_BINDING_FOLDER, 'onnxruntime_wasm.js');
const WASM_PATH = path.join(WASM_BINDING_FOLDER, 'onnxruntime_wasm.wasm');
const WASM_DIST_FOLDER = path.join(__dirname, '..', 'dist');
const WASM_DIST_PATH = path.join(WASM_DIST_FOLDER, 'onnxruntime_wasm.wasm');
try {
npmlog.info('Build', `Ensure file: ${WASM_JS_PATH}`);
if (!fs.pathExistsSync(WASM_JS_PATH)) {
throw new Error(`file does not exist: ${WASM_JS_PATH}`);
}
npmlog.info('Build', `Ensure file: ${WASM_PATH}`);
if (!fs.pathExistsSync(WASM_PATH)) {
throw new Error(`file does not exist: ${WASM_PATH}`);
}
} catch (e) {
npmlog.error('Build', `WebAssembly files are not ready. build WASM first. ERR: ${e}`);
throw e;
}
npmlog.info('Build', `Copying file "${WASM_PATH}" to "${WASM_DIST_PATH}"...`);
fs.ensureDirSync(WASM_DIST_FOLDER);
fs.copyFileSync(WASM_PATH, WASM_DIST_PATH);
npmlog.info('Build', 'Building bundle...');
{
npmlog.info('Build.Bundle', '(1/2) Retrieving npm bin folder...');
const npmBin = execSync('npm bin', {encoding: 'utf8'}).trimRight();
npmlog.info('Build.Bundle', `(1/2) Retrieving npm bin folder... DONE, folder: ${npmBin}`);
npmlog.info('Build.Bundle', '(2/2) Running webpack to generate bundles...');
const webpackCommand = path.join(npmBin, 'webpack');
const webpackArgs = ['--env', `--bundle-mode=${MODE}`];
npmlog.info('Build.Bundle', `CMD: ${webpackCommand} ${webpackArgs.join(' ')}`);
const webpack = spawnSync(webpackCommand, webpackArgs, {shell: true, stdio: 'inherit'});
if (webpack.status !== 0) {
console.error(webpack.error);
process.exit(webpack.status === null ? undefined : webpack.status);
}
npmlog.info('Build.Bundle', '(2/2) Running webpack to generate bundles... DONE');
}
npmlog.info('Build', 'Building bundle... DONE');