davidmyersdev / vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments.
MIT License
263 stars 17 forks source link

v0.11.0 - Uncaught SyntaxError Buffer #31

Closed Icehunter closed 11 months ago

Icehunter commented 11 months ago

Uncaught SyntaxError: The requested module

'/@fs/home/dev/repo/node_modules/.pnpm/buffer@6.0.3/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/vite-plugin-node-polyfills@0.11.0_vite@4.4.9/node_modules/vite-plugin-node-polyfills/shims/dist/index.js'
window.Buffer = BufferPolyfill
import { global as globalPolyfill } from '/home/dev/repo/node_modules/.pnpm/vite-plugin-node-polyfills@0.11.0_vite@4.4.9/node_modules/vite-plugin-node-polyfills/shims/dist/index.js'
window.global = globalPolyfill
import { process as processPolyfill } from '/home/dev/repo/node_modules/.pnpm/vite-plugin-node-polyfills@0.11.0_vite@4.4.9/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`,
    },
  },
});
Icehunter commented 11 months ago

Specifically, in a production build there is no issue that I've seen as my site deploys. This happens when running in dev mode.

davidmyersdev commented 11 months ago

Duplicate of #29 (fixed in v0.11.1).