Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't using in Vite2 #257

Closed
Innei opened this issue Feb 19, 2022 · 5 comments
Closed

Can't using in Vite2 #257

Innei opened this issue Feb 19, 2022 · 5 comments

Comments

@Innei
Copy link

Innei commented Feb 19, 2022

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
Copy link

cxyb commented Mar 10, 2022

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
Copy link
Author

Innei commented Mar 12, 2022

@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
Copy link

bluwy commented Jun 17, 2022

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: vite does not handle "browser" field in package.json properly, when using package 'tapable' vitejs/vite#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: vitejs/vite#8549

@philsturgeon
Copy link
Member

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

@MaciejDybowski
Copy link

@philsturgeon did you resolve problem with Vite configuration?

thienandangthanh added a commit to thienandangthanh/wifi-connect that referenced this issue Nov 7, 2023
thienandangthanh added a commit to thienandangthanh/wifi-connect that referenced this issue Nov 7, 2023
* Init new vite project

* Add rendition 35.1.0

* Add browserslist-to-esbuild

* Add browsersListToEsbuild config

* Add styled-components 6.1.0

* Add whatwg-fetch 3.6.19

* Add husky and lint-staged

* Add @types/json-schema

* Add promise-polyfill

* Copy src from old ui

- copy src directory
- move public/index.html to index.html
- remove %PUBLIC_URL%
- add module script to the bottom of the body tag

* Use util instead of Node.js util

Reference: APIDevTools/json-schema-ref-parser#257 (comment)

* Add `start` script to run as old ui

* Set default port to 3000 and auto open browser

* Remove redundant vite.svg logo

* Use `build` instead of `dist` as build output

Reason: to match create-react-app output

* Only output sourcemap in development mode

* Use eslint instead of @balena/lint with lintstaged

* Rename project and version

* Downgrade husky to v4, lint-staged to v10

* Replace old ui with new vite ui

Reason: %PUBLIC_URL% is not used with vite
thienandangthanh added a commit to thienandangthanh/wifi-connect that referenced this issue Nov 7, 2023
* Init new vite project

* Add rendition 35.1.0

* Add browserslist-to-esbuild

* Add browsersListToEsbuild config

* Add styled-components 6.1.0

* Add whatwg-fetch 3.6.19

* Add husky and lint-staged

* Add @types/json-schema

* Add promise-polyfill

* Copy src from old ui

- copy src directory
- move public/index.html to index.html
- remove %PUBLIC_URL%
- add module script to the bottom of the body tag

* Use util instead of Node.js util

Reference: APIDevTools/json-schema-ref-parser#257 (comment)

* Add `start` script to run as old ui

* Set default port to 3000 and auto open browser

* Remove redundant vite.svg logo

* Use `build` instead of `dist` as build output

Reason: to match create-react-app output

* Only output sourcemap in development mode

* Use eslint instead of @balena/lint with lintstaged

* Rename project and version

* Downgrade husky to v4, lint-staged to v10

* Replace old ui with new vite ui
thienandangthanh added a commit to thienandangthanh/wifi-connect that referenced this issue Nov 7, 2023
* Init new vite project

* Add rendition 35.1.0

* Add browserslist-to-esbuild

* Add browsersListToEsbuild config

* Add styled-components 6.1.0

* Add whatwg-fetch 3.6.19

* Add husky and lint-staged

* Add @types/json-schema

* Add promise-polyfill

* Copy src from old ui

- copy src directory
- move public/index.html to index.html
- remove %PUBLIC_URL%
- add module script to the bottom of the body tag

* Use util instead of Node.js util

Reference: APIDevTools/json-schema-ref-parser#257 (comment)

* Add `start` script to run as old ui

* Set default port to 3000 and auto open browser

* Remove redundant vite.svg logo

* Use `build` instead of `dist` as build output

Reason: to match create-react-app output

* Only output sourcemap in development mode

* Use eslint instead of @balena/lint with lintstaged

* Rename project and version

* Downgrade husky to v4, lint-staged to v10

* Replace old ui with new vite ui
thienandangthanh added a commit to thienandangthanh/wifi-connect that referenced this issue Nov 8, 2023
* Init new vite project

* Add rendition 35.1.0

* Add browserslist-to-esbuild

* Add browsersListToEsbuild config

* Add styled-components 6.1.0

* Add whatwg-fetch 3.6.19

* Add husky and lint-staged

* Add @types/json-schema

* Add promise-polyfill

* Copy src from old ui

- copy src directory
- move public/index.html to index.html
- remove %PUBLIC_URL%
- add module script to the bottom of the body tag

* Use util instead of Node.js util

Reference: APIDevTools/json-schema-ref-parser#257 (comment)

* Add `start` script to run as old ui

* Set default port to 3000 and auto open browser

* Remove redundant vite.svg logo

* Use `build` instead of `dist` as build output

Reason: to match create-react-app output

* Only output sourcemap in development mode

* Use eslint instead of @balena/lint with lintstaged

* Rename project and version

* Downgrade husky to v4, lint-staged to v10

* Replace old ui with new vite ui

* Revert README changes

* Remove redundant HTML comment

Reason: %PUBLIC_URL% is not used with vite

* Place generated assets inside the `static/assets`

Reason: to serve with wifi-connect binary

* Only run eslint fix with ts, tsx file with husky

* Replace CRA with Vite in README
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants