Minifying code for PhoneGap App?

江枫思渺然 提交于 2020-01-12 02:31:17

问题


I am currently starting to build an PhoneGap application, I know the advantages of minifying code for browsers already. However, I was wondering if it was worth minifying my HTML, JavaScript and CSS code for my PhoneGap application?

I would understand that the code run faster potentially faster?

Also if that is the case is there an application that will do it all for me? (When I say do it for me I mean take a directory on my machine and minify the code saving a .min.js version? or is it a simple file by file?)

Thanks in advance for any help


回答1:


In general it is not worth minifying PhoneGap application. Minifying is beneficial for web application as it reduces size of each request.

With PhoneGap all your HTML/JS/CSS will be bundled into application package (which is compressed btw.) and downloaded once on application install. So there won't be any gains in speed.




回答2:


Well tickle my feet and call me grandma, but I have been minifying the javascript for my phoneGap app, and it most certainly improves performance.

Of course there is barely any difference in terms of the size of the application package. So the one time that a user downloads and installs the app, there is no real gain from minifying.

But every time the user runs the app, having minified javascript does make a difference. Even though the file is local, it still needs to be loaded into the 'browser', and smaller files are loaded faster and will thus start executing earlier.

And chances are you'll have a lot of javascript since most apps want to "do" something instead of just providing passive HTML content.

I'm considering minifying the phonegap.js file itself too. Any thoughts on that are highly appreciated.




回答3:


An answer from PhoneGap representative, copied from PhoneGap Community Forum :

"Not necessarily. JavaScript compression is most important for network performance - web applications that are requesting files from remote servers need to wait for the file to be downloaded, so having a smaller file makes the process a lot smoother.

What matters on mobile devices, and PhoneGap apps specifically, is memory, rather than network traffic. While you may get some memory benefits from running a minified JS file, both the original and the minified file are interpreted into the same code*, so the difference is negligible.

In most cases, minifying JavaScript is low on the list of priorities for a PhoneGap application."




回答4:


This might be a bit late, but if you are still interested in minifying your application's code, I made an NPM package that minifies your Javascript, CSS, and image files called cordova-minify (https://www.npmjs.org/package/cordova-minify).

My project is also on GitHub, so if you want to contribute and/or see what is under the hood, it's there - https://github.com/alastairparagas/cordova-minify. It's practically a Cordova Hook that calls some NPM package dependencies to do the respective compression.




回答5:


From my experience Javascript grows a lot more than html/css.

From here: https://reactjs.org/docs/add-react-to-a-website.html

Tip: Minify JavaScript for Production
Before deploying your website to production, be mindful that unminifed JavaScript can significantly slow down the page for your users.
...
If you don’t have a minification step for your scripts, here’s one way to set it up.

Which takes to https://gist.github.com/gaearon/42a2ffa41b8319948f9be4076286e1f3

In production, it is recommended to minify any JavaScript code that is included with your application. Minification can help your website load several times faster, especially as the size of your JavaScript source code grows.

Yes, it is recommended to minify.
The 2nd link above has simple commands on how to minify, using terser npm package.



来源:https://stackoverflow.com/questions/10532376/minifying-code-for-phonegap-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!