Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution to import resolve warnings when using esbuild? #546

Open
thruflo opened this issue Feb 17, 2025 · 0 comments
Open

Solution to import resolve warnings when using esbuild? #546

thruflo opened this issue Feb 17, 2025 · 0 comments
Labels
question Further information is requested

Comments

@thruflo
Copy link
Contributor

thruflo commented Feb 17, 2025

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 #478 (comment)

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

async function _loadPGlite(): Promise<PGliteWithExtensions> {
  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 :

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
@thruflo thruflo added the question Further information is requested label Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant