forge: webview’s preload script cannot be found in production

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?

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 3
  • Comments: 15 (5 by maintainers)

Most upvoted comments

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.