APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
952 stars 227 forks source link

Can't using in Vite2 #257

Closed Innei closed 1 year ago

Innei commented 2 years ago

util is node built-in lib, but not in browser. And vite has no polyfill of node util.

> ../../node_modules/.pnpm/@jsdevtools+ono@7.1.3/node_modules/@jsdevtools/ono/esm/types.js:1:9: error: No matching export in "browser-external:util" for import "inspect"
    1 │ import { inspect } from "util";
      ╵          ~~~~~~~

8:08:43 PM [vite] error while updating dependencies:
Error: Build failed with 1 error:
../../node_modules/.pnpm/@jsdevtools+ono@7.1.3/node_modules/@jsdevtools/ono/esm/types.js:1:9: error: No matching export in "browser-external:util" for import "inspect"
    at failureErrorWithLog (/Users/xiaoxun/github/playground/components-playgound/node_modules/.pnpm/esbuild@0.13.15/node_modules/esbuild/lib/main.js:1493:15)
    at /Users/xiaoxun/github/playground/components-playgound/node_modules/.pnpm/esbuild@0.13.15/node_modules/esbuild/lib/main.js:1151:28
    at runOnEndCallbacks (/Users/xiaoxun/github/playground/components-playgound/node_modules/.pnpm/esbuild@0.13.15/node_modules/esbuild/lib/main.js:941:63)
    at buildResponseToResult (/Users/xiaoxun/github/playground/components-playgound/node_modules/.pnpm/esbuild@0.13.15/node_modules/esbuild/lib/main.js:1149:7)
    at /Users/xiaoxun/github/playground/components-playgound/node_modules/.pnpm/esbuild@0.13.15/node_modules/esbuild/lib/main.js:1258:14
    at /Users/xiaoxun/github/playground/components-playgound/node_modules/.pnpm/esbuild@0.13.15/node_modules/esbuild/lib/main.js:629:9
    at handleIncomingPacket (/Users/xiaoxun/github/playground/components-playgound/node_modules/.pnpm/esbuild@0.13.15/node_modules/esbuild/lib/main.js:726:9)
    at Socket.readFromStdout (/Users/xiaoxun/github/playground/components-playgound/node_modules/.pnpm/esbuild@0.13.15/node_modules/esbuild/lib/main.js:596:7)
    at Socket.emit (node:events:520:28)
    at addChunk (node:internal/streams/readable:315:12)
cxyb commented 2 years ago

this is config you should use

import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'; import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'; import rollupNodePolyFill from 'rollup-plugin-node-polyfills' // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], resolve: { alias: { // This Rollup aliases are extracted from @esbuild-plugins/node-modules-polyfill, // see https://github.com/remorses/esbuild-plugins/blob/master/node-modules-polyfill/src/polyfills.ts // process and buffer are excluded because already managed // by node-globals-polyfill util: 'rollup-plugin-node-polyfills/polyfills/util', sys: 'util', events: 'rollup-plugin-node-polyfills/polyfills/events', stream: 'rollup-plugin-node-polyfills/polyfills/stream', path: 'rollup-plugin-node-polyfills/polyfills/path', querystring: 'rollup-plugin-node-polyfills/polyfills/qs', punycode: 'rollup-plugin-node-polyfills/polyfills/punycode', url: 'rollup-plugin-node-polyfills/polyfills/url', string_decoder: 'rollup-plugin-node-polyfills/polyfills/string-decoder', http: 'rollup-plugin-node-polyfills/polyfills/http', https: 'rollup-plugin-node-polyfills/polyfills/http', os: 'rollup-plugin-node-polyfills/polyfills/os', assert: 'rollup-plugin-node-polyfills/polyfills/assert', constants: 'rollup-plugin-node-polyfills/polyfills/constants', _stream_duplex: 'rollup-plugin-node-polyfills/polyfills/readable-stream/duplex', _stream_passthrough: 'rollup-plugin-node-polyfills/polyfills/readable-stream/passthrough', _stream_readable: 'rollup-plugin-node-polyfills/polyfills/readable-stream/readable', _stream_writable: 'rollup-plugin-node-polyfills/polyfills/readable-stream/writable', _stream_transform: 'rollup-plugin-node-polyfills/polyfills/readable-stream/transform', timers: 'rollup-plugin-node-polyfills/polyfills/timers', console: 'rollup-plugin-node-polyfills/polyfills/console', vm: 'rollup-plugin-node-polyfills/polyfills/vm', zlib: 'rollup-plugin-node-polyfills/polyfills/zlib', tty: 'rollup-plugin-node-polyfills/polyfills/tty', domain: 'rollup-plugin-node-polyfills/polyfills/domain' } }, optimizeDeps: { esbuildOptions: { // Node.js global to browser globalThis define: { global: 'globalThis' }, // Enable esbuild polyfill plugins plugins: [ NodeGlobalsPolyfillPlugin({ process: true, buffer: true }), NodeModulesPolyfillPlugin() ] } }, build: { rollupOptions: { plugins: [ // Enable rollup polyfills plugin // used during production bundling rollupNodePolyFill() ] } } }) But with this it is working in dev but production build fails to parse JSON, any ideas?

Innei commented 2 years ago

@cxyb I found a way, copy util polyfill as file util.js in polyfills folder, and link it as a package in package.json, like below.

"dependencies": {
"util": "./polyfills/util.js"
// ....
}
bluwy commented 2 years ago

I found a fix like so:

# Shim util module
npm i -D util

Vite will auto pick this up without resolve.alias.

In index.html add:

<script>
  globalThis.global = globalThis;
  globalThis.process = { env: {} };
</script>

in body to shim global and process.env


There's 3 issues here:

  1. util shim should not be needed. It's imported by @jsdevtools/ono but it has a browser field that should avoid it in the first place. Related issue: https://github.com/vitejs/vite/issues/7576
  2. global is used by call-me-maybe dep
  3. process.env is used by util (ironically)

1 and 3 is related and should be resolved when that issue is resolved.

Related discussion: https://github.com/vitejs/vite/discussions/8549

philsturgeon commented 1 year ago

I don't know what Vite is but if #275 hasn't helped please send pull requests.

MaciejDybowski commented 11 months ago

@philsturgeon did you resolve problem with Vite configuration?