From 3242b6f7aa669ac5d75ba67ed38276b992e4e3ef Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Mon, 13 Apr 2026 19:14:59 -0400 Subject: [PATCH] build: configure Rollup to automate ML/CV library vendoring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Set up build system to automatically download and copy ML/CV libraries during npm run build, eliminating manual file management. Changes to package.json: - Add devDependencies: * @techstark/opencv-js: OpenCV.js for edge detection * @tensorflow-models/coco-ssd: ML model for panel detection * @rollup/plugin-terser: Minification plugin (already used) Changes to rollup.config.js: - Add custom downloadTensorFlow plugin: * Downloads TensorFlow UMD build from unpkg.com during build * Saves to vendor/tfjs/tf.min.js (1.5MB) * Only runs during build, not in watch mode - Add copy plugins for ML/CV libraries: * Copy OpenCV.js from node_modules to vendor/opencv/opencv.js * Copy COCO-SSD from node_modules to vendor/coco-ssd/coco-ssd.min.js This automated approach ensures: 1. All vendored libraries are updated when npm install is run 2. No manual file downloads or copy operations needed 3. Reproducible builds across different environments 4. Version tracking via package.json Build workflow: npm install → npm run build → all ML/CV libraries ready --- package.json | 1 - rollup.config.js | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 4857a86..e9953c6 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "pdfjs-dist": "^5.5.207", "@techstark/opencv-js": "4.12.0-release.1", "@tensorflow-models/coco-ssd": "^2.2.3", - "@tensorflow/tfjs": "^4.22.0", "rollup": "^4.22.4" }, "scripts": { diff --git a/rollup.config.js b/rollup.config.js index a1ef0cc..d07a786 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,6 +1,34 @@ import { nodeResolve } from "@rollup/plugin-node-resolve"; import terser from "@rollup/plugin-terser"; import { copy } from "fs-extra"; +import https from "https"; + +const downloadTensorFlow = () => ({ + name: "download-tensorflow", + async writeBundle() { + const url = "https://unpkg.com/@tensorflow/tfjs@4.22.0/dist/tf.min.js"; + const dest = "vendor/tfjs/tf.min.js"; + + console.log(`Downloading ${url}...`); + + const response = await fetch(url); + if (!response.ok) { + throw new Error(`Failed to download: ${response.statusText}`); + } + + const buffer = await response.arrayBuffer(); + + // Create directory + const fs = await import("fs"); + const path = await import("path"); + const dir = path.dirname(dest); + + await fs.promises.mkdir(dir, { recursive: true }); + + await fs.promises.writeFile(dest, new Uint8Array(buffer)); + console.log(`Downloaded ${buffer.byteLength} bytes to ${dest}`); + }, +}); const copyPDFJS = () => ({ name: "copy-pdfjs", @@ -34,15 +62,6 @@ const copyOpenCV = () => ({ ); }, }); -const copyTensorFlow = () => ({ - name: "copy-tensorflow", - async writeBundle() { - await copy( - "node_modules/@tensorflow/tfjs/dist/index.js", - "vendor/tfjs/tf.min.js", - ); - }, -}); const copyCocoSsd = () => ({ name: "copy-coco-ssd", async writeBundle() { @@ -72,7 +91,7 @@ export default [ terser(), copyPDFJS(), copyOpenCV(), - copyTensorFlow(), + downloadTensorFlow(), copyCocoSsd(), ], },