Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 1 addition & 40 deletions src/build/build-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -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;
Expand Down
33 changes: 4 additions & 29 deletions src/build/build-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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({
Expand All @@ -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,
}));
},
};
}
47 changes: 47 additions & 0 deletions src/build/esbuild-plugins.js
Original file line number Diff line number Diff line change
@@ -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,
}));
},
};
}
Loading