-
-
Notifications
You must be signed in to change notification settings - Fork 531
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
Support webpack externals, modules listed as external should not be pruned during package #1250
Comments
@dustinstein - I'm seeing the same issue here. Did you make any progress on this? |
I'm almost embarrassed to say what I did but I was at the end of my expertise. I started a new project with electron-edge-js-quick-start and copied my main electron process code to that project. Since the electron-forge webpack was working well (before packaging or making) I built the project with electron-forge and used the compiled index.js file in .webpack/renderer/main_window folder in my electron-edge-js-quick-start project and it ran and packaged well. |
@dustinstein - |
This comment was marked as off-topic.
This comment was marked as off-topic.
I have been digging through the packger code for hours and found a solution by adding a few hooks. Basically we need to find out which packages got marked as externals in webpack, and run Sharing my forge config here: const fs = require('fs-extra');
const path = require('path');
const { spawn } = require('child_process');
module.exports = {
packagerConfig: {},
makers: [
{
name: '@electron-forge/maker-squirrel',
config: {
name: 'puppeteer_playground',
},
},
{
name: '@electron-forge/maker-zip',
platforms: [
'darwin',
],
},
{
name: '@electron-forge/maker-deb',
config: {},
},
{
name: '@electron-forge/maker-rpm',
config: {},
},
],
plugins: [
[
'@electron-forge/plugin-webpack',
{
mainConfig: './webpack.main.config.js',
renderer: {
config: './webpack.renderer.config.js',
entryPoints: [
{
html: './src/renderer/index.html',
js: './src/renderer/index.tsx',
name: 'main_window',
},
],
},
},
],
],
hooks: {
readPackageJson: async (forgeConfig, packageJson) => {
// only copy deps if there isn't any
if (Object.keys(packageJson.dependencies).length === 0) {
const originalPackageJson = await fs.readJson(path.resolve(__dirname, 'package.json'));
const webpackConfigJs = require('./webpack.renderer.config.js');
Object.keys(webpackConfigJs.externals).forEach(package => {
packageJson.dependencies[package] = originalPackageJson.dependencies[package];
});
}
return packageJson;
},
packageAfterPrune: async (forgeConfig, buildPath) => {
console.log(buildPath);
return new Promise((resolve, reject) => {
const npmInstall = spawn('npm', ['install'], {
cwd: buildPath,
stdio: 'inherit',
});
npmInstall.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error('process finished with error code ' + code));
}
});
npmInstall.on('error', (error) => {
reject(error);
});
});
}
}
}; |
@lhr0909 I can't thank you enough. I was stuck in this for two days |
@lhr0909 I added your config and it works for the initial compilation step and HMR! However, any page reload after subsequent webpack rebuilds results in an error:
Even though Also, running
|
Good prior art from @timfish #1276 (comment) https://www.npmjs.com/package/@timfish/forge-externals-plugin |
There's also an unfinished PR for forge: There were some reported issues around scoped packages which should probably be looked into/tested: |
I'm having the same issue, I'm using inside my webpack configuration I have set them up as external, they work fine when I run it locally, but when I pack it, it doesn't work |
thank you for taking your time to post it here. I'm getting an error when I try to make my app with that, I'm adding the hooks to the error:
should I add the hooks somewhere else? |
@augustnmonteiro the configuration should be added into forge.config.js |
I had the same issue using "sharp" module.
|
Do packageAfterPrune run in development as well? I am getting errors with serialport and usb and the solutions people mention do not work on development mode. Thank you in advance. |
it work! |
I had to make a few modifications to @lhr0909's configuration to get it to work with ES6 modules and Windows. Here are my hooks: hooks: {
// Handle externals properly:
readPackageJson: async (forgeConfig, packageJson) => {
// only copy deps if there isn't any
if (Object.keys(packageJson.dependencies).length === 0) {
const originalPackageJson = await fs.readJson(path.resolve(__dirname, "package.json"));
Object.keys(rendererConfig.externals || {}).forEach((pkg) => {
packageJson.dependencies[pkg] = originalPackageJson.dependencies[pkg];
});
}
return packageJson;
},
packageAfterPrune: async (forgeConfig, buildPath) => {
console.log(buildPath);
try {
const res = await execSync("npm install", { cwd: buildPath });
console.log(res.toString());
} catch (error) {
console.error(error);
}
}
} You'll need these imports: import { mainConfig } from "./webpack.main.config";
import { rendererConfig } from "./webpack.renderer.config";
import fs from "fs-extra";
import path from "path";
import { execSync } from "child_process"; It works for me on Windows and macOS. Hopefully this helps someone! |
I am using 'electron-edge-js' to run a simple C# script in my main.js file. To use the imported package I've set my webpack.main.config.js to:
This works as expected when using the start script (because the packages are in the node_modules folder). But when the app is packaged, I get the error: module 'electron-edge-js' not found.
When inspecting the built resources>app>node_modules folder I see that it's empty and the created package.json shows no dependencies. I feel like I'm missing something obvious but I've searched and tried possible solutions to no avail.
What does your
config.forge
data inpackage.json
look like?Minimum test case to reproduce can be found here
or clone: git@github.com:dustinstein/test-app.git
The text was updated successfully, but these errors were encountered: