javascript - .pipe() 函数在 gulp 中返回什么?

标签 javascript node.js npm gulp npmjs

在下面的代码片段中,.pipe(gulpIf('*.css', cssnano())) 的输入和输出是什么?

gulp.task('useref', function(){
  return gulp.src('app/*.html')
    .pipe(useref())
    .pipe(gulpIf('*.js', uglify()))
    .pipe(gulpIf('*.css', cssnano()))
    .pipe(gulp.dest('dist'))
}); 

API 文档 ( link ) 说 .pipe(destination) 返回对目标流的引用以设置链,如果是 .pipe(gulpIf('*.js' , uglify())) 会将流返回到缩小的 .js 文件,如何将其通过管道传输到 .pipe(gulpIf('*.css', cssnano()))

最佳答案

Gulp 只是一个任务运行器,具有相当简单的基本功能。它的力量来自广泛的第三方包生态系统,您自己的代码片段至少使用了四个。我说四个是因为它显示在您的 gulpfile.js 源代码中; gulp 本身有 13 direct runtime dependencies :

"dependencies": {
    "archy": "^1.0.0",
    "chalk": "^1.0.0",
    "deprecated": "^0.0.1",
    "gulp-util": "^3.0.0",
    "interpret": "^1.0.0",
    "liftoff": "^2.1.0",
    "minimist": "^1.1.0",
    "orchestrator": "^0.3.0",
    "pretty-hrtime": "^1.0.0",
    "semver": "^4.1.0",
    "tildify": "^1.0.0",
    "v8flags": "^2.0.2",
    "vinyl-fs": "^0.3.0"
},

...以及类似数量的直接开发依赖项。

gulp 传递的流由 vinyl-fs 提供并且不代表单个文件而是虚拟文件系统。

关于您的代码,您肯定只从 HTML 文件开始:

gulp.src('app/*.html')

... 但紧接着你执行了一个名为 gulp-useref 的第三方包:

.pipe(useref())

根据其文档:

will parse the build blocks in the HTML, replace them and pass those files through. Assets inside the build blocks will be concatenated and passed through in a stream as well.

换句话说,它解析 HTML 文件以识别/生成 Assets 文件,并将它们添加到流中以供进一步处理。

关于javascript - .pipe() 函数在 gulp 中返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44064898/

相关文章:

node.js - NPM 安装在 Docker 容器内失败,但在带有企业代理的主机上运行

node.js - npm install 中的 --save-dev 选项是什么意思?

node.js - 如何在 Mongoose 模型中实现事务概念?

javascript - NodeJs/Express 获取 ID//按下的提交按钮的名称

javascript - 谷歌图表容器未定义..但div在那里

javascript - 使用 ng-repeat 添加多个 View 实例时的范围问题

javascript - 使用 JavaScript 替换 txt 文件中的一行

javascript - 编写依赖父构建工具的 Vue 组件的正确方法

javascript - jQuery 窗口调整大小并小于特定宽度

javascript - Breeze 续集 - 警告续集版本已过时 3.x 有一个关键的安全修复