If you want to write a front-end library, or a web application without a framework, what modular style do you follow? And what tools do you choose and why?
Below is a quick list of the options. I am trying to learn more about them atm, so I am curious on your take! Let me know if there is something I missed that may be relevant!
Modular "Styles"
- ES6 Modules: Native to JavaScript.
- CommonJS: This system was born with server-side JavaScript in mind, it is used in Node. Modules are loaded synchronously, and processed in the order the JavaScript runtime finds them. Only supports objects as modules. You can use it client-side with Browserify.
- Asynchronous Module Definition (AMD): Takes a browser-first approach. Modules and dependencies can be asynchronously loaded. Modules can be different types (objects, functions, strings, and so on). RequireJS is the most popular client-side implementation.
- Universal Module Definition (UMD): Bid to provide "universal" pattern that supports both CommonJS and AMD styles.
- Module pattern: Some form of using closures to create a local scope for related variables and functions.
- Don't modularise code. Have one file with everything in it.
Bundlers
Module bundling is the process of combining a group of modules (and their dependencies) into a single file (or group of files) in the correct order. Bundlers may handle other assets such as CSS and images also.
A lot of different bundlers exist now, arguably, these are the most popular ones:
- Webpack: Webpack is used to compile JavaScript modules, but it can also transform front-end assets like HTML, CSS, and images if the corresponding loaders are included.
- Parcel: A web application bundler, differentiated by its developer experience. It offers blazing fast performance utilizing multicore processing, and requires zero configuration.
- RequireJS: Optimized for in-browser use, but it can be used in other JavaScript environments. Supports CommonJS-style modules.
- Rollup: Rollup allows you to write your code as ES6 modules, and will then compile it back down to existing supported formats such as CommonJS modules, AMD modules, and IIFE-style scripts.
- Browserify: Allows developers to CommonJS-style modules that compile for use in the browser.
- FuseBox: Alternative to Webpack with first-class TypeScript support. Can replace Babel.
Choosing the right bundler in 2020.
Transpilers
Transpilers are tools that read source code written in one programming language, and produce the equivalent code in another language. Transpilers are used to convert from CoffeeScript and Typescript to JavaScript, and from a recent version of JavaScript to an older version.
Popular transpilers are:
- Babel: Babel is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript. However, it now has a lot of plugins to extend Babel for specific libraries, tools for things like linting, optimizations for browsers, and minification.
- Bublé: Bublé is used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript. Bublé is heavily inspired by Babel, but limits itself to ES features that can be compiled to compact, performant ES5 (plus JSX), and it's comparatively tiny and much faster
- Traceur: Traceur is used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript. Supports experimental features.
- Typescript: TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.
- CoffeeScript: CoffeeScript is a little language that attempts to expose the good parts of JavaScript in a simple way. Transpiles into a version of JavaScript of your chosing.
Task Runners
Task runners are used to automatically perform frequent tasks such as minification, compilation, unit testing, and linting.
Photo by Vanessa Bucceri on Unsplash
Thank you for reading! Feel free to subscribe to my RSS feed, and share this article with others on social media. 💌
You can show your appreciation by buying me a coffee on ko-fi. 🙂
Top comments (3)
I have the index.html file in the main folder directory, I also have
pages
(if applicable or needed),_assets
which contains my images, CSS, and if in use SASS containing folders (if using sass, I also include asass.sh
script so I can have a quick SASS compile (and in the SASS folder, a mobile and desktop folder). Back on the main folder,_client
contains JavaScript code I need, and sometimes other things like Python and V lang.I organize everything in a
src
folder that would containercss
,js
,svg
and any other directories of files that need to be "processed" in some way. All processed files go into thedist
root directory.My JavaScript, I write in ES6 and use rollup to transpile (babel plugin) and bundle the final format in a minified
iife
file.As for styles, I use Sass and write in BEM methodology. That way each
.scss
file would be it's own component with maybe a single_global.scss
file for just general styles. It's pretty rare that I'll need random general styles tho.For task runners, I just use NPM scripts. I've been using Gulp for a few years but dropped it recently in favor of NPM cause I can just write most of my tasks using packages directly without the added layer of a task runner. So for example, my Sass script looks like this:
Last thing I'll add is I take a slightly different approach if I'm writing a component I plan to reuse or include in my component library. That's when I use this concept:
Here's an example of that concept in my component library where I use Lerna to manage.