Skip to content

Commit

Permalink
Fix bundling for Next.js
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Aug 1, 2024
1 parent 1c62712 commit 92f0fe7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/pglite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"test": "rm -rf ./pgdata-test && concurrently -s first --hide 1 --prefix none -k \"sleep 2 && ava tests/*.test.js tests/**/*.test.js\" \"npx http-server --port 3334 ./\"",
"test:quick": "rm -rf ./pgdata-bun-test && ava tests/*.test.js tests/target/node-*.test.js",
"test:bun": "rm -rf ./pgdata-test && npx bun test tests/basic.test.js && npx bun test tests/pgvector.test.js && npx bun test tests/targets/node-fs.test.js",
"build:js": "tsup",
"build:js": "tsup && tsx scripts/bundle-wasm.ts",
"build": "npm run build:js",
"dev": "concurrently \"tsup --watch\" \"sleep 1 && tsx scripts/bundle-wasm.ts\" \"pnpm dev-server\"",
"dev-server": "pnpm http-server ../",
Expand Down
42 changes: 20 additions & 22 deletions packages/pglite/scripts/bundle-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,27 @@ async function findAndReplaceInDir(
}
}

const copyFiles = async (srcDir: string, destDir: string) => {
await fs.mkdir(destDir, { recursive: true });
const files = await fs.readdir(srcDir);
for (const file of files) {
if (file.startsWith(".")) {
continue;
}
const srcFile = path.join(srcDir, file);
const destFile = path.join(destDir, file);
const stat = await fs.stat(srcFile);
if (stat.isFile()) {
await fs.copyFile(srcFile, destFile);
console.log(`Copied ${srcFile} to ${destFile}`);
}
}
};

async function main() {
await fs.copyFile("./release/postgres.wasm", "./dist/postgres.wasm");
await fs.copyFile("./release/postgres.data", "./dist/postgres.data");
await fs.copyFile("./release/postgres.js", "./dist/postgres.js");
await fs.copyFile("./release/vector.tar.gz", "./dist/vector.tar.gz");
await findAndReplaceInDir(
"./dist",
/new URL\('\.\.\/release\//g,
"new URL('./",
[".js"]
);
await findAndReplaceInDir(
"./dist",
/new URL\("\.\.\/release\//g,
'new URL("./',
[".js"]
);
await findAndReplaceInDir(
"./dist/vector",
/new URL\("\.\.\/\.\.\/release\//g,
'new URL("\.\.\/',
[".js"]
);
await copyFiles("./src/release", "./dist");
await findAndReplaceInDir("./dist", /\.\.\/release\//g, "./", [".js"]);
await findAndReplaceInDir("./dist", /\.\.\/release/g, "", [".js"], true);
}

await main();

0 comments on commit 92f0fe7

Please sign in to comment.