|
| 1 | +<!-- front-matter |
| 2 | +id: using-plugins |
| 3 | +title: Using Plugins |
| 4 | +hide_title: true |
| 5 | +sidebar_label: Using Plugins |
| 6 | +--> |
| 7 | + |
| 8 | +# Using Plugins |
| 9 | + |
| 10 | +Gulp plugins are [Node Transform Streams][through2-docs] that encapsulate common behavior to transform files in a pipeline - often placed between `src()` and `dest()` using the `.pipe()` method. They can change the filename, metadata, or contents of every file that passes through the stream. |
| 11 | + |
| 12 | +Plugins from npm - using the "gulpplugin" and "gulpfriendly" keywords - can be browsed and searched on the [plugin search page][gulp-plugin-site]. |
| 13 | + |
| 14 | +Each plugin should only do a small amount of work, so you can connect them like building blocks. You may need to combine a bunch of them to get the desired result. |
| 15 | + |
| 16 | +```js |
| 17 | +const { src, dest } = require('gulp'); |
| 18 | +const uglify = require('gulp-uglify'); |
| 19 | +const rename = require('gulp-rename'); |
| 20 | + |
| 21 | +exports.default = function() { |
| 22 | + return src('src/*.js') |
| 23 | + // The gulp-uglify plugin won't update the filename |
| 24 | + .pipe(uglify()) |
| 25 | + // So use gulp-rename to change the extension |
| 26 | + .pipe(rename({ extname: '.min.js' })) |
| 27 | + .pipe(dest('output/')); |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Do you need a plugin? |
| 32 | + |
| 33 | +Not everything in gulp should use plugins. They are a quick way to get started, but many operations are improved by using a module or library instead. |
| 34 | + |
| 35 | +```js |
| 36 | +const { rollup } = require('rollup'); |
| 37 | + |
| 38 | +// Rollup's promise API works great in an `async` task |
| 39 | +exports.default = async function() { |
| 40 | + const bundle = await rollup.rollup({ |
| 41 | + input: 'src/index.js' |
| 42 | + }); |
| 43 | + |
| 44 | + return bundle.write({ |
| 45 | + file: 'output/bundle.js', |
| 46 | + format: 'iife' |
| 47 | + }); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +Plugins should always transform files. Use a (non-plugin) Node module or library for any other operations. |
| 52 | + |
| 53 | +```js |
| 54 | +const del = require('delete'); |
| 55 | + |
| 56 | +exports.default = function(cb) { |
| 57 | + // Use the `delete` module directly, instead of using gulp-rimraf |
| 58 | + del(['output/*.js'], cb); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Conditional plugins |
| 63 | + |
| 64 | +Since plugin operations shouldn't be file-type-aware, you may need a plugin like [gulp-if][gulp-if-package] to transform subsets of files. |
| 65 | + |
| 66 | +```js |
| 67 | +const { src, dest } = require('gulp'); |
| 68 | +const gulpif = require('gulp-if'); |
| 69 | +const uglify = require('gulp-uglify'); |
| 70 | + |
| 71 | +function isJavaScript(file) { |
| 72 | + // Check if file extension is '.js' |
| 73 | + return file.extname === '.js'; |
| 74 | +} |
| 75 | + |
| 76 | +exports.default = function() { |
| 77 | + // Include JavaScript and CSS files in a single pipeline |
| 78 | + return src(['src/*.js', 'src/*.css']) |
| 79 | + // Only apply gulp-uglify plugin to JavaScript files |
| 80 | + .pipe(gulpif(isJavaScript, uglify())) |
| 81 | + .pipe(dest('output/')); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Inline plugins |
| 86 | + |
| 87 | +Inline plugins are one-off Transform Streams you define inside your gulpfile by writing the desired behavior. |
| 88 | + |
| 89 | +There are two situations where creating an inline plugin is helpful: |
| 90 | +* Instead of creating and maintaining your own plugin. |
| 91 | +* Instead of forking a plugin that exists to add a feature you want. |
| 92 | + |
| 93 | +```js |
| 94 | +const { src, dest } = require('gulp'); |
| 95 | +const uglify = require('uglify-js'); |
| 96 | +const through2 = require('through2'); |
| 97 | + |
| 98 | +exports.default = function() { |
| 99 | + return src('src/*.js') |
| 100 | + // Instead of using gulp-uglify, you can create an inline plugin |
| 101 | + .pipe(through2.obj(function(file, _, cb) { |
| 102 | + if (file.isBuffer()) { |
| 103 | + const code = uglify.minify(file.contents.toString()) |
| 104 | + file.contents = Buffer.from(code) |
| 105 | + } |
| 106 | + })) |
| 107 | + .pipe(dest('output/')); |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +[gulp-plugin-site]: https://gulpjs.com/plugins/ |
| 112 | +[through2-docs]: https://github.com/rvagg/through2 |
| 113 | +[gulp-if-package]: https://www.npmjs.com/package/gulp-if |
0 commit comments