官术网_书友最值得收藏!

Creating your first Gulp configuration file

Gulp tasks are defined by the contents of the project's gulpfile.js file. This is a JavaScript program, so the same skills you have with JavaScript will apply here. Furthermore, it's executed by Node.js, so if you have any Node.js knowledge, you can use it to your advantage.

Tip

This file should be placed in the root directory of your project and must be named gulpfile.js.

The first few lines of your Gulp configuration file will require the Gulp plugins you'll need to complete the tasks. The following lines will then specify how the various tasks need to be performed. For example, a very simple configuration might look as follows:

var gulp = require("gulp");
gulp.task("copy-files", function () {
  gulp.src(["./src/**/*"])
      .pipe(gulp.dest("./build"));
});

This configuration only performs one task: it moves all the files contained within src/ to build/. In many ways, this is the simplest form of a build workflow, but it's a bit too simple for our purposes.

Tip

Note the pattern we used to match all the files. If you need to see the documentation on what patterns are supported, see https://www.npmjs.com/package/glob.

To execute the task, you can execute gulp copy-files. Gulp would then execute the task and copy all the files from src/ to build/.

What makes Gulp so powerful is the concept of task composition. Tasks can depend on any number of other tasks and those tasks can depend on yet more tasks. This makes it easy to create complex workflows out of simpler pieces. Furthermore, each task is asynchronous, so it is possible for many tasks with no shared dependencies to operate in parallel.

Each task, as you can see in the prior code, is comprised of a selection of a series of source files (src()), optionally performing some additional processing on each file (via pipe()) and then writing those files to a destination path (dest()). If no additional processing is specified (as in the prior example), Gulp will simply copy the files that match the wildcard pattern. The beauty of streams, however, is that one can execute any number of transformations before the final data is saved. So, the workflows can become very complex.

Now that you've seen a simple task, let's get into some more complicated tasks in the next section.

主站蜘蛛池模板: 乌拉特后旗| 托里县| 乐陵市| 资阳市| 南岸区| 广灵县| 浪卡子县| 凤庆县| 汉沽区| 安宁市| 黎平县| 东阿县| 岱山县| 翼城县| 威信县| 隆子县| 宕昌县| 巩义市| 曲靖市| 大荔县| 方山县| 福泉市| 珲春市| 施甸县| 隆子县| 保康县| 临漳县| 昭通市| 紫金县| 慈溪市| 和顺县| 彭水| 南京市| 翼城县| 丹凤县| 澎湖县| 香港| 原阳县| 衢州市| 南乐县| 武安市|