Skip to content

Commit

Permalink
Modify build to not run postgres.js though tsup
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Jul 25, 2024
1 parent a679733 commit f9aed97
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/pglite/scripts/bundle-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function findAndReplaceInDir(
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.so", "./dist/postgres.so");
await fs.copyFile("./release/postgres.js", "./dist/postgres.js");
await fs.copyFile("./release/vector.tar.gz", "./dist/vector.tar.gz");
await findAndReplaceInDir(
"./dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/extensionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tinyTar from "tinytar";
import { IN_NODE } from "./utils.js";
import type { PostgresMod } from "./postgres.js";
import type { PostgresMod } from "./postgresMod.js";

export async function loadExtensionBundle(
bundlePath: URL,
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/idbfs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FilesystemBase } from "./types.js";
import type { FS, PostgresMod } from "../postgres.js";
import type { FS, PostgresMod } from "../postgresMod.js";
import { PGDATA } from "./index.js";
import { dumpTar } from "./tarUtils.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/memoryfs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FilesystemBase } from "./types.js";
import type { PostgresMod, FS } from "../postgres.js";
import type { PostgresMod, FS } from "../postgresMod.js";
import { dumpTar } from "./tarUtils.js";

export class MemoryFS extends FilesystemBase {
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/nodefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import { FilesystemBase } from "./types.js";
import { PGDATA } from "./index.js";
import type { PostgresMod, FS } from "../postgres.js";
import type { PostgresMod, FS } from "../postgresMod.js";
import { dumpTar } from "./tarUtils.js";

export class NodeFS extends FilesystemBase {
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/tarUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { tar, untar, type TarFile, REGTYPE, DIRTYPE } from "tinytar";
import { FS } from "../postgres.js";
import { FS } from "../postgresMod.js";
import { PGDATA } from "./index.js";

export async function dumpTar(FS: FS, dbname?: string): Promise<File | Blob> {
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PostgresMod, FS } from "../postgres.js";
import type { PostgresMod, FS } from "../postgresMod.js";

export type FsType = "nodefs" | "idbfs" | "memoryfs";

Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Mutex } from "async-mutex";
import PostgresModFactory, { type PostgresMod } from "./postgres.js";
import PostgresModFactory, { type PostgresMod } from "./postgresMod.js";
import { type Filesystem, parseDataDir, loadFs } from "./fs/index.js";
import { makeLocateFile } from "./utils.js";
import { parseResults } from "./parse.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ts-ignore
import PostgresModFactory from "../release/postgres.js";
// This file in placed in the build from ../release/postgres.js
import PostgresModFactory from "./postgres.js";

// Uses the types from @types/emscripten

Expand Down
29 changes: 27 additions & 2 deletions packages/pglite/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { defineConfig } from "tsup";
import path from "path";
import { fileURLToPath } from "url";
import { Plugin } from 'esbuild'
import fs from 'fs'

const thisFile = fileURLToPath(new URL(import.meta.url));
const root = path.dirname(thisFile);

let replaceAssertPlugin = {
const replaceAssertPlugin = {
name: "replace-assert",
setup(build: any) {
// Resolve `assert` to a blank file
Expand All @@ -15,6 +17,25 @@ let replaceAssertPlugin = {
},
};

const excludeFromMinificationAndChunkingPlugin: Plugin = {
name: 'exclude-from-minification-and-chunking',
setup(build) {
const filePath = path.resolve(root, 'release', 'postgres.js');
build.onResolve({ filter: /release\/postgres\.js$/ }, args => ({
path: filePath,
namespace: 'exclude-from-minification-and-chunking',
}));

build.onLoad({ filter: /.*/, namespace: 'exclude-from-minification-and-chunking' }, async args => {
const contents = await fs.promises.readFile(args.path, 'utf8');
return {
contents,
loader: 'ts',
};
});
}
}

const entryPoints = [
"src/index.ts",
"src/worker/index.ts",
Expand All @@ -31,12 +52,16 @@ export default defineConfig({
},
clean: true,
format: ["esm"],
external: [
'./postgres.js',
// path.resolve(root, 'release', 'postgres.js'), // Exclude postgres.js from bundling
],
esbuildOptions(options, context) {
options.inject = [
"src/polyfills/buffer.ts",
"src/polyfills/indirectEval.ts",
];
},
esbuildPlugins: [replaceAssertPlugin],
esbuildPlugins: [replaceAssertPlugin, excludeFromMinificationAndChunkingPlugin],
minify: true,
});
8 changes: 2 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f9aed97

Please sign in to comment.