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

Does not work with Vite #354

Closed skamansam closed 1 month ago

skamansam commented 1 month ago

when I try to use it in the browser, with Vite, I get the following error in the console:

ReferenceError: process is not defined
    at node_modules/.pnpm/util@0.12.5/node_modules/util/util.js (@apidevtools_json-schema-ref-parser.js?v=5ead2acc:1809:5)
    at __require (chunk-WXXH56N5.js?v=5ead2acc:12:50)
    at node_modules/.pnpm/@jsdevtools+ono@7.1.3/node_modules/@jsdevtools/ono/esm/types.js (@apidevtools_json-schema-ref-parser.js?v=5ead2acc:2305:27)
    at __init (chunk-WXXH56N5.js?v=5ead2acc:9:56)
    at node_modules/.pnpm/@jsdevtools+ono@7.1.3/node_modules/@jsdevtools/ono/esm/index.js (@apidevtools_json-schema-ref-parser.js?v=5ead2acc:2321:5)
    at __init (chunk-WXXH56N5.js?v=5ead2acc:9:56)
    at node_modules/.pnpm/@apidevtools+json-schema-ref-parser@11.6.4/node_modules/@apidevtools/json-schema-ref-parser/dist/lib/refs.js (@apidevtools_json-schema-ref-parser.js?v=5ead2acc:3214:18)
    at __require (chunk-WXXH56N5.js?v=5ead2acc:12:50)
    at node_modules/.pnpm/@apidevtools+json-schema-ref-parser@11.6.4/node_modules/@apidevtools/json-schema-ref-parser/dist/lib/index.js (@apidevtools_json-schema-ref-parser.js?v=5ead2acc:7589:37)
    at __require (chunk-WXXH56N5.js?v=5ead2acc:12:50)

Vite doesn't use process.env, it uses import.meta. I'm not sure if this is in one of your libs, or an included lib, but regardless, this lib doesn't work with Vite.

syhner commented 1 month ago

https://github.com/APIDevTools/swagger-parser?tab=readme-ov-file#browser-support

To use Swagger Parser in a browser, you'll need to use a bundling tool such as Webpack, Rollup, Parcel, or Browserify. Some bundlers may require a bit of configuration, such as setting browser: true in rollup-plugin-resolve.

So you'll need to install the polyfills

pnpm add -D @esbuild-plugins/node-globals-polyfill @esbuild-plugins/node-modules-polyfill

And update your vite config

vite.config.ts

import { defineConfig } from 'vitest/config';

+ import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
+ import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill';

export default defineConfig({
  optimizeDeps: {
    esbuildOptions: {
      plugins: [
+         NodeGlobalsPolyfillPlugin({
+           process: true,
+           buffer: true
+         }),
+         NodeModulesPolyfillPlugin()
      ]
    }
  }
});
jonluca commented 1 month ago

Can you try v11.6.5?