electric-sql / electric-sql/pglite

Solution to import resolve warnings when using esbuild?

Open
#546 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
TypeScript
Stars
16k
Forks
442
Avg merge
20h 19m
Merged PRs (30d)
7

Description

I'm using PGlite within a Phoenix app, which uses esbuild (by default) as its bundler. I have things working thanks to the pointers on https://github.com/electric-sql/pglite/issues/478#issue-2753693700

Fwiw, my instantiation code (I'm using sync, live and vector) looks like this:

```ts
async function _loadPGlite(): Promise {
const dataFetch = fetch('/pglite/postgres.data')
const wasmFetch = fetch('/pglite/postgres.wasm')

const vector = {
name: 'pgvector',
setup: (_pg: PGliteInterface, emscriptenOpts: any) => {
return {
emscriptenOpts,
bundlePath: new URL(`${window.location.origin}/pglite/vector.tar.gz`)
}
}
}

const [fsBundle, wasmModule] = await Promise.all([
dataFetch.then(resp => resp.blob()),
WebAssembly.compileStreaming(wasmFetch)
])

const pglite: PGliteWithExtensions = await PGlite.create(
DATA_DIR, {
extensions: {
electric: electricSync(),
live,
vector
},
relaxedDurability: true,
fsBundle,
wasmModule
}
)

await pglite.exec(localSchema)

return pglite
}
```

My esbuild config looks like this, using :

```elixir
config :esbuild,
version: "0.17.11",
app: [
args: [
"js/index.ts",
"--bundle",
"--external:/fonts/*",
"--external:/images/*",
"--external:/pglite/*",
"--loader:.sql=file",
"--loader:.svg=file",
"--outdir=../priv/static/assets",
"--target=es2020"
],
cd: Path.expand("../assets", __DIR__),
env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
]
```

This bundles for the browser using the default `iife` platform. When I run the asset build, it works and my app runs. However I get the following four warnings from require statements in the nodefs:

```
▲ [WARNING] Import "resolve" will always be undefined because the file "(disabled):path" has no exports [import-is-undefined]

node_modules/@electric-sql/pglite/dist/fs/nodefs.js:1:198:
1 │ ...r(t){super(t),this.rootDir=o.resolve(t),s.existsSync(o.join(this...
╵ ~~~~~~~

▲ [WARNING] Import "existsSync" will always be undefined because the file "(disabled):fs" has no exports [import-is-undefined]

node_modules/@electric-sql/pglite/dist/fs/nodefs.js:1:211:
1 │ ...his.rootDir=o.resolve(t),s.existsSync(o.join(this.rootDir))||s.m...
╵ ~~~~~~~~~~

▲ [WARNING] Import "join" will always be undefined because the file "(disabled):path" has no exports [import-is-undefined]

node_modules/@electric-sql/pglite/dist/fs/nodefs.js:1:224:
1 │ ...r=o.resolve(t),s.existsSync(o.join(this.rootDir))||s.mkdirSync(t...
╵ ~~~~

▲ [WARNING] Import "mkdirSync" will always be undefined because the file "(disabled):fs" has no exports [import-is-undefined]

node_modules/@electric-sql/pglite/dist/fs/nodefs.js:1:247:
1 │ ...nc(o.join(this.rootDir))||s.mkdirSync(this.rootDir)}async init(t...
╵ ~~~~~~~~~
```

Is there a way to get rid of these warnings? Why is my browser bundle including the nodefs?

N.b.: I've tried marking `@electric-sql/pglite` as external using `"--external:@electric-sql/pglite"` in the esbuild args list but that creates runtime errors:

```
Uncaught Error: Dynamic require of "@electric-sql/pglite" is not supported
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.