Giter VIP home page Giter VIP logo

vite-plugin-node-polyfills's Introduction

Sponsor me Donate

vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments. Supports node: protocol imports.

Why do I need this?

Module "stream" has been externalized for browser compatibility. Cannot access "stream.Readable" in client code.

Since browsers do not support Node's Core Modules, packages that use them must be polyfilled to function in browser environments. In an attempt to prevent runtime errors, Vite produces errors or warnings when your code references builtin modules such as fs or path.

Getting Started

Install the package as a dev dependency.

# npm
npm install --save-dev vite-plugin-node-polyfills

# pnpm
pnpm install --save-dev vite-plugin-node-polyfills

# yarn
yarn add --dev vite-plugin-node-polyfills

Add the plugin to your vite.config.ts file.

import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    nodePolyfills(),
  ],
})

Customizable when you need it

The following options are available to customize it for your needs.

import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    nodePolyfills({
      // To add only specific polyfills, add them here. If no option is passed, adds all polyfills
      include: ['path'],
      // To exclude specific polyfills, add them to this list. Note: if include is provided, this has no effect
      exclude: [
        'http', // Excludes the polyfill for `http` and `node:http`.
      ],
      // Whether to polyfill specific globals.
      globals: {
        Buffer: true, // can also be 'build', 'dev', or false
        global: true,
        process: true,
      },
      // Override the default polyfills for specific modules.
      overrides: {
        // Since `fs` is not supported in browsers, we can use the `memfs` package to polyfill it.
        fs: 'memfs',
      },
      // Whether to polyfill `node:` protocol imports.
      protocolImports: true,
    }),
  ],
})

All polyfills

  • If protocolImports is true, also adds node:[module]
[
  '_stream_duplex',
  '_stream_passthrough',
  '_stream_readable',
  '_stream_transform',
  '_stream_writable',
  'assert',
  'buffer',
  'child_process',
  'cluster',
  'console',
  'constants',
  'crypto',
  'dgram',
  'dns',
  'domain',
  'events',
  'fs',
  'http',
  'http2',
  'https',
  'module',
  'net',
  'os',
  'path',
  'process',
  'punycode',
  'querystring',
  'readline',
  'repl',
  'stream',
  'string_decoder',
  'sys',
  'timers',
  'timers/promises',
  'tls',
  'tty',
  'url',
  'util',
  'vm',
  'zlib',
]

About the author

Hello! My name is David, and in my spare time, I build tools to help developers be more productive. If you find my work valuable, I would really appreciate a sponsorship or donation. If you want to see more of my work, check out davidmyers.dev.

Thanks for your support! 🪴

vite-plugin-node-polyfills's People

Contributors

arnaudbarre avatar atlowchemi avatar cristian-spiescu avatar davidmyersdev avatar derekdavenport avatar maxnowack avatar nflaig avatar rugvip avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vite-plugin-node-polyfills's Issues

Uncaught SyntaxError: Unexpected string

I have a react app with the lib mode activated in my vite config.
I use import.meta.env.mode then build the app.
When I use the library in an other app I get :
Uncaught SyntaxError: Unexpected string

reproducible example repo

steps to reproduce:
1- install dependencies

pnpm install

2- build the library :

pnpm build

3- In main.jsx, uncomment line 4 and comment line 3

4- start the dev server

pnpm dev

5- check the console in Chrome
Uncaught SyntaxError: Unexpected string and the webpage shows nothing

Expected behaviour :
The web page displays "yay production" in prod and "not production" in dev mode.

I reproduced it with a Firefox and Chrome.

Warning on "use client" directive when using plugin alongside react

When using this plugin alongside some react libraries, running a vite build results in a spew of errors like:

../node_modules/wagmi/dist/index.js (1:0) Module level directives cause errors when bundled, "use client" in "../node_modules/wagmi/dist/index.js" was ignored.

There are a bunch of issues filed in several libraries, but I think something is happening when using both this plugin and react, when dependencies use the new use client directive. See this issue in the rollup project:

rollup/rollup#4699

I have created a small stackblitz environment where running npm run build will result in these warnings:

https://stackblitz.com/edit/vitejs-vite-pcpqdr?file=vite.config.ts

The key thing to note is that just removing this plugin fixes all the warnings. So there is some funky interaction.

Uncaught ReferenceError: Buffer is not defined

Hi @davidmyersdev,

  1. When i use vite-plugin-node-polyfills and config default, run dev environment it gives error buffer.
Screen Shot 2023-06-20 at 16 22 15

Screen Shot 2023-06-20 at 16 22 32

  1. When i run the build environment it works.

Screen Shot 2023-06-20 at 16 23 22

link: [https://codesandbox.io/p/github/navan101/vite-buffer/master?file=/src/App.vue:1,1&workspaceId=89d4ce07-40e9-48ee-965a-5e9183e6320b]

Thank you!

Cannot rollup in NuxtJS 3

I am trying put third-party markdown editor into my project, and it is lucky to have this plugin to solve the problem:
"Module "stream" has been externalized for browser compatibility. Cannot access "stream.Readable" in client code."

However, when I am trying to build my project for production, there is an error occur:
image

I am able to build my project without adding this plugin to vite config, but all my markdown components & Github Octokit modules will not be functional.

Therefore, I would like to know if there is any solution regarding to my problem?

v0.11 - ReferenceError [Error]: window is not defined

v0.9 builds fine. Bumping to v0.11 causes this stack with same codebase everywhere else:

node:internal/event_target:1012
process.nextTick(() => { throw err; });
^
ReferenceError [Error]: window is not defined
at file:///home/build/block/client/.svelte-kit/output/server/chunks/index3.js:2:1
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Emitted 'error' event on Worker instance at:
at [kOnErrorMessage] (node:internal/worker:300:10)
at [kOnMessage] (node:internal/worker:311:37)
at MessagePort. (node:internal/worker:212:57)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28)

Node.js v18.16.0
 ELIFECYCLE  Command failed with exit code 1.

Plugin breaches heap limit on large projects

So some good news, and some bad news. Good news: for smaller projects, v0.11.2 does work. The bad news is that for a larger project I'm working on, this plugin is causing the build to fail with a heap limit error,

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
Stack trace
 1: 0xc98550 node::Abort() [/home/user/.nvm/versions/node/v20.5.1/bin/node]
 2: 0xb700a7  [/home/user/.nvm/versions/node/v20.5.1/bin/node]
 3: 0xebae80 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/home/user/.nvm/versions/node/v20.5.1/bin/node]
 4: 0xebb167 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/home/user/.nvm/versions/node/v20.5.1/bin/node]
 5: 0x10cc7d5  [/home/user/.nvm/versions/node/v20.5.1/bin/node]
 6: 0x10e4658 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/home/user/.nvm/versions/node/v20.5.1/bin/node]
 7: 0x10ba771 v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/home/user/.nvm/versions/node/v20.5.1/bin/node]
 8: 0x10bb905 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/home/user/.nvm/versions/node/v20.5.1/bin/node]
 9: 0x1098e76 v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/home/user/.nvm/versions/node/v20.5.1/bin/node]
10: 0x14f3ca6 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/home/user/.nvm/versions/node/v20.5.1/bin/node]
11: 0x7f93f7ed9ef6
Aborted (core dumped)

Possibly useful info:

  • As modules get processed, the speed at which they get processed decreases.
  • With Node v18 Vite processes ~750 modules before crashing, with Node v20 closer to ~1100
  • I'm developing a web extension, which requires assets to be written to disk even in dev mode. This is done via vite-plugin-web-extension.

I've created a new reproduction repo using this plugin on a large codebase which displays the error above during the build.

Injected path cannot be marked as external: Vite 4.0.4

I added the lib using yarn and updated vite.config.js as instructed, but I get this error when I run the server:

✘ [ERROR] The injected path "myproj/node_modules/node-stdlib-browser/helpers/esbuild/shim.js" cannot be marked as external

Build failed with 1 error:
error: The injected path "myproj/node_modules/node-stdlib-browser/helpers/esbuild/shim.js" cannot be marked as external

I'm using
Vite: 4.0.4
Node: 18.12.1
Yarn: 1.22.19

default.createRequire not working

I am working on a CommonJS module, which uses Module as in Module.createRequire, however, Module is null so it is throwing errors

Can you please let me know how to fix this?

Unexpected absolute import paths

It seems that when using this plugin, one of the related output files has an absolute import path, which fails to resolve when loaded in the broswer:

import {Buffer as BufferPolyfill} from '/home/path/to/repo/node_modules/vite-plugin-node-polyfills/shims/dist/index.js'
globalThis.Buffer = BufferPolyfill
import {global as globalPolyfill} from '/home/path/to/repo/node_modules/vite-plugin-node-polyfills/shims/dist/index.js'
globalThis.global = globalPolyfill
import {process as processPolyfill} from '/home/path/to/repo/node_modules/vite-plugin-node-polyfills/shims/dist/index.js'
globalThis.process = processPolyfill
import {i} from "../../refresh-runtime.js";
i(window);
window.$RefreshReg$ = ()=>{}
;
window.$RefreshSig$ = ()=>o=>o;

Other assets I've inspected don't have the /home/path/to/repo bit, and start directly at /node_modules/....

This is what the Vite config looks like,
import react from '@vitejs/plugin-react-swc';
import path from 'path';
import { UserConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import wasm from 'vite-plugin-wasm';
import webExtension, { readJsonFile } from 'vite-plugin-web-extension';
import tsconfigPaths from 'vite-tsconfig-paths';

const config: UserConfig = {
  build: {
    target: 'esnext',
  },
  plugins: [
    react(),
    tsconfigPaths(),
    nodePolyfills(),
    wasm(),
    webExtension(),
  ],
  resolve: {
    alias: {
      // Needed by the `vite-plugin-web-extension` plugin.
      // See https://github.com/aklinker1/vite-plugin-web-extension/issues/84#issuecomment-1471196461
      // In dev mode, make sure fast refresh works
      '/@react-refresh': path.resolve('node_modules/@vitejs/plugin-react-swc/refresh-runtime.js'),
    },
  },
};

export default config;

Any idea how to fix the imports? Could the web extension plugin be interfering somehow?

Uncaught ReferenceError: global is not defined

image

vue: 3.3.4
vite: 4.4.9

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import { setAutoImport } from './settings/plugins/autoImport';
import { setAutoComponents } from './settings/plugins/components';

export default defineConfig({
    plugins: [
        vue(),
        nodePolyfills({
            include: ['buffer', 'process'],
            globals: {
                Buffer: true, // can also be 'build', 'dev', or false
                global: true,
                process: true,
            },
            protocolImports: false,
        }),
        setAutoImport(),
        setAutoComponents(),
        viteCompression(), // { disable: !isProduction }
    ],
...
});

Request: Add `include` option to only include pollyfills for certain packages

Sometimes it's more beneficial to have an include option instead of an exclude one.
In my case, I have a package that is importing only url and I only need to polyfill that.

Proposed API:

import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

export default defineConfig({
  plugins: [
    nodePolyfills({
     // Only polyfill url module
      include: [
        'url'
      ],
      // Has no effect, when include is also provided
      exclude: [
        'fs', 
      ],

    }),
  ],
})

Does not work!

failed to load config from /Users/steve/sites/dfxswap.io/vite.config.js
error when starting dev server:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/steve/sites/dfxswap.io/node_modules/vite-plugin-node-polyfills/package.json
at new NodeError (node:internal/errors:387:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:439:9)
at packageExportsResolve (node:internal/modules/esm/resolve:663:7)
at resolveExports (node:internal/modules/cjs/loader:493:36)
at Module._findPath (node:internal/modules/cjs/loader:533:31)
at Module._resolveFilename (node:internal/modules/cjs/loader:942:27)
at Module._load (node:internal/modules/cjs/loader:804:27)
at Module.require (node:internal/modules/cjs/loader:1022:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (/Users/steve/sites/dfxswap.io/vite.config.js:83:41)

Could not read from file: light\node_modules\node-stdlib-browser\esm\mock\empty.js\promises

 VITE v4.1.3  ready in 1590 msLocal:   http://localhost:4000/blitz/admin/lightNetwork: use --host to exposeInspect: http://localhost:4000/blitz/admin/light/blitz/admin/light/__inspect/press h to show help
✘ [ERROR] Could not read from file: D:\WWW\blitz-admin-web-light\node_modules\node-stdlib-browser\esm\mock\empty.js\promises

    node_modules/rollup/dist/es/rollup.js:16:7:
      16import 'node:fs/promises';
         ╵        ~~~~~~~~~~~~~~~~~~

D:\WWW\blitz-admin-web-light\node_modules\esbuild\lib\main.js:1604
  let error = new Error(`${text}${summary}`);
              ^

Error: Build failed with 1 error:
node_modules/rollup/dist/es/rollup.js:16:7: ERROR: Could not read from file: D:\WWW\blitz-admin-web-light\node_modules\node-stdlib-browser\esm\mock\empty.js\promises
    at failureErrorWithLog (D:\WWW\blitz-admin-web-light\node_modules\esbuild\lib\main.js:1604:15)
    at D:\WWW\blitz-admin-web-light\node_modules\esbuild\lib\main.js:1056:28
    at runOnEndCallbacks (D:\WWW\blitz-admin-web-light\node_modules\esbuild\lib\main.js:1476:61)
    at buildResponseToResult (D:\WWW\blitz-admin-web-light\node_modules\esbuild\lib\main.js:1054:7)
    at D:\WWW\blitz-admin-web-light\node_modules\esbuild\lib\main.js:1166:14
    at responseCallbacks.<computed> (D:\WWW\blitz-admin-web-light\node_modules\esbuild\lib\main.js:701:9)
    at handleIncomingPacket (D:\WWW\blitz-admin-web-light\node_modules\esbuild\lib\main.js:756:9)
    at Socket.readFromStdout (D:\WWW\blitz-admin-web-light\node_modules\esbuild\lib\main.js:677:7)
    at Socket.emit (node:events:512:28)
    at addChunk (node:internal/streams/readable:324:12) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: {
        column: 7,
        file: 'node_modules/rollup/dist/es/rollup.js',
        length: 18,
        line: 16,
        lineText: "import 'node:fs/promises';",
        namespace: '',
        suggestion: ''
      },
      notes: [],
      pluginName: '',
      text: 'Could not read from file: D:\\WWW\\blitz-admin-web-light\\node_modules\\node-stdlib-browser\\esm\\mock\\empty.js\\promises'
    }
  ],
  warnings: []
}

Node.js v19.6.0
error Command failed with exit code 1.

events.js does not provide an export named 'default'

I am trying to use this plugin along with vitest and it's causing following errors for the events polyfill.

SyntaxError: The requested module '/@fs/..../node_modules/events/events.js?v=f72cd9a9' does not provide an export named 'default'

Here are my configurations

export default defineConfig({
  plugins: [
    topLevelAwait(),
    nodePolyfills({
      include: ["buffer", "process", "util", "string_decoder", "url", "querystring"],
      globals: {Buffer: true, process: true},
      protocolImports: true,
    }),
  ],
});

Events package resolved to following:

And environment details are following:

  System:
    OS: macOS 14.0
    CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
    Memory: 212.27 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.7.0 - ~/.asdf/installs/nodejs/20.7.0/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/20.7.0/bin/yarn
    npm: 10.1.0 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Brave Browser: 118.1.59.120
    Chrome: 118.0.5993.88
    Firefox: 112.0.1
    Safari: 17.0

Vite v4.x Support?

Found: [email protected]
npm ERR! node_modules/vite
npm ERR! vite@"^4.0.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer vite@"^2.0.0 || ^3.0.0" from [email protected]

Would it be possible to support Vite v4.x in this plugin, please?

Importing fs returns a null object.

For some reason, importing node:fs returns a null object, any other polyfill works..?

import path from 'node:path'; // {resolve: ƒ, normalize: ƒ, isAbsolute: ƒ, join: ƒ, relative: ƒ, …} 
import http from 'node:http'; // {request: ƒ, get: ƒ, ClientRequest: ƒ, IncomingMessage: ƒ, Agent: ƒ, …}
import fs from 'node:fs'; // null

So, what could've gone wrong?
I'm not excluding the fs by the way~, Here is my vite.config.js:

// vite.config.js
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';

export default defineConfig({
    plugins: [
        nodePolyfills({
            globals: {
                Buffer: true, 
                global: true, 
                process: true, 
            },
            protocolImports: true,
        }),
    ]
});

Uncaught TypeError: Uint8Array is undefined

After upgrading from v0.9.0 to v0.11.3, the page provided by the vite dev server (v4.4.7) crashes because of the following error:

This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support.
Uncaught TypeError: Uint8Array is undefined

However, Buffer.TYPED_ARRAY_SUPPORT yields true in the Browser console and when running the following function directly (extracted from the source), it also returns true:

function typedArraySupport() {
  try {
    const arr = new Uint8Array(1);
    const proto = { foo: function() {
      return 42;
    } };
    Object.setPrototypeOf(proto, Uint8Array.prototype);
    Object.setPrototypeOf(arr, proto);
    return arr.foo() === 42;
  } catch (e) {
    return false;
  }
}

The code which crashes is marked to be from the following module:

node_modules/.pnpm/[email protected]/node_modules/buffer/index.js

readBigUInt64BE function not being implemented?

Hi, im using the combination of vue and vuetify, with Vite 4.3.9,
I'm simply just import buffer function with nodePolyfills define inside of the export default part of my vite.config.ts
everything works like a charm except the readBigUInt64BE being report as "not a function", so I quickly search through the code, and found out there's no match of readBigUInt64BE inside of this node modules, is it not being implemented yet or I did some wrong with my code?
thanks!

I have this Error

i have msg error : Uncaught SyntaxError: ambiguous indirect export: promises , when using fs.. what should i do ? i still dont understand about the error msg

Support Vite 5

Add support for Vite 5.

Current configuration:

"peerDependencies": {
    "vite": "^2.0.0 || ^3.0.0 || ^4.0.0"
},

In the meantime, for a temporary workaround (in your own package.json):

"overrides": {
    "vite": "5.0.0"
}

v0.11.0 in Windows has error

v0.11.0 has Error, But v0.9.0 is OK

[plugin:vite:import-analysis] Failed to resolve import "D:EnterpriseTaihe�ite-quasar
ode_modules�ite-plugin-node-polyfillsshimsdistindex.js" from "src\main.ts". Does the file exist?
D:/Enterprise/Taihe/vite-quasar/src/main.ts:1:43
1  |  import { Buffer as BufferPolyfill } from 'D:\Enterprise\Taihe\vite-quasar\node_modules\vite-plugin-node-polyfills\shims\dist\index.js'
   |                                            ^
2  |  window.Buffer = BufferPolyfill
3  |  import { global as globalPolyfill } from 'D:\Enterprise\Taihe\vite-quasar\node_modules\

Screenshot of Vite + Vue + TS

Can this work with fs/promises?

vite: 4.4.0
vite-plugin-node-polyfills: 0.9.0

I stumbled across this Plugin while trying to solve a "Failed to resolve entry for package "fs"" error and this looks promising. However while this Plugin does fix that first error, I then get the following error. Am I doing something wrong, or is this use case not covered?

image

image

Large performance impact on vitest on latest version

I was updating my dependencies and noticed a large performance decrease when running our test suite. A regular run time is 5 minutes, but it became 30 minutes after dependency updates.

I tracked it down to this package when I updated from 0.14.1 to 0.16.0.
I haven't been able to verify the exact cause but it appears to cause a massive slowdown in resolution/loading of modules.

I noted that it took multiple times longer for the first test file to even start executing and there were numerous times in the test run when no tests at all were executing as it was stuck loading/resolving.

This is not a big deal as there's no pressing need to update but thought it might be helpful to know.

Nuxt3 - Cannot read properties of null & fs

Hi! I read I should post this in Discussions but I couldn't find the tab, sorry!

I have this error, so I did bun install -D vite-plugin-node-polyfill and thought that would import fs correctly. I'm still getting this issue where my Nuxtjs3 web app, upon refreshing the page, shows a brief flash of everything being loaded then showing the 500: Cannot read properties of null (reading '__esModule') error pointing at the import fs from 'fs' line.

I'm not sure if I imported the plugin correctly into my nuxt.config.ts file. I also currently don't have a /plugins directory. I'm using node v18.0.0 and bun as my package manager.

Any help or ideas is much appreciated! Thank you!

Screenshot 2023-10-20 at 5 37 15 PM Screenshot 2023-10-20 at 5 38 02 PM Screenshot 2023-10-20 at 5 38 24 PM

Currently with import fs from 'fs':
Screenshot 2023-10-20 at 5 44 10 PM

If I change the import line to import * as fs from 'fs':
Screenshot 2023-10-20 at 5 45 17 PM

If I change the import line to const fs = require('fs'):
Screenshot 2023-10-20 at 5 43 30 PM

Writable.toWeb is not a function

I am trying to import and work with Writable from the library stream and it says it is not a function

import {Writable} from "node:stream"; 
await readableStream?.pipeTo(Writable.toWeb(writeStream));

here is my vite.config.js :

import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'
export default defineConfig({
  plugins: [
    nodePolyfills({
      overrides: {
        fs: 'memfs',
      },
    }),
  ],
})

ps: i have tried to override as well for stream and got this issue : GET http://URL.... 504 (Outdated Optimize Dep)

v0.11.0 - Uncaught SyntaxError Buffer

Uncaught SyntaxError: The requested module

'/@fs/home/dev/repo/node_modules/.pnpm/[email protected]/node_modules/buffer/index.js?v=0b1a4bd8' does not provide an export named 'Buffer' (at index.js?v=0b1a4bd8:1:10)

9,10,etc worked before.

I create a module which uses buffer, build it and use it in a vite app. Everything is built with vite, including other packages in turbo repo.

In v0.11.0 the resulting library has this:

import { Buffer as BufferPolyfill } from '/home/dev/repo/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-plugin-node-polyfills/shims/dist/index.js'
window.Buffer = BufferPolyfill
import { global as globalPolyfill } from '/home/dev/repo/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-plugin-node-polyfills/shims/dist/index.js'
window.global = globalPolyfill
import { process as processPolyfill } from '/home/dev/repo/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-plugin-node-polyfills/shims/dist/index.js'
window.process = processPolyfill

In v0.10.0 the resulting library has this:

/*!
 * The buffer module from node.js, for the browser.
 *
 * @author   Feross Aboukhadijeh <https://feross.org>
 * @license  MIT
 */
(function(e) {
  const a = Ca, t = ar, n = typeof Symbol == "function" && typeof Symbol.for == "function" ? Symbol.for("nodejs.util.inspect.custom") : null;
  e.Buffer = i, e.SlowBuffer = P, e.INSPECT_MAX_BYTES = 50;
  const l = 2147483647;
  e.kMaxLength = l, i.TYPED_ARRAY_SUPPORT = d(), !i.TYPED_ARRAY_SUPPORT && typeof console < "u" && typeof console.error == "function" && console.error(
    "This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support."
  );
  function d() {
    try {
      const s = new Uint8Array(1), r = { foo: function() {
        return 42;
      } };
      return Object.setPrototypeOf(r, Uint8Array.prototype), Object.setPrototypeOf(s, r), s.foo() === 42;
    } catch {
      return !1;
    }
  }

My vite config;

import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';

import path from 'path';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [nodePolyfills()],
  resolve: {
    alias: {
      '@/': new URL('./src/', import.meta.url).pathname,
    },
  },
  build: {
    target: 'esnext',
    lib: {
      entry: path.resolve(__dirname, 'src/index.ts'),
      formats: ['cjs', 'es'],
      fileName: (format) => `index.${format}.js`,
    },
  },
});

Bundling Bloat

I was so excited to find this plugin! This is my first issue ever on GitHub, so I'm sure I'm doing it wrong, but here we go:

While it seems to work, this plugin unfortunately also seems to break Vite's efficient bundling, as my bundles with this plugin seem to be 25% to 120% larger than their pre-plugin counterparts.

Is this expected / are others experiencing this?

Vite 4.0.4 and latest polyfills

Hi,
I get the following error when I try this plugin with vite.

✘ [ERROR] The injected path "/Users/mbichler/dev/resolute/frontend_react/node_modules/node-stdlib-browser/helpers/esbuild/shim.js" cannot be marked as external

CleanShot 2023-01-09 at 11 54 29

rollup-plugin-inject is deprecated, rollup-plugin-node-polyfills is unmaintained

ionic-team/rollup-plugin-node-polyfills#32

the repo seems to be dead

remorses/esbuild-plugins#18

@FredKSchott's fork is also recommended by the @rollup team: https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency

cloudflare/workers-sdk#1232

Perhaps we could switch to https://github.com/niksy/node-stdlib-browser instead of @esbuild-plugins/node-modules-polyfill?

alternative:
https://github.com/sodatea/vite-plugin-node-stdlib-browser
... "just works" : )

Plugin does not work with sentry plugin

Hello,

I hat an issue get this plugin working with a sentry vite plugin, as described here: rollup/rollup-plugin-commonjs#327

I did the following

Environment
Linux with Node 18

Steps to Reproduce
Install nuxt 3 base
Use vite
Use vite plugin with vite-plugin-node-polyfills
Use sentry Plugin
"nuxt build" fails
Does not happen, when using polyfills or sentry separately.

Expected Result
build success

Actual Result
ERROR "sentry-release-injection-file" is imported as an external by "browserify-sign/algos.js?commonjs-external", but is already an existing non-external module id. 16:08:04

ERROR "sentry-release-injection-file" is imported as an external by "browserify-sign/algos.js?commonjs-external", but is already an existing non-external module id. 16:08:04

at error (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:2245:30)

As stated in the answer, sentry advised to take a look at rollup/rollup-plugin-commonjs#327 which provides a similar solution which may fit in for vite-plugin-node-polyfills as well.

The plugin breaks nuxi build

Environment


  • Operating System: Linux
  • Node Version: v18.17.0
  • Nuxt Version: 3.7.1
  • CLI Version: 3.8.0
  • Nitro Version: 2.6.3
  • Package Manager: [email protected]
  • Builder: -
  • User Config: vite
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://github.com/learntheropes/github-anc6tm
https://stackblitz.com/edit/github-anc6tm
But you need to build it.

Describe the bug

In a Nuxt 3 application, vite.config.js is replaced in nuxt.config.js as follows.

import { nodePolyfills } from 'vite-plugin-node-polyfills';

export default defineNuxtConfig({
  vite: {
    plugins: [
      nodePolyfills({
        include: ['buffer', 'util', 'stream', 'crypto'],
      }),
    ],
  },
});

However, the plguin breaks nuxi build as follows :

(inject plugin) rollup-plugin-inject: failed to parse /home/giovanni/Documents/Dev/github-anc6tm/.nuxt/dist/server/server.mjs. Consider restricting the plugin to particular files via options.include

 ERROR  RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)                                                                                      nitro 7:53:52 PM


229:   }
230:   const head = inject(headSymbol);
231:   if (!head && dist.process.env.NODE_ENV !== "production")
                         ^
232:     console.warn("Unhead is missing Vue context, falling back to shared context. This may have unexpected results.");
233:   return head || getActiveHead();


 ERROR  Unexpected token (Note that you need plugins to import files that are not JavaScript)                                                                                                         7:53:52 PM

  at error (node_modules/rollup/dist/es/shared/node-entry.js:2287:30)
  at Module.error (node_modules/rollup/dist/es/shared/node-entry.js:13726:16)
  at Module.tryParse (node_modules/rollup/dist/es/shared/node-entry.js:14457:25)
  at Module.setSource (node_modules/rollup/dist/es/shared/node-entry.js:14058:39)
  at ModuleLoader.addModuleSource (node_modules/rollup/dist/es/shared/node-entry.js:24623:20) 



 ERROR  Unexpected token (Note that you need plugins to import files that are not JavaScript) 

Accordingly to Nuxt devs, is a plugin issue: nuxt/nuxt#23091 (comment)

cause build slow

bufore using this extension, build cost: 2m 10s
after: 3m 38s

before config:

const getNodePolyfill = (module: string) => {
    return {
        find: module,
        replacement: resolveNodeModules('rollup-plugin-node-polyfills/polyfills/', module),
    };
};
const config = {
    resolve: {
            alias: [
                // ppt 导出能力依赖
                getNodePolyfill('stream'),
                getNodePolyfill('os'),
                getNodePolyfill('util'),
            ]
    }
}

after:

{
    plugins: [
        nodePolyfills({
            // Whether to polyfill `node:` protocol imports.
            protocolImports: true,
        }),
    ],
}

Versions:

System:
    OS: macOS 13.2.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 3.03 GB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.19.1 - ~/Library/Caches/fnm_multishells/58258_1676951390481/bin/node
    npm: 8.19.3 - ~/Library/Caches/fnm_multishells/58258_1676951390481/bin/npm
  Browsers:
    Chrome: 110.0.5481.100
    Chrome Canary: 112.0.5608.0
    Firefox: 109.0.1
    Firefox Developer Edition: 111.0
    Safari: 16.3
  npmPackages:
    rollup: ^3.17.2 => 3.17.2 
    vite: ^4.1.2 => 4.1.2 

Require is not wrong, but it seems that there is no effect

error:

index.js:8 Uncaught TypeError: Cannot read properties of null (reading 'existsSync')
    at getElectronPath (index.js:8:10)
    at node_modules/.pnpm/registry.npmmirror.com+electron@23.0.0-beta.8/node_modules/electron/index.js (index.js:21:18)
    at __require2 (chunk-SR54VZQL.js?v=2c462924:18:50)
    at electron.js?v=2c462924:7:16

index.js from electron node_module:

const fs = require('fs');
const path = require('path');

const pathFile = path.join(__dirname, 'path.txt');

function getElectronPath () {
  let executablePath;
  if (fs.existsSync(pathFile)) {
    executablePath = fs.readFileSync(pathFile, 'utf-8');
  }
  if (process.env.ELECTRON_OVERRIDE_DIST_PATH) {
    return path.join(process.env.ELECTRON_OVERRIDE_DIST_PATH, executablePath || 'electron');
  }
  if (executablePath) {
    return path.join(__dirname, 'dist', executablePath);
  } else {
    throw new Error('Electron failed to install correctly, please delete node_modules/electron and try installing again');
  }
}

module.exports = getElectronPath();

env:

"vite": "^4.1.1",
"vite-plugin-node-polyfills": "^0.7.0",
"electron": "23.0.0-beta.8",

Sveltekit builds?

I was using this plugin with a svelte vite project, which uses some commonJs modules that import Buffer and process.

This plugin works fine in dev mode, but fails in build.
To make this plugin work, I forced dependency optimization on and and made vite skip the commonJs transforms.

optimizeDeps: {
  disabled: false,
},
build: {
  commonjsOptions: {
    include: [],
  }
}

I'm not really sure why this worked, but sveltekit doesn't support forcing dependency optimization on. Is there a better/proper way that I can make the polyfills available to non-optimized commonJs dependencies?

Failed to load url /node_modules/path-browserify/posix

Reproduction: https://stackblitz.com/edit/vitejs-vite-1axycb?file=main.js
Its currently possible to do import { join } from 'path' or import { posix } from 'path' but not these:

import { join } from 'path/posix';
import { join } from 'node:path/posix';

Error:

Failed to load url /node_modules/path-browserify/posix (resolved id: /home/projects/vitejs-vite-1axycb/node_modules/path-browserify/posix) in /home/projects/vitejs-vite-1axycb/main.js. Does the file exist?

(Not sure if that is the correct repo to report it to or https://github.com/niksy/node-stdlib-browser)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.