https://engineering.velocityapp.com/webpack-vs-browersify-vs-systemjs-for-spas-95b349a41fa0

Right now, there are at least 8 powerful open source Javascript bundlers; a few years ago there were only a couple. In building a large application at Velocity , I ultimately decided to review the following three build systems: WebpackBrowserifySystemjs. After analysing the three, I ultimately switched bundlers and it has saved us days of development time.

A Brief Comparison

A typical bundler will import application source code and it’s dependencies through an entry point. This entry point allows us to import only the relevant files and included dependencies into our final bundle and concatenate and minify them to make them available to use in the browser. Each bundler does this differently, but in the end all become single javascript file or group of files to serve when necessary. In the following review, I considered the speed of the watch task (compiling the application on the fly after changing the code), the ease of setup/configuration, size of the bundle, the flexibility of the builder to customise the environment etc, and time it takes to build. Keep in mind, the build times of these bundlers vary significantly in configuration and project size.

Browserify

Browserify is perhaps the simplest bundler because it uses NodeJS require syntax to import javascript. It does not deal with any other assets other than pure javascript modules. You can also bundle your typescript/es6 app with Browserify plugins. However, you’ll definitely need to add a task manager like gulp or grunt for a production ready application as you’ll want to also hash or bundle file, add environments, regex replaces, further minification and asset management. Below is a simple Browserify gulp task that takes in a set of Browserify settings and entry point and outputs a bundle.js file. Gulp handles where the files go:

 

While parts of the gulp task are convoluted, the browserify part of the configuration is only a couple of lines. In comparison to the others, I would use browserify for javascript/typescript projects that do not require a lot of asset management and aren’t really complicated or large.

In review:

The Speed of Browserify:

  • Building for Production: I’d say Browserify is almost on par with Webpack and SystemJs. However, it only bundles code, not assets. Also, the transforms can really slow down the builds.
  • Watching — Browserify out of the box does not watch well, it’s very slow. You need to add watchify to incrementally watch your code changes. If you have any transforms this is still at least 5–6 seconds for a large application. You can configure hot module reloading (the loading of a small module or part of your code, instead of the entire code), but there aren’t many examples how to get the best out of this, nor is it widely used or integrated into the core like webpack.
  • Verdict: Good for pure Javascript applications, not Typescript or any apps that require a transforms.

Setup:

Browserify is by far the simplest setup in comparison to SystemJs or Webpack. There is still a bit of magic in that everything seems to function like NodeJs, but otherwise it does the job a bundler is supposed to do.

Verdict: Easiest to Setup

Bundle Size:

Browserify offers no built-in tree shaking or minification plugins, thus it’s bundle is quite large and you’ll need to use plugins or write a lot of gulp tasks.

Verdict: Large bundle, but plain javascript, you’ll have to figure it out yourself.

Flexibility:

The flexibility and customisability of Browserify is almost as good as Webpack, in that the plugins and easy integration with task managers, but the lack of asset management and the poor performance of plugins really make it difficult to leverage the power of the build system.

Verdit: Good, but not top dog.

SystemJs

Like Browserify, SystemJs uses the require syntax, but it is more compatible with other times of files. On GitHub, it calls itself the ‘universal loader’, in that it loads ES6 modules, AMD, CommonJS and global scripts in the browser and NodeJS. Meaning, it has a lot more built-in support for including different types of files than Webpack or Browserify. However, this comes at a cost. You have to directly tell SystemJs where to look for its dependencies and you end up with a config file like so:

 

Above is an Angular 2 SystemJs example. Including all your dependencies one by one is not ideal for large application, thus I recommend that you’d only want to use SystemJs for smaller, more diverse projects and save yourself sometime not having to write out this configuration.

In Review:

The Speed of SystemJs:

  • Builds — Despite having to be able to load in any module type, SystemJs is surprisingly fast for production builds because it does one job very well. I’d say it’s faster than Browserify a lot of the time.
  • Watching — For watching, it is also quite snappy and no magic happens under the hood. In some cases it’s faster than Browserify for large applications. You can also configure relevant hot module reloading with SystemJs as well, which is awesome and there are lots of Angular 2 examples online on how to do this.

Verdict: Fast, but not the fastest.

Setup:

The setup is probably the most tedious and also maintaining the SystemJs config is also quite difficult as you have to manually add dependencies and make sure your file paths don’t change.

Verdict: Simple, yet tedious and manual.

Bundle Size:

SystemJs seems less mysterious when bundling and the size isn’t really different than browserify because there are few to no additional add-ons that can do ‘magical’ things for you.

Verdict: Like Browserify, know your minification and tree-shaking gulp/node plugins.

Flexibility:

SystemJs is not as flexible or customisable as Browserify or Webpack.

Verdict: Do everything separately with Gulp.

Webpack

Webpack treats everything as a module. Your css, html, javascript, fonts, images. Anything and everything can be inlined into your code if you desire. In addition to making everything a module, it also allows to you specify how you load these modules with ‘loaders’ and bundle them together into your bundle or index.html. Because webpack can bundle your entire app together , it makes tree shaking and optimising your code easy. Below, is an example from the Webpack Angular 2 starter. The Angular team has also recently switched to Webpack from SystemJs in their angular-cli. Below, is a sample webpack configuration file:

 

In Review:

The Speed of Webpack:

  • Builds — Webpack’s build time directly depends on the configuration, but out of the box it is really fast, despite also including html and styles into the bundling process.
  • Watching — For watching, it is also quite snappy. Many misconfigure webpack to run production build during its watches, thus it is important to read the documentation and make sure to only use webpack.optimize for production builds, not development. You can also configure relevant hot module reloading and with React you can hot reload your state — keeping the state of your application as you develop, which is awesome!

Verdict: Fast, configurable, and ready for production use.

Setup:

The first-time setup is difficult to grasp, with terms like ‘loaders’, but after understanding such and how the bundling works, setting up other webpack projects is easy. You won’t even need gulp. Just bundle everything with webpack and some npm commands. In the end, you’ll end up writing less code than gulp or any other bundler.

Verdict: Hard to understand at first, but well worth it.

Bundle Size:

Because of webpack’s built-in optimisation plugins it does an amazing job and getting rid of unused code and tree shaking.

Verdict: The best for production builds as it has built-in de-duping, uglifying, and tree-shaking (2.0).

Flexibility:

Webpack is one of the most flexible bundlers because there are loaders pretty much for anything — so you can shim or include any type of file you want. It’s more flexible than browersify in that you can decide more entry points and support different types of assets.

Verdict: The most flexible and powerful of the 3, but again learning how each part of it works is hard.

Conclusion

I recommend using Webpack 1 for bundling large web applications.

Webpack is also my first choice for single page web applications. At Velocity, we switched from Browserify to Webpack and reduced our bundle size by 10–20%, watch times by seconds, and builds by minutes. Also we’ve reduced the number of gulp tasks in our builds and decreased the complexity.

Overall, webpack is the clear winner when it comes to speed and flexibility. Webpack 2 offers more features and performance outside of the box, it is in beta and not ready for production as some of the sourcemapping for scss is broken and not all loaders support it.

Browserify is good for quick small applications. SystemJS is great for small to medium sized applications that rely on a variety of different dependencies. However, with Webpack you can support all of what SystemJs or Browserify support with external loaders.

Please share your thoughts on my comparison and code samples. Also, if you know any speed or improvements that I can make to my examples, please let me know. Also below, I listed some other bundlers you may want to take a look at.

Cheers,

Evan Gillogley

Web Developer at Velocity

Other Great Bundlers:

Rollup(great tree shaking, primarily used for ES6)

Lasso — made by eBay — influenced by Browserify, but bundles all the assets.

JSPM — Like SystemJs with npm integration.

For more bundlers, check out thispage.

Webpack vs Browersify vs SystemJs for SPAs的更多相关文章

  1. Vue.js 2.0 和 React、Augular等其他框架的全方位对比

    引言 这个页面无疑是最难编写的,但也是非常重要的.或许你遇到了一些问题并且先前用其他的框架解决了.来这里的目的是看看Vue是否有更好的解决方案.那么你就来对了. 客观来说,作为核心团队成员,显然我们会 ...

  2. 在 2016 年学 JavaScript 是一种什么样的体验?

    转 译者:方应杭 嘿,我最近接到一个 Web 项目,不过老实说,我这两年没怎么接触 Web 编程,听说 Web 技术已经发生了一些变化.听说你是这里对新技术最了解的 Web 开发工程师? 准确地说,我 ...

  3. Vue.js 2.0 和 React、Augular

    Vue.js 2.0 和 React.Augular 引言 这个页面无疑是最难编写的,但也是非常重要的.或许你遇到了一些问题并且先前用其他的框架解决了.来这里的目的是看看Vue是否有更好的解决方案.那 ...

  4. vue对比其他框架

    对比其他框架 React React 和 Vue 有许多相似之处,它们都有: 使用 Virtual DOM 提供了响应式(Reactive)和组件化(Composable)的视图组件. 将注意力集中保 ...

  5. (三) Angular2项目框架搭建心得

    前言: 在哪看到过angular程序员被React程序员鄙视,略显尴尬,确实Angular挺值得被调侃的,在1.*版本存在的几个性能问题,性能优化的"潜规则"贼多,以及从1.*到2 ...

  6. 2017年 JavaScript 框架回顾 -- 后端框架

    本文是2017年 JavaScript 框架回顾系列的最后的一篇文章,主要介绍 JavaScript 的后端框架情况. 从上图中可以看到,Express 作为用 JavaScript 编写的后端服务的 ...

  7. 在 2016 年学 JavaScript 是一种什么样的体验?(React从入门到放弃)

    jquery 年代 vs 前端模块化 http://blog.csdn.net/offbye/article/details/52793921 ++ 嘿,我最近接到一个 Web 项目,不过老实说,我这 ...

  8. Vue.js vs React vs Angular 深度对比[转]

    这个页面无疑是最难编写的,但我们认为它也是非常重要的.或许你曾遇到了一些问题并且已经用其他的框架解决了.你来这里的目的是看看 Vue 是否有更好的解决方案.这也是我们在此想要回答的. 客观来说,作为核 ...

  9. JavaScript怎样学

    嘿,我最近接到一个 Web 项目,不过老实说,我这两年没怎么接触 Web 编程,听说 Web 技术已经发生了一些变化.听说你是这里对新技术最了解的 Web 开发工程师? 准确地说,我是一名「前端工程师 ...

随机推荐

  1. 精通CSS+DIV网页样式与布局--制作实用菜单

    在上篇博文中,小编中主要的简单总结了一下CSS中关于如何设置页面和浏览器元素,今天小编继续将来介绍CSS的相关基础知识,这篇博文,小编主要简单的总结一下在CSS中如何制作网页中的菜单,这部分的内容包括 ...

  2. 软考之路--从生活着手,看PV如何操作

    PV操作,是软考当中一个很重要的考点,一听到这个名词,顿时赶脚高大上有么有,在软考的历年试题中,也不乏PV操作的身影,老师也对PV操作进行了一次讲课,那时年少,听得稀里糊涂,也不是很理解,在小编的理解 ...

  3. Socket编程实践(7) --Socket-Class封装(改进版v2)

    本篇博客定义一套用于TCP通信比较实用/好用Socket类库(运用C++封装的思想,将socket API尽量封装的好用与实用), 从开发出Socket库的第一个版本以来, 作者不知道做了多少改进,  ...

  4. oracle ebs应用产品安全性-数据访问权限集

    定义 数据访问权限集是一个重要的.必须设定的系统配置文件选项.对具有相同科目表.日历和期间类型的分类帐及其所有平衡段值或管理段值的定义读写权限,系统管理员将其分配至不同的责任以控制不同的责任对分类帐数 ...

  5. 四大组件之 BroadcastReceiver小结

    总结提高,与君共勉 1. BroadcastReceiver是什么? BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来接收来自系统和应用中的广播. ...

  6. Cocos2D v2.0至v3.x简洁转换指南(三)

    Cocos2D 3.3中的注意事项 如果你在使用Cocos2D 3.3+(是SpriteBuilder 1.3+的一部分)你将不得不替分别的换所有存在的UITouch和UITouchEvent为CCT ...

  7. Android Studio中创建Kotlin For Android项目

    Kotlin俗称Android中的Swift,它是Jetbrains公司开发的基于JVM的一门语言,JetBrains公司可能大家并不熟悉,不过相信IntelliJ IDE大家一定知道,Android ...

  8. (转载)SQL Server2008附加数据库之后显示为只读时解决方法

    SQL Server2008附加数据库之后显示为只读时解决方法 啰嗦的话就不多说了,直入主题吧! 方案一: 碰到这中情况一般是使用的sa账户登录的,只要改为Windows身份验证,再附加数据库即可搞定 ...

  9. 谈谈java中的线程(初级概念)

    定义 关于进程与线程的定义 可参看一下这个介绍 http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html 在不细抠定义的情况下 ...

  10. Mahout Bayes分类

    Mahout Bayes分类器是按照<Tackling the Poor Assumptions of Naive Bayes Text Classiers>论文写出来了,具体查看论文 实 ...