Code used in production is different from development code. In production, you need to build packages that run fast, manage dependencies, automate tasks, load external modules, and more. JavaScript tools that make it possible to turn development code into production code are called build tools.
The reason frontend code is "built" can be explained by looking into the various build steps and their importance.
Steps in building frontend code
There are four steps involved in building frontend code:
1. Transpiling
Transpiling lets developers take advantage of the newest, hottest updates to languages and extensions and maintain browser compatibility. Here is an example using Babel:
// arrow function syntax in array map
const double = [1, 2, 3].map((num) => num * 2);
// after being transpiled
const double = [1, 2, 3].map(function(num) {
return num * 2;
});
2. Bundling
Bundling is the process of taking all the "import" or "require" statements; finding the matching JavaScript snippets, packages, and libraries; adding them to the appropriate scope; and packaging it all into one big JavaScript file. Commonly used bundlers include Browserify, Webpack, and Parcel.
3. Minifing
Minifying reduces the final file size by removing white space and code comments. You can take this a step further by adding an obfuscate step, which changes variable names and method names, obscuring the code, so it is less human-readable once it's delivered to the client. Here is an example using Grunt:
// before minifying
const double = [1, 2, 3].map(function(num) {
return num * 2;
});
// after minifying
const double = [1, 2, 3].map(function(num) {
return num * 2;
});
4. Packaging
After all the steps above are finished, the compatible, bundled, minified/obfuscated file must be put somewhere. Packaging is the process of putting the results of the above steps somewhere specified by the developer. This is usually done by the bundler.
Frontend build tools
Frontend tooling and build tools can be split into the following categories:
- Package managers: NPM, Yarn
- Transpilers: Babel, etc.
- Bundlers: Webpack, Parcel, Browserify
- Minifiers: UglifyJS, Packer, Minify, etc.
There are a variety of build tools you can use in the JavaScript ecosystem, including the following.
Grunt and Bower
Grunt was introduced as a command-line tool that provided just one script to specify and configure tasks. Bower followed shortly after as a way to manage client-side packages. The two, along with NPM, seemed to fulfill the majority of automation needs and were used regularly together. The problem with Grunt was that it didn't provide developers the freedom to configure more complex task chains, while Bower made developers manage twice as many packages as usual because it separates frontend and backend packages (i.e., Bower components vs. Node modules).
The future of Grunt and Bower: Grunt and Bower are on their way out of the JavaScript tooling ecosystem, but there are several replacements.
Gulp and Browserify
Gulp was released a year and a half after Grunt. It felt natural. Writing a build script in JavaScript compared to JSON gave freedom. You could write functions, create variables on the fly, use conditionals anywhere—not that this would make for a particularly good-looking build script, but it was possible. Browserify and Gulp can be used in tandem. Browserify allows NPM packages (which are for backend Node servers) to be brought into the frontend, making Bower obsolete. It also looks and feels better, with one package manager for the frontend and backend.
The future of Gulp: Gulp can be improved to match the current popular build tools, but this is entirely up to the creators. It is still in use but not as popular as it was before.
Webpack and NPM/Yarn scripts
Webpack is the hottest kid on the block in modern frontend development tooling. Webpack is an open-source JavaScript module bundler. It is made primarily for JavaScript, but it can transform frontend assets like HTML, CSS, and images if the corresponding loaders are included. With Webpack, you can also write scripts like Gulp and execute them with NPM/Yarn.
The future of Webpack: Webpack is currently the hottest thing in the JavaScript tooling ecosystem, and all the JS cool kids are using React and Webpack these days. It is currently in version 4 and not going anywhere anytime soon.
Parcel
Parcel is a web application bundler that launched in 2018 and is differentiated by its developer experience. It offers blazing-fast performance utilizing multicore processing and requires zero configuration. Parcel is also a new kid on the block, but adoption hasn't been fast, especially for large applications. Developers prefer to use Webpack over Parcel because of the former's extensive support and customizability.
The future of Parcel: Parcel is very easy to use, it is faster than Webpack if you measure bundle and build time, and it also offers a better developer experience. The reason Parcel hasn't been adopted much might be because it's still relatively new. Parcel has a very bright future in the frontend build tools ecosystem, and it will be around for a while.
Rollup
Rollup is a module bundler for JavaScript that compiles small pieces of code into something larger and more complex, such as a library or an application. It is advisable to use Rollup when building a library with minimal third-party imports.
The future of Rollup: Rollup is super-cool and is being adopted rapidly. It has great features and will be part of the frontend tooling ecosystem for a long time.
Learn more
The JavaScript tooling ecosystem is dynamic and competitive, and only the best tools survive. In the future, we will see tools that require less (or no) configuration, better customizability, more extensibility, and higher speed.
The tools you use for an application's frontend are a personal call that you need to make based on your project's requirements. Choose what works best for you, and most of the time, there are tradeoffs.
For more information, see:
- JavaScript tooling: The evolution and future of JS/frontend build tools
- Tools and modern workflow for frontend developers
- Modern frontend: The tools and build process explained
- Best build tools in frontend development
This article originally appeared on Shedrack Akintayo's blog and is republished with his permission.
4 Comments