Parcel is better than Gulp, for me at least

2021-06-02

Picture of a lot cardboard boxes pilled up

But first, what is Parcel?

Parcel is a simple no-configuration web application bundler, it's a tool used to bundle individual modules into a single file that can be exectued by the browser. Parcel supports different languages and file types like HTML, CSS, and JavaScript.

Parcel sells itself as a "blazing fast, zero-config" bundler, and it works, because as you may know, their main competitor is not very easy to get into, as the learning curve for Webpack can be steep.

And for somebody like myself that has to handle a full-time job while tyring to learn new things in the always-changing web development world, a simple, low-configuration bundler it's just what I needed for my client and personal projects.

What about Gulp?

Before Parcel I used Gulp, and even though Gulp and Parcel are not scrictly under the same category because Gulp is a task manager and Parcel is a bundler, I used Gulp in the same way that I use Parcel.

I can't remember how or when I was introduced to Gulp but at first, I thought it was the best thing, you could use browser-sync, scss, and even optimize your images for production. You only had to create a config file in the root folder, but once you got the hange of it, it was no issue, besides, you can always just copy-paste things if you have the same folder structure.

Here's what my file looked like:

const browserSync = require('browser-sync').create();

function watch() {
  browserSync.init({
    server: {
      baseDir: 'src',
    },
  });
  gulp.watch('src/assets/css/**/*.css').on('change', browserSync.reload);
  gulp.watch('src/*html').on('change', browserSync.reload);
  gulp.watch('src/assets/js/**/*.js').on('change', browserSync.reload);
}

exports.watch = watch;

const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const concat = require('gulp-concat');
const terser = require('gulp-terser');
const sourcemaps = require('gulp-sourcemaps');
const postcss = require('gulp-postcss');
const cssnano = require('cssnano');
const { src, parallel, dest } = require('gulp');

const jsPath = 'src/assets/js/**/*.js';
const cssPath = 'src/assets/css/**/*.css';

function buildHTML() {
  return src('src/*.html').pipe(gulp.dest('dist'));
}

function exportDocs() {
  return src('src/assets/docs/*').pipe(gulp.dest('dist/assets/docs'));
}

function jsTask() {
  return src(jsPath)
    .pipe(sourcemaps.init())
    .pipe(concat('main.js'))
    .pipe(terser())
    .pipe(sourcemaps.write('.'))
    .pipe(dest('dist/assets/js'));
}

function cssTask() {
  return src(cssPath)
    .pipe(sourcemaps.init())
    .pipe(concat('main.css'))
    .pipe(postcss([cssnano()]))
    .pipe(sourcemaps.write('.'))
    .pipe(dest('dist/assets/css'));
}

function fontsTask() {
  return src('src/assets/fonts/**/*').pipe(gulp.dest('dist/assets/fonts'));
}

function imgTask() {
  return src('src/images/*').pipe(imagemin()).pipe(gulp.dest('dist/images'));
}

exports.exportDocs = exportDocs;
exports.cssTask = cssTask;
exports.jsTask = jsTask;
exports.fontsTask = fontsTask;
exports.imgTask = imgTask;

exports.build = parallel(
  buildHTML,
  cssTask,
  jsTask,
  exportDocs,
  fontsTask,
  imgTask
);

If it worked, why change it?

I didn't look for an alternative, I was introduced to it during a JavaScript course and it was so easy to implement and use that I just went for it. Only one npm install, write a couple of scripts and that was it, no more configuration files.

It did the same thing I was doing with Gulp, except for the images "optimization", and I put that in quotes because I realized after a couple of uses that Gulp was not optimizing anything, I know it's highly likely that I wasn't doing something right but who has the time to find out if I can always optimize all my images with a CDN or manually with a converter.

So how do you use Parcel?

I normally install Parcel in all my local projects. After the npm init and getting my package.json file you got to the CLI and write:

npm install parcel --save-dev

The only thing that remains after this is creating your scripts to run and build your project. So go ahead and add the scripts below to your package.json file:

"scripts": {
    "dev": "parcel index.html",
    "build": "parcel build index.html --dist-dir ./dist"
  }

Now Parcel should be up and running, you can even add SCSS to it if you wanted to, I do it all the time as I prefer the SCSS approach to projects but hey, you do you.