Closed astoilkov closed 2 years ago
Can you provide a minimal reproduction Demo?
If you use Node.js API or relted NPM package in the Electron-Renderer, you need to configure the renderer
option of the plugin.
Got it. I will check your suggestion and write you back. If that's not the fix I will make a reproduction project.
I'm using vite-plugin-electron-renderer
only (I'm building the main process using my own Vite config). The dev works, only build doesn't. It's not the renderer
option.
I can't seem to isolate it in a separate package. I'll keep trying.
Meanwhile, do you have any other ideas?
Ooo, I just saw something that may be useful. I think it's once again a Web Worker issue:
Here it's the entire log:
vite v3.1.3 building for production...
✓ 266 modules transformed.
✓ 2071 modules transformed.
[vite:worker-import-meta-url] 'sep' is not exported by __vite-browser-external, imported by app/local_modules/path-normalize/index.ts
file: <repo>/app/components/Main/effects/useWorkspaceWatcher.ts:1:9
1: import { sep } from 'path'
^
2:
3: // #performance
file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:1858
base = Object.assign(new Error(base.message), base);
^
Error: 'sep' is not exported by __vite-browser-external, imported by app/local_modules/path-normalize/index.ts
at error (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:1858:30)
at Module.error (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:12429:16)
at Module.traceVariable (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:12788:29)
at ModuleScope.findVariable (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:11440:39)
at FunctionScope.findVariable (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:6372:38)
at ChildScope.findVariable (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:6372:38)
at Identifier.bind (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:7439:40)
at CallExpression.bind (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
at CallExpression.bind (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:8935:15)
at IfStatement.bind (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
at BlockStatement.bind (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
at FunctionDeclaration.bind (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
at ExportDefaultDeclaration.bind (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
at Program.bind (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
at Module.bindReferences (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:12425:18)
at Graph.sortModules (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:23024:20)
at Graph.build (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:22902:14)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:23689:13
at async catchUnfinishedHookActions (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:23088:20)
at async rollupInternal (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:23686:5)
at async bundleWorkerEntry (file://<repo>/node_modules/vite/dist/node/chunks/dep-557f29e6.js:38168:20)
at async workerFileToUrl (file://<repo>/node_modules/vite/dist/node/chunks/dep-557f29e6.js:38260:29)
at async Object.transform (file://<repo>/node_modules/vite/dist/node/chunks/dep-557f29e6.js:40167:31)
at async transform (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:21928:16)
at async ModuleLoader.addModuleSource (file://<repo>/node_modules/rollup/dist/es/shared/rollup.js:22153:30) {
code: 'PLUGIN_ERROR',
url: 'https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module',
id: '<repo>/app/components/Main/effects/useWorkspaceWatcher.ts',
pos: 9,
loc: {
column: 9,
file: '<repo>/app/local_modules/path-normalize/index.ts',
line: 1
},
frame: "1: import { sep } from 'path'\n ^\n2: \n3: // #performance",
watchFiles: [
... 2300 more items
],
pluginCode: 'MISSING_EXPORT',
plugin: 'vite:worker-import-meta-url',
hook: 'transform'
}
Here is a minimal reproduction: https://github.com/astoilkov/electron-vite-worker-issue
@astoilkov 👋 I figured it out!
Now, you can add the following worker
option to solve you problem. 🚀
vite-plugin-electron
will fix this BUG in next version. 🌱
import { defineConfig } from 'vite'
+ import { builtinModules } from 'module'
import renderer from './plugins'
import pkg from '../../package.json'
import resolve from 'vite-plugin-resolve'
// https://vitejs.dev/config/
export default defineConfig({
root: __dirname,
mode: process.env.NODE_ENV,
base: './',
plugins: [
// 🚧 Avoid export `ipcRenderer`
resolve({
electron: `export default require('electron');`,
}).map(plugin => Object.assign(plugin, { enforce: 'pre' })),
// Support use Node.js API in Electron-Renderer
// @see - https://github.com/electron-vite/vite-plugin-electron-renderer
renderer(),
],
build: {
outDir: '../../dist/renderer',
emptyOutDir: true,
sourcemap: true,
},
+ worker: {
+ rollupOptions: {
+ external: [
+ 'electron',
+ ...builtinModules,
+ ],
+ },
+ },
server: process.env.VSCODE_DEBUG ? {
host: pkg.debug.env.VITE_DEV_SERVER_HOSTNAME,
port: pkg.debug.env.VITE_DEV_SERVER_PORT,
} : undefined,
})
Awesome! Thanks, @caoxiemeihao.
Do you know why this shows up?
Can I silence it? Can it cause an issue somewhere?
Yes, it seems like it's an issue. When running the app the following error is thrown:
Do you know why this shows up?
This is how to appear?
Sorry, I didn't understand you. It shows after the Vite build is complete. Do you need reproducible demo?
Here is a minimal reproduction: https://github.com/astoilkov/electron-vite-worker-issue
Can this be reproduced?
No, I can't replicate it there. Hmm. Again, I will need to investigate.
Do you know why it might be guessing require$$1
. In the minimal reproduction, it's correctly guessing path
. Do you know where the code for that is in Vite and what's the logic there (that might point me to where to look)?
Done. I've replicated the problem — https://github.com/astoilkov/electron-vite-worker-issue.
Run yarn run build
and you will see this:
This is causing an error when you run the app and click the "hello" text.
The issue is caused by:
graceful-fs
which is part of the array returned by renderer.resolve()
functiongraceful-fs
should be used in the worker thread in order for the problem to appear
I'm experiencing the error while doing
vite build
for production:It's strange. Other imports from the
path
module work.