-
-
Notifications
You must be signed in to change notification settings - Fork 171
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
Question: Can you bundle dependencies into the main module output? #268
Comments
@millimoose first of all: sorry that there hasn't been any reaction, at all, to your issue in months. As you've probably seen, issues are piling up and remain unaddressed for months.. we lack manpower.. Based on previous conversations with @develar, I assume that one of his core ideas about an electron app vs a web app is that we just don't have to care about file size or loading times, code splitting and related optimizations. Because hey! It's all bundled up and available in no time. So.. while I cannot provide any solution, I hope I could explain the reason why electron-webpack just bundles up everything :) |
Hey, thanks for getting to me anyway! Fair enough if that’s your project’s call; I have a hunch the difference was significant for my project but it’s been a while I can’t really reproduce it on short notice. My main motivation IIRC was hoping to migrate a cobbled-together build to electron-webpack to hopefully have main process HMR and I was trying to have the new setup work as closely to the old one as possible to avoid some bugs I’d been seeing, so I might try and see if I can figure out how to use that feature on its own with my current build.
—dV
…________________________________
From: Jovica Aleksic <[email protected]>
Sent: Friday, May 17, 2019 7:09 pm
To: electron-userland/electron-webpack
Cc: David Vallner; Mention
Subject: Re: [electron-userland/electron-webpack] Question: Can you bundle dependencies into the main module output? (#268)
@millimoose<https://github.com/millimoose> first of all: sorry that there hasn't been any reaction, at all, to your issue in months. As you've probably seen, issues are piling up and remain unaddressed for months.. we lack manpower..
Based on previous conversations with @develar<https://github.com/develar>, I assume that one of his core ideas about an electron app vs a web app is that we just don't have to care about file size or loading times, code splitting and related optimizations. Because hey! It's all bundled up and available in no time.
And I basically agree with that philosophy.
I agree also that reducing bundle size is not a real concern for an electron app because a couple of megabytes are nothing compared to the many dozens of megabytes of electron, chromium etc itself.
We won't ever be able to ship an electron app that is "small" in file size..
(BTW check out Google's Carlo project. It's like electron in many ways, but it relies on chrome or chromium being installed on the user's machine - allowing tiny output almost as small as your Js bundle)
So.. while I cannot provide any solution, I hope I could explain the reason why electron-webpack just bundles up everything :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#268?email_source=notifications&email_token=AAFFATCFPSP5WASYTOPFPYDPV3RDHA5CNFSM4GWI4U42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVVKKJY#issuecomment-493528359>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AAFFATFUNRFEPHYHWFDSMZLPV3RDHANCNFSM4GWI4U4Q>.
|
I inadvertently had to look into the difference again because an unrelated issue forced me to investigate using unbundled dependencies. The difference for me is around 500MB of (That said it's possible said issue might force my hand to use unbundled dependencies anyway, which would ironically mean I might look into electron-webpack as-is again now that that's no longer an issue.) |
Hey @millimoose I'm still not quite sure I understand the problem :)
Those 500MB of dependencies didn't appear out of nowhere, right? It must be some depdendencies depdendency etc. My point being: Doesn't your app actually need those dependencies? Wouldn't it crash in some case when some sub-sub-dependency is required but not available? Also.. is it the point that.. That some of your dependencies claim to need some modules/packages, making the bundle huge, but you would actually like to drop those modules despite of the dependency tree? I'm not sure how unbundled dependencies work in electron, hadn't heard the term before. |
I think this will become clearer when I define my terminology. I'll be using "module" to mean a CJS / ES6 module - a .js file that requires/imports other modules, and exports some functions etc; and "package" to mean say a library you get from NPM, which contains a bunch of modules. Crucially, by "bundling" I'm referring to what Webpack does in the simplest case: which is take a module / file as a starting point, look at what modules it require and which modules those require transitively etc., and emit a single file - the bundle - containing all the code and only the code you need. (Webpack actually goes further, it looks at individual functions the modules export - the ideal is for the bundle to only include those lines of code your app can possibly need and not include those it never will.) Webpack also supports external modules, which is telling webpack "hey, when somebody imports these, don't actually include them in the bundle or process them at all, they'll be available to require/import in the usual way. (The whole reason tools like Webpack were created was to take a NodeJS-like project codebase with NPM dependencies, and allow it to be loaded in a browser with a Doing so is the safer way of bundling Node apps and certainly a reasonable default, because even though Webpack tries very hard, not all Node packages will work correctly when bundled, but it's more than you'd think. But quite a lot of them work - out of said 500mb of crud only three libraries gave me any grief and that was mainly due to loading native bindings. Given this, not bundling them up is a nontrivial amount of bloat even relative to Chromium+Electron. Long story short, what I mean is that out of all of my This doesn't have a lot to do with Electron or ASARs, they're handle the archiving and loading from said archive here. |
Until it's fixed on electron-webpack side, you can patch it Inside "scripts": {
"postinstall": "node patch.js"
} Create Edit: this script can be shortened further, see this post. // https://github.com/electron-userland/electron-webpack/issues/268
const ewPath = require("path").join(
__dirname,
"node_modules",
"electron-webpack",
"out",
"main.js"
);
script = require("fs").readFileSync(ewPath).toString("utf8");
script = script.replace(/const externals =/, "const externals = [];");
script = script.replace(
'externals.push("source-map-support/source-map-support.js");',
[
'externals.push("source-map-support/source-map-support.js");',
'externals.push("source-map");',
'externals.push("buffer-from");',
].join("")
);
fs.writeFileSync(ewPath, script); In files: [
"!**/node_modules/**",
"**/node_modules/source-map-support/**",
"**/node_modules/source-map/**",
"**/node_modules/buffer-from/**",
], ResultsLoose node_modules
Bundled app
I think that additional comment is not needed. |
@prcdpr so source-map and buffer-from do belong into webpack externals? |
@loopmode I'm still learning, so I checked my code twice and it turns out that
"buffer-from": "^1.0.0",
"source-map": "^0.6.0" This is why we need those two in node_modules. Thanks for noticing. So my // https://github.com/electron-userland/electron-webpack/issues/268
const ewPath = require("path").join(
__dirname,
"node_modules",
"electron-webpack",
"out",
"main.js"
);
script = require("fs").readFileSync(ewPath).toString("utf8");
script = script.replace(/const externals =.+/, "const externals = [];");
fs.writeFileSync(ewPath, script); Not sure what you meant by setting externals to empty array in custom config? electron-webpack/packages/electron-webpack/src/main.ts Lines 298 to 300 in 0c0bf2a
This would have no effect at all or am I missing something? |
Yeah the trick is to use a custom config module that exposes a function, not an object. |
See #420 (comment) |
Thanks, I read that discussion and After adding And requires for Everything seems good but one thing keeps me wondering... I think that most of these externals were discovered by Webpack as coming from Node or Electron and marked as such. But how Is it this code that hardcodes it into the beginning of main bundle? electron-webpack/packages/electron-webpack/src/targets/MainTarget.ts Lines 30 to 43 in 8a9d289
I'm happy I finally found out how I can bundle my code but seriously it should be a default or at least documented 🤔 |
I have three renderers, one for Here is the code https://github.com/cdmbase/fullstack-pro/blob/master/portable-devices/desktop/webpack.renderer.additions.js#L41 |
Hmm. Interesting problem. I'm not sure but i think it should already work the way you want it to - unless you have references/imports in your code, maybe in unexpected places, that pull in "everything".. But otherwise: I cannot answer your question about how to enable tree shaking in electron-webpack. |
Btw If you use lazy loading and |
I'm attempting to migrate a cobbled-together Electron+Webpack build to electron-webpack and electron-builder to have an integrated solution with HMR. Our current build bundles all dependencies into the main entry point file like you would in a webapp (before bundle splitting) to take advantage of tree-shaking and reduce the final size.
I tried adding my production dependencies to
whiteListedModules
, but electron-builder still added my entirenode_modules
to the output and broke native dependency loading in a cryptic way which worked in the ad-hoc build.Is it possible to make this sort of setup work, or is there a a specific reason why it's not supported?
The text was updated successfully, but these errors were encountered: