Skip to content

Commit

Permalink
Patch all bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-- authored and Mytherin committed Jan 2, 2024
1 parent fd4e276 commit f3158e2
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions packages/duckdb-wasm/bundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ fs.copyFile(path.resolve(src, 'bindings', 'duckdb-eh.wasm'), path.resolve(dist,
fs.copyFile(path.resolve(src, 'bindings', 'duckdb-coi.wasm'), path.resolve(dist, 'duckdb-coi.wasm'), printErr);

(async () => {
// Don't attempt to bundle NodeJS modules in the browser build.
console.log('[ ESBUILD ] Patch bindings');
patchFile('./src/bindings/duckdb-mvp.js', 'child_process');
patchFile('./src/bindings/duckdb-eh.js', 'child_process');
patchFile('./src/bindings/duckdb-coi.js', 'child_process');
patchFile('./src/bindings/duckdb-coi.pthread.js', 'vm');

// -------------------------------
// Browser bundles

Expand Down Expand Up @@ -132,13 +139,6 @@ fs.copyFile(path.resolve(src, 'bindings', 'duckdb-coi.wasm'), path.resolve(dist,
});

console.log('[ ESBUILD ] duckdb-browser-blocking.cjs');
// Don't attempt to bundle NodeJS modules in the browser build.
execSync(
`sed -i.bak 's/require("child_process")/["child_process"].map(require)/g' ./src/bindings/duckdb-mvp.js && rm ./src/bindings/duckdb-mvp.js.bak`,
);
execSync(
`sed -i.bak 's/require("child_process")/["child_process"].map(require)/g' ./src/bindings/duckdb-eh.js && rm ./src/bindings/duckdb-eh.js.bak`,
);
await esbuild.build({
entryPoints: ['./src/targets/duckdb-browser-blocking.ts'],
outfile: 'dist/duckdb-browser-blocking.cjs',
Expand Down Expand Up @@ -203,10 +203,6 @@ fs.copyFile(path.resolve(src, 'bindings', 'duckdb-coi.wasm'), path.resolve(dist,
});

console.log('[ ESBUILD ] duckdb-browser-coi.worker.js');
// Don't attempt to bundle NodeJS modules in the browser build.
execSync(
`sed -i.bak 's/require("vm")/["vm"].map(require)/g' ./src/bindings/duckdb-coi.pthread.js && rm ./src/bindings/duckdb-coi.pthread.js.bak`,
);
await esbuild.build({
entryPoints: ['./src/targets/duckdb-browser-coi.worker.ts'],
outfile: 'dist/duckdb-browser-coi.worker.js',
Expand Down Expand Up @@ -361,3 +357,9 @@ fs.copyFile(path.resolve(src, 'bindings', 'duckdb-coi.wasm'), path.resolve(dist,
console.log(`Patched ${file}`);
}
})();

function patchFile(fileName, moduleName) {
// Patch file to make sure ESBuild doesn't statically analyse and attempt to load "moduleName"
const sedCommand = `s/require("${moduleName}")/["${moduleName}"].map(require)/g`;
execSync(`sed -i.bak '${sedCommand}' ${fileName} && rm ${fileName}.bak`);
}

0 comments on commit f3158e2

Please sign in to comment.