onnxruntime/js/web/test/test-shared.ts
Yulong Wang 6ea493571e
[js/web] use esbuild to accelerate bundle build (#17745)
### Description

Use esbuild to accelerate bundle build.

This change uses esbuild to replace webpack for onnxruntime-web. Bundle
build time reduced from ~20sec to ~0.6sec on my windows dev box.

A few changes applied:
- import nodejs modules using "node:" prefix
- remove enum declaration inside namespace (EncoderUsage)
- use "fs/promise" to replace the old promisify from "util"
- separate ort-web and test-runner. Previously they are bundled
together, now they are built into 2 files.
- optimize karma runner launch time
- remove unnecessary sourcemap preprocessor. sourcemaps are handled
inside esbuild
- remove unnecessary proxies (because ort-web and test-runner are
separated now, the path are correctly inferred)
    - remove file watcher for test data
- optimize special handling as esbuild plugins:
- polyfill dummy imports for node.js modules when targetting browser.
    - load as content string for ort-wasm-*.worker.js
    - load as content string for ./proxy-worker/main.ts
- a source patch to ort-wasm*-threaded*.js (see details in comments in
code)
- updated debug configurations for sourcemap mapping to ensure
out-of-box good dev experience
2023-10-06 13:37:37 -07:00

47 lines
1.3 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as base64 from 'base64-js';
import * as fs from 'node:fs/promises';
import {Attribute} from '../lib/onnxjs/attribute';
import {Graph} from '../lib/onnxjs/graph';
export function base64toBuffer(data: string): Uint8Array {
return base64.toByteArray(data);
}
export function bufferToBase64(buffer: Uint8Array): string {
return base64.fromByteArray(buffer);
}
export async function readFile(file: string) {
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
// node
return fs.readFile(file);
} else {
// browser
const response = await fetch(file);
return new Uint8Array(await response.arrayBuffer());
}
}
export async function readJsonFile(file: string): Promise<any> {
const content = await readFile(file);
return JSON.parse(new TextDecoder().decode(content));
}
/**
* create a single-node graph for unit test purpose
*/
export function createMockGraph(opType: string, attributes: Attribute): Graph {
const node: Graph.Node = {name: '', opType, inputs: [], outputs: [], attributes};
return {
getInputIndices: () => [],
getInputNames: () => [],
getOutputIndices: () => [],
getOutputNames: () => [],
getNodes: () => [node],
getValues: () => []
};
}