Module Exclusion
I spent at least an hour looking around for a solution before submitting this issue, so I apologize if a similar issue has already been submitted.
I need to be able to exclude imported modules (or directories of modules) from the build process because they are only available when the application is running in the Electron environment. The build process currently fails because it cannot locate the Electron related modules.
import Electron from 'electron' // causes the build to fail
Can exclusions be defined in the package.json file?
This, in general, seems like a fragile thing to do, and might mean it is better to rethink how you structure your app.
For example, you could write modules that can work both with Electron and without:
import createMyOtherThing from './createMyOtherThing';
function createMyThing(Electron) {
var MyOtherThing = createMyOtherThing(Electron);
var MyThing = {
doSomething() {
if (Electron) {
// ...
} else {
// ...
}
MyOtherThing.doSomethingElse();
}
};
return MyThing;
}
Then you could reuse those modules between projects as a library, and “inject” Electron as the last step:
electron-app/src/index.js
import createMyThing from 'my-library';
import Electron from 'electron';
const MyThing = createMyThing(Electron);
web-app/src/index.js
import createMyThing from 'my-library';
const MyThing = createMyThing(null);
There is no really good way for doing conditional imports in this case because even if you put a CommonJS require() behind a condition, this code will still exist in development bundle, and Webpack will still try to resolve that dependency.