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

Unexpected absolute import paths #33

Closed aryzing closed 10 months ago

aryzing commented 10 months ago

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, ```ts 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?

aryzing commented 10 months ago

Had a quick look at the plugin's source, could this banner be the issue?

https://github.com/davidmyersdev/vite-plugin-node-polyfills/blob/e70d94ff20fccfbfc8cf948d11b7a8224b139385/src/index.ts#L166-L171

ljy0jy commented 10 months ago

I got a same issues with you. import absolute path with build。

ljy0jy commented 10 months ago
 globals: {
    Buffer: true, // can also be 'build', 'dev', or false
    global: true,
    process: true,
  },

globals set false will solve then polyfills will invalid。 did you solve this problem?

davidmyersdev commented 10 months ago

This is fixed in v0.11.2.