#
使用插件¥Using Plugins
Gulp 插件是 Node 转换流,封装了在管道中转换文件的常见行为 - 通常使用 .pipe()
方法放置在 src()
和 dest()
之间。他们可以更改通过流的每个文件的文件名、元数据或内容。
¥Gulp plugins are Node Transform Streams 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.
来自 npm 的插件 - 使用 "gulpplugin" 和 "gulpfriendly" 关键字 - 可以在 插件搜索页面 上浏览和搜索。
¥Plugins from npm - using the "gulpplugin" and "gulpfriendly" keywords - can be browsed and searched on the plugin search page.
每个插件应该只做少量的工作,因此你可以像构建块一样将它们连接起来。你可能需要将它们组合起来才能获得所需的结果。
¥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.
#
你需要插件吗?¥Do you need a plugin?
并非 gulp 中的所有内容都应该使用插件。它们是一种快速入门方法,但许多操作可以通过使用模块或库来改进。
¥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.
插件应该始终转换文件。使用(非插件)节点模块或库进行任何其他操作。
¥Plugins should always transform files. Use a (non-plugin) Node module or library for any other operations.
#
条件插件¥Conditional plugins
由于插件操作不应该识别文件类型,因此你可能需要像 gulp-if 这样的插件来转换文件子集。
¥Since plugin operations shouldn't be file-type-aware, you may need a plugin like gulp-if to transform subsets of files.
#
内联插件¥Inline plugins
内联插件是你通过编写所需行为在 gulpfile 中定义的一次性转换流。
¥Inline plugins are one-off Transform Streams you define inside your gulpfile by writing the desired behavior.
在两种情况下创建内联插件会很有帮助:
¥There are two situations where creating an inline plugin is helpful:
而不是创建和维护自己的插件。
¥Instead of creating and maintaining your own plugin.
而不是复刻现有的插件来添加你想要的功能。
¥Instead of forking a plugin that exists to add a feature you want.