How to Migrate to Gulp.js 4.0

SitePoint
How to Migrate to Gulp.js 4.0

Despite competition from webpack and Parcel, Gulp.js remains one of the most popular JavaScript task runners. Gulp.js is configured using code which makes it a versatile, general-purpose option. As well as the usual transpiling, bundling and live reloading, Gulp.js could analyze a database, render a static site, push a Git commit, and post a Slack message with a single command.

For an introduction to Gulp, take a look at the following:

An Introduction to Gulp.js How to Use Gulp.js to Automate Your CSS Tasks Develop WordPress Themes Faster with Gulp Gulp.js 4.0

Gulp.js 3.x has been the default for around half a decade. Until recently, npm install gulp would have installed 3.9.1 — the version referenced in the tutorials above.

Gulp.js 4.0 has been available throughout that time, but had to be explicitly installed with npm install gulp@next. This was partly owing to ongoing development and because Gulp.js 4 gulpfile.js configuration files are not compatible with those developed for version 3.

On December 10, 2018, Gulp.js 4.0 was announced as the default and published to npm. Anyone using npm install gulp on a new project will receive version 4.

Is it Necessary to Migrate to Gulp.js 4?

No. Gulp.js 3 has been deprecated and is unlikely to receive further updates, but it can still be used. Existing projects won’t update unless the version is explicitly changed in the dependencies section of package.json. For example:

"dependencies": { "gulp": "^4.0.0" }

You an also install Gulp.js 3 in new projects using:

npm install gulp@^3.9.1

It’s possibly best to stick with Gulp.js 3.x if you have a particularly complex, mission-critical build system.

However, existing Gulp.js plugins should be compatible and most gulpfile.js configurations can be migrated in an hour or two. There are several benefits to upgrading, which will become apparent throughout this tutorial.

Upgrade to Gulp.js 4.0

Update your package.json dependencies as shown above, then run npm install to upgrade. You can also update the command-line interface using npm i gulp-cli -g, although this hasn’t changed at the time of writing.

To check the installation, enter gulp -v at the command line:

$ gulp -v [15:15:04] CLI version 2.0.1 [15:15:04] Local version 4.0.0 Migrating gulpfile.js

Running any task is now likely to raise scary-looking errors. For example:

AssertionError [ERR_ASSERTION]: Task function must be specified at Gulp.set [as _setTask] (/node_modules/undertaker/lib/set-task.js:10:3) at Gulp.task (/node_modules/undertaker/lib/task.js:13:8) at /gulpfile.js:102:8 at Object.<anonymous> (/gulpfile.js:146:3) at Module._compile (internal/modules/cjs/loader.js:688:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3) at Module.require (internal/modules/cjs/loader.js:636:17) at require (internal/modules/cjs/helpers.js:20:18) at execute (/gulp-cli/lib/versioned/^4.0.0/index.js:36:18) at Liftoff.handleArguments (/gulp-cli/index.js:175:63) at Liftoff.execute (/gulp-cli/node_modules/liftoff/index.js:203:12) at module.exports (/gulp-cli/node_modules/flagged-respawn/index.js:51:3) at Liftoff.<anonymous> (/gulp-cli/node_modules/liftoff/index.js:195:5)

It’s daunting, but you can ignore everything except the first reference of gulpfile.js, which shows the line where an error was encountered (102 in this example).

Fortunately, most of these errors are caused by the same type of problem. The following sections use the CSS tasks tutorial code as an example. The code is available on GitHub and provides the original Gulp.js 3 gulpfile.js and the migrated Gulp.js 4 equivalent.

The post How to Migrate to Gulp.js 4.0 appeared first on SitePoint.

Comments

Popular posts from this blog

Visual Studio Code: A Power User’s Guide

6+ Best Websites for Free Fonts in 2020

How to Contribute to Open Source TypeScript Projects