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

Unable to "un-extern" native node modules from renderer #61

Closed
4 tasks done
ykrasik opened this issue Nov 18, 2022 · 4 comments
Closed
4 tasks done

Unable to "un-extern" native node modules from renderer #61

ykrasik opened this issue Nov 18, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@ykrasik
Copy link

ykrasik commented Nov 18, 2022

Describe the bug

I'm porting a project from webpack to electron-vite. Webpack was previously bundling this as an IIFE.

I ran into a case where one of my dependencies used in the renderer, not sure which one, was using the "utils" nodejs module transitively. I think it was pulling "utils" in through make-error-cause.

This would result in the following error from renderer, even though nodeIntegration: true and contextIsolation: false:
index.html:1 Uncaught TypeError: Failed to resolve module specifier "util". Relative references must start with either "/", "./", or "../".

I don't understand why this was happening, as from what I understand, turning on nodeIntegration: true should've given renderer access to "utils". Or was webpack bundling "utils" in the renderer before? Anyway, as I couldn't find a solution, I decided to polyfill it instead. What I found out is - you can't polyfill any node native modules, because they are marked as externals.

I was trying to polyfill them through an alias:

export default defineConfig({
  renderer: {
    resolve: {
      alias: {
        util: 'rollup-plugin-node-polyfills/polyfills/util',
      },
    },
  }
})

Also, the following had no effect - I was unable to override the default extern config for rollup:

import { IsExternal } from 'rollup'

const isExternal: IsExternal = id => {
  console.log(`Resolving: ${id}`)
  return false
}

export default defineConfig({
  renderer: {
    build: {
      rollupOpitions: {
        external: isExternal,
      },
    },
  }
})

I think that having an ability to un-extern node native modules, or replace the extern block with a function, is useful and currently missing.

P.S. If you could explain why this error appears with vite-electron but not with webpack, that would help a lot.

Electron-Vite Version

1.0.14

Electron Version

21.2.2

Vite Version

3.2.4

Validations

@ykrasik ykrasik added the bug Something isn't working label Nov 18, 2022
@alex8088
Copy link
Owner

By default, electron-vite currently does not support nodeIntegration in renderer. So can't use node module. Not sure whether your original development mode must enable node integration.
There are two ways:

  • use make-error instead of make-error-cause, make-error support browser.
  • use plugin vite-plugin-commonjs-externals to externalize make-error-cause( need turn on nodeIntegration)

I don't know if these can help.

@ykrasik
Copy link
Author

ykrasik commented Nov 18, 2022

About make-error - it's not my direct dependency, another lib that I use pulls it in.
So... the problem is that nodejs native modules must be required and not imported from renderer? That would at least explain it. That plugin didn't work for me, but I'm ok with a polyfill as its only the "util" module.

It would be nice to be able to disable the default externing of node builtins in renderer. Currently I solved it by applying a patch as part of yarn install:

package.json

"resolutions": {
  "electron-vite": "patch:electron-vite@1.0.14#.yarn/patches/electron-vite-1.0.14.patch"
},

.yarn/patches/electron-vite-1.0.14.patch

diff --git a/dist/chunks/lib-d0e65f8e.js b/dist/chunks/lib-d0e65f8e.js
index e0211a8ce..f2bdc0c14 100644
--- a/dist/chunks/lib-d0e65f8e.js
+++ b/dist/chunks/lib-d0e65f8e.js
@@ -361,1 +361,1 @@
-                            external: [...node_module.builtinModules.flatMap(m => [m, `node:${m}`])]
+                            // external: [...node_module.builtinModules.flatMap(m => [m, `node:${m}`])]

@alex8088
Copy link
Owner

@ykrasik Thanks. I didn't understand your idea correctly. This requirement seems reasonable, I will fix it.

@alex8088
Copy link
Owner

alex8088 commented Dec 4, 2022

new ver 1.0.15 is out!

@alex8088 alex8088 closed this as completed Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants