chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Plugin to automatically mark all node:* imports as external.
|
||||
* This prevents having to manually list all Node.js built-in modules.
|
||||
*/
|
||||
export const externalNodeBuiltinsPlugin = () => ({
|
||||
name: "external-node-builtins",
|
||||
setup(build) {
|
||||
// Mark all node:* imports as external
|
||||
build.onResolve({ filter: /^node:/ }, (args) => ({
|
||||
path: args.path,
|
||||
external: true,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Plugin to ignore/exclude certain modules by returning an empty module.
|
||||
* Equivalent to webpack's resolve.alias with false value.
|
||||
*/
|
||||
export const ignoreModulesPlugin = (modules = []) => ({
|
||||
name: "ignore-modules",
|
||||
setup(build) {
|
||||
// Escape special regex characters in module names
|
||||
const escapeRegex = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const escapedModules = modules.map(escapeRegex);
|
||||
|
||||
// Match both "module" and "node:module" patterns
|
||||
const patterns = escapedModules.flatMap((mod) => [mod, `node:${mod}`]);
|
||||
const filter = new RegExp(`^(${patterns.join("|")})$`);
|
||||
|
||||
build.onResolve({ filter }, (args) => {
|
||||
return { path: args.path, namespace: "ignore-modules" };
|
||||
});
|
||||
build.onLoad({ filter: /.*/, namespace: "ignore-modules" }, (args) => {
|
||||
switch (args.path) {
|
||||
case "node:stream":
|
||||
return {
|
||||
contents: `
|
||||
const noop = () => {};
|
||||
export default {};
|
||||
export const Readable = { fromWeb: noop };
|
||||
`,
|
||||
};
|
||||
case "node:stream/promises":
|
||||
return {
|
||||
contents: `
|
||||
const noop = () => {};
|
||||
export default {};
|
||||
export const pipeline = noop;
|
||||
`,
|
||||
};
|
||||
case "node:fs":
|
||||
case "node:path":
|
||||
case "node:url":
|
||||
case "sharp":
|
||||
case "onnxruntime-node":
|
||||
default:
|
||||
return {
|
||||
contents: `export default {};`,
|
||||
};
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
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`);
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Plugin to strip the "node:" prefix from module requests.
|
||||
* Equivalent to webpack's StripNodePrefixPlugin.
|
||||
*/
|
||||
export const stripNodePrefixPlugin = () => ({
|
||||
name: "strip-node-prefix",
|
||||
setup(build) {
|
||||
build.onResolve({ filter: /^node:/ }, (args) => {
|
||||
return {
|
||||
path: args.path.replace(/^node:/, ""),
|
||||
external: true,
|
||||
};
|
||||
});
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user