diff --git a/packages/plugins/@nocobase/plugin-workflow-javascript/build.config.ts b/packages/plugins/@nocobase/plugin-workflow-javascript/build.config.ts new file mode 100644 index 00000000000..570c4581f5b --- /dev/null +++ b/packages/plugins/@nocobase/plugin-workflow-javascript/build.config.ts @@ -0,0 +1,52 @@ +import { defineConfig } from '@nocobase/build'; +import fs from 'fs/promises'; +import path from 'path'; + +function getPackageRoot(packageName: string) { + let resolved = ''; + try { + resolved = require.resolve(`${packageName}/package.json`, { paths: [__dirname] }); + } catch (_error) { + resolved = require.resolve(packageName, { paths: [__dirname] }); + } + + const marker = `${path.sep}node_modules${path.sep}${packageName.split('/').join(path.sep)}${path.sep}`; + const markerIndex = resolved.lastIndexOf(marker); + if (markerIndex < 0) { + throw new Error(`Cannot resolve package root for ${packageName} from ${resolved}`); + } + + return resolved.slice(0, markerIndex + marker.length - 1); +} + +async function copyRuntimePackage(packageName: string, copied: Set, log: (msg: string) => void) { + if (copied.has(packageName)) { + return; + } + + copied.add(packageName); + + const source = getPackageRoot(packageName); + const packageJson = require(path.join(source, 'package.json')); + const target = path.resolve(__dirname, 'dist/node_modules', packageName); + + log(`copying ${packageName} to dist/node_modules`); + await fs.mkdir(path.dirname(target), { recursive: true }); + await fs.cp(source, target, { + recursive: true, + force: true, + dereference: false, + verbatimSymlinks: true, + }); + + const dependencies = Object.keys(packageJson.dependencies || {}); + for (const dependency of dependencies) { + await copyRuntimePackage(dependency, copied, log); + } +} + +export default defineConfig({ + afterBuild: async (log) => { + await copyRuntimePackage('quickjs-emscripten', new Set(), log); + }, +}); diff --git a/packages/plugins/@nocobase/plugin-workflow-javascript/src/server/QuickJs.js b/packages/plugins/@nocobase/plugin-workflow-javascript/src/server/QuickJs.js index d25b8e293f5..7d7fb508878 100644 --- a/packages/plugins/@nocobase/plugin-workflow-javascript/src/server/QuickJs.js +++ b/packages/plugins/@nocobase/plugin-workflow-javascript/src/server/QuickJs.js @@ -8,7 +8,19 @@ */ const { parentPort, workerData } = require('node:worker_threads'); -const { getQuickJS } = require('quickjs-emscripten'); +const path = require('node:path'); + +function loadQuickJS() { + const bundledPath = path.join(__dirname, '..', 'node_modules', 'quickjs-emscripten'); + try { + return require(bundledPath); + } catch (error) { + const packageName = 'quickjs-emscripten'; + return require(packageName); + } +} + +const { getQuickJS } = loadQuickJS(); const MEMORY_LIMIT_BYTES = 128 * 1024 * 1024; const MAX_STACK_SIZE_BYTES = 1024 * 1024;