question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

issue with polyfill in `vite build` output

See original GitHub issue

Describe the bug

during my exploration with @toruslabs/openlogin sdk, i find out that using vite.js gives a different result between dev and build environment.

both vite & vite build running successfully. the only problem started when we browse the app via browser.

my value of vite.config.js

export default defineConfig({
  publicDir: 'static',
  plugins: [vue()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  optimizeDeps: {
    include: [
      
    ],
    exclude: [
    ],
    esbuildOptions: {
      sourcemap: false,
      // Node.js global to browser globalThis
      define: {
          global: 'globalThis'
      },
      // Enable esbuild polyfill plugins
      plugins: [
          NodeGlobalsPolyfillPlugin({
            process: true,
            buffer: true
          }),
          
      ],
   }
  },
  build:{
    exclude: [
      
    ],
    assetsDir: 'src', 
    sourcemap: false,
    define: {
        global: 'globalThis',
    },
    rollupOptions: {
        output: {
            globals: {
            },
        },
        plugins: [
          rollupNodePolyFill(),
          ],
        
    },
    commonjsOptions: {
      transformMixedEsModules: true,
    },
  }, 
})

development env Screen Shot 2022-03-20 at 08 27 24

production env

  1. yarn build or vite build
  2. serve -d dist
  3. browse the output via browser, got error:
Screen Shot 2022-03-20 at 08 30 16

index.7152b104.js:2 TypeError: Cannot read properties of undefined (reading 'call') at new _r (index.7152b104.js:12:34847) at new P2 (index.7152b104.js:13:48810) at new m0 (index.7152b104.js:13:56954) at z2.setupStream (index.7152b104.js:13:60950) at z2.init (index.7152b104.js:13:60850) at async Promise.all (:3000/index 0) at async Y2.init (index.7152b104.js:19:5045) at async Proxy.login (index.7152b104.js:19:12134)

Reproduction

https://github.com/akangaziz/vite-openlogin-issue

System Info

` System:
    OS: macOS 11.4
    CPU: (8) arm64 Apple M1
    Memory: 117.55 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.14.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /opt/homebrew/bin/yarn
    npm: 8.5.4 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 99.0.4844.74
    Firefox: 97.0.2
    Safari: 14.1.1
  npmPackages:
    @vitejs/plugin-vue: ^2.2.2 => 2.2.4 
    vite: ^2.8.4 => 2.8.6 `

Used Package Manager

yarn

Logs

both `vite` or `vite build` runs successfully.

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
haydenyoungcommented, Sep 7, 2022

Node polyfilling isn’t something Vite supports by default as it often brings a larger client bundle size. It looks like this is a configuration issue with polyfills than a bug in Vite. Closing this for now.

Can we get some guidance on how to solve this in Vite? It seems a common problem and not being able to use node package because Vite doesn’t support polyfills makes continuing with Vite problematic.

From what I can see, the problem with the process package is that when polyfilling, files in vite/.deps (i’m guessing this is some kind of caching mechanism to speed up live loading) is creating a conflicting process function. So, it seems this is a problem within Vite?

It would be great to at least get clarity on this as the only “workaround” seems very clunky.

4reactions
bluwycommented, Sep 6, 2022

Node polyfilling isn’t something Vite supports by default as it often brings a larger client bundle size. It looks like this is a configuration issue with polyfills than a bug in Vite. Closing this for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Vite
Configuring Vite #. When running vite from the command line, Vite will automatically try to resolve a config file named vite.config.js inside project...
Read more >
Polyfill Node.js built-in modules with Vite | by Fabiano Taioli
A problem I have found using Vite in my project is how to poly-fill Node. js built-in modules for the browser. Using Node....
Read more >
How do I polyfill the "process" Node module in the Vite dev ...
You need to add some polyfill plugins to allow Node globals, such as process . Fortunately, you can simply edit (or create) the...
Read more >
rollup.js
Load the module code, but don't make any new objects available. import './module.js';. This is useful for polyfills, or when the primary purpose...
Read more >
@vitejs/plugin-legacy - npm
LEGACY env variable, which will only be true in the legacy production build, and false in all other cases. Usage. // vite.config.js import ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found