diff --git a/src/build/build-example.js b/src/build/build-example.js index 44a09c7e88..09dbf8468e 100644 --- a/src/build/build-example.js +++ b/src/build/build-example.js @@ -3,6 +3,7 @@ import path from 'path'; import ts from 'typescript'; import esbuild from 'esbuild'; import { platform } from 'node:process'; +import { makeNodeModulesExternal, makeJsooExternal, makeO1jsExternal } from './esbuild-plugins.js'; export { buildAndImport, build, buildOne }; @@ -103,46 +104,6 @@ function typescriptPlugin(tsConfig) { }; } -function makeNodeModulesExternal() { - let isNodeModule = /^[^./\\]|^\.[^./\\]|^\.\.[^/\\]/; - return { - name: 'plugin-external', - setup(build) { - build.onResolve({ filter: isNodeModule }, ({ path }) => ({ - path, - external: !(platform === 'win32' && path.endsWith('index.js')), - })); - }, - }; -} - -function makeO1jsExternal() { - return { - name: 'plugin-external', - setup(build) { - build.onResolve({ filter: /^o1js$/ }, () => ({ - path: './index.js', - external: true, - })); - }, - }; -} - -function makeJsooExternal() { - let isJsoo = /(bc.cjs|plonk_wasm.cjs)$/; - return { - name: 'plugin-external', - setup(build) { - build.onResolve({ filter: isJsoo }, ({ path: filePath, resolveDir }) => { - return { - path: path.resolve(resolveDir, filePath), - external: true, - }; - }); - }, - }; -} - function findTsConfig() { let tsConfigPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists); if (tsConfigPath === undefined) return; diff --git a/src/build/build-node.js b/src/build/build-node.js index 06cb4867e4..6fbb795df9 100644 --- a/src/build/build-node.js +++ b/src/build/build-node.js @@ -3,6 +3,7 @@ import { platform } from 'node:process'; import { fileURLToPath } from 'node:url'; import esbuild from 'esbuild'; import minimist from 'minimist'; +import { makeNodeModulesExternal, makeJsooExternal } from './esbuild-plugins.js'; let { bindings = './src/bindings/compiled/node_bindings/' } = minimist(process.argv.slice(2)); @@ -23,7 +24,8 @@ if (isMain) { } async function buildNode({ production }) { - // bundle the index.js file with esbuild and create a new index.cjs file which conforms to CJS + // bundle the index.js file with esbuild and create a new index.cjs file which + // conforms to CJS let jsEntry = path.resolve('dist/node', path.basename(entry).replace('.ts', '.js')); let outfile = jsEntry.replace('.js', '.cjs'); await esbuild.build({ @@ -35,35 +37,8 @@ async function buildNode({ production }) { target, resolveExtensions: ['.node.js', '.ts', '.js'], allowOverwrite: true, - plugins: [makeNodeModulesExternal(), makeJsooExternal()], + plugins: [makeNodeModulesExternal(), makeJsooExternal({ useRelativePath: true })], dropLabels: ['ESM'], minify: false, }); } - -function makeNodeModulesExternal() { - let isNodeModule = /^[^./\\]|^\.[^./\\]|^\.\.[^/\\]/; - return { - name: 'plugin-external', - setup(build) { - build.onResolve({ filter: isNodeModule }, ({ path }) => ({ - path, - external: !(platform === 'win32' && path.endsWith('index.js')), - })); - }, - }; -} - -function makeJsooExternal() { - let isJsoo = /(bc.cjs|plonk_wasm.cjs)$/; - return { - name: 'plugin-external', - setup(build) { - build.onResolve({ filter: isJsoo }, ({ path: filePath, resolveDir }) => ({ - path: - './' + path.relative(path.resolve('.', 'dist/node'), path.resolve(resolveDir, filePath)), - external: true, - })); - }, - }; -} diff --git a/src/build/esbuild-plugins.js b/src/build/esbuild-plugins.js new file mode 100644 index 0000000000..7355ac963e --- /dev/null +++ b/src/build/esbuild-plugins.js @@ -0,0 +1,47 @@ +import { platform } from 'node:process'; +import path from 'node:path'; + +export { makeNodeModulesExternal, makeJsooExternal, makeO1jsExternal }; + +function makeNodeModulesExternal() { + let isNodeModule = /^[^./\\]|^\.[^./\\]|^\.\.[^/\\]/; + return { + name: 'plugin-external', + setup(build) { + build.onResolve({ filter: isNodeModule }, ({ path }) => ({ + path, + external: !(platform === 'win32' && path.endsWith('index.js')), + })); + }, + }; +} + +function makeJsooExternal(options = {}) { + let isJsoo = /(bc.cjs|plonk_wasm.cjs)$/; + return { + name: 'plugin-external', + setup(build) { + build.onResolve({ filter: isJsoo }, ({ path: filePath, resolveDir }) => { + const resolvedPath = path.resolve(resolveDir, filePath); + return { + path: options.useRelativePath + ? './' + path.relative(path.resolve('.', 'dist/node'), resolvedPath) + : resolvedPath, + external: true, + }; + }); + }, + }; +} + +function makeO1jsExternal() { + return { + name: 'plugin-external', + setup(build) { + build.onResolve({ filter: /^o1js$/ }, () => ({ + path: './index.js', + external: true, + })); + }, + }; +}