Files
huggingface--transformers.js/packages/transformers/scripts/build/plugins/postBuildPlugin.mjs
T
wehub-resource-sync 311a3666c4
Build documentation / build (push) Failing after 0s
Unit tests / build (18) (push) Has been cancelled
Unit tests / build (20) (push) Has been cancelled
Unit tests / build (22) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:44:39 +08:00

44 lines
1.4 KiB
JavaScript

import path from "node:path";
import { copyFileSync, unlinkSync, existsSync } from "node:fs";
import { colors, createLogger } from "../../../../../scripts/logger.mjs";
const log = createLogger("transformers");
/**
* Plugin to post-process build files.
* Equivalent to webpack's PostBuildPlugin.
*/
export const postBuildPlugin = (distDir, rootDir) => {
// it should copy the files only once. In watch mode for example it should not rerun every time
let completed = false;
return {
name: "post-build",
setup(build) {
build.onEnd(() => {
if (completed) return;
completed = true;
const ORT_JSEP_FILE = "ort-wasm-simd-threaded.jsep.mjs";
const ORT_BUNDLE_FILE = "ort.bundle.min.mjs";
// 1. Remove unnecessary files
const file = path.join(distDir, ORT_BUNDLE_FILE);
if (existsSync(file)) unlinkSync(file);
// 2. Copy unbundled JSEP file
const ORT_SOURCE_DIR = path.join(rootDir, "node_modules/onnxruntime-web/dist");
const src = path.join(ORT_SOURCE_DIR, ORT_JSEP_FILE);
if (existsSync(src)) {
const dest = path.join(distDir, ORT_JSEP_FILE);
copyFileSync(src, dest);
log.success(`${colors.gray}Copied ${ORT_JSEP_FILE}${colors.reset}`);
} else {
log.warning(`Could not find ${ORT_JSEP_FILE} in node_modules`);
}
});
},
};
};