question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webview’s preload script cannot be found in production

See original GitHub issue

My preload.js file is in src/components/MyComponent folder. And my other file containing webview is like following:

 const getSourceDirectory = () =>
 process.env.NODE_ENV === "development"
 ? path.join(process.cwd(), "src/components/MyComponent") // or wherever your local build is compiled
 : path.join(process.resourcesPath) // asar location
 const preload = `file://${path.resolve(getSourceDirectory(), 'preload.js')}`

And I include it like:

<webview
	preload={preload}
	src="https://google.com"
/>

This works in development because I know the path. I can’t seem to find the path in production though.

And if I put the same path as development, i.e, "src/components/MyComponent" then it says Can’t find preload.js which is obvious.

How do I make this thing work in production with electron-forge?

I know the error is in the else condition of the ternary operator, i.e, path.join(process.resourcesPath) should be changed to something else but what should it be changed to?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
Kikketercommented, Mar 23, 2021

Using the “magic” variables worked for me as long as I used what @deadcoder0904 mentioned:

              "entryPoints": [
                {
                  "html": "./src/index.html",
                  "js": "./src/renderer.js",
                  "name": "main_window",
                  "preload": {
                    "js": "./src/preload.js"
                  }
                }
              ]

With the magic global variable:

    webPreferences: {
      preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
    },
  })

This isn’t documented as far as I can tell: https://www.electronforge.io/config/plugins/webpack#project-setup Hope this helps someone, this worked for me from the fresh “Webpack” variety starting point.

0reactions
george-thomas-hillcommented, May 15, 2022

I have the same problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

webview's preload script cannot be found in production #1590
My preload.js file is in src/components/MyComponent folder. And my other file containing webview is like following: const getSourceDirectory ...
Read more >
Preload not working/compiled correctly in production (Electron ...
To me it seems the issue is that your project structure is slightly wrong. The static directory is supposed to be located in...
Read more >
Breaking Changes | Electron
If your preload scripts do not depend on Node, no action is needed. If your preload scripts do depend on Node, either refactor...
Read more >
Link types: preload - HTML: HyperText Markup Language | MDN
The preload value of the element's rel attribute lets you declare fetch requests in the HTML's , specifying resources that your page will ......
Read more >
Build Options - Vite
build.modulePreload # ... Note: the polyfill does not apply to Library Mode. If you need to support browsers without native dynamic import, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found