Nodejs 最近看了下gulp.js,着实不错
Nodejs 最近看了下gulp.js,着实不错
###gulp.js学习笔记
gulp.js 是类似grunt的一个自动化工具,相对比于grunt的config文件的复杂,gulp.js只需要5个函数即可。(参考地址:<a src=“http://www.codersgrid.com/2014/01/11/gulp-js-streaming-build-tool-beats-grunt-js/”>http://www.codersgrid.com/2014/01/11/gulp-js-streaming-build-tool-beats-grunt-js/</a>)
####gulp的一个demo
<pre>
<code>
//gulpfile
var gulp = require(‘gulp’); gulp.task(‘default’,function(){ //register a task //some code }) </code> </pre>
gulp中的方法很有意思的一点在于支持pipe管道,这个方法比grunt的命令更加清晰,下面我们看下用法
<pre>
<code>
/*
gulpfile for my Ros project
2014/02/23
*/
var gulp = require(‘gulp’)
,uglify = require(‘gulp-uglify’)
,concat = require(‘gulp-concat’)
,size = require(‘gulp-size’);
var filePath = { appScriptPath:{ src:‘app/scripts/*/.js’, dest:‘app/build/js’ } } gulp.task(‘default’,function(){ return gulp.src(filePath.appScriptPath.src) .pipe(uglify()) .pipe(concat(‘main.js’)) .pipe(gulp.dest(filePath.appScriptPath.dest)); });
</code> </pre> 代码就不更多解释了,太清晰了…完全不用注释…
####实际项目中需要的module #####增量编译 gulp-changed gulp-cached gulp-changed <pre> <code> var gulp = require(‘gulp’); var changed = require(‘gulp-changed’); var ngmin = require(‘gulp-ngmin’); // just as an example
var SRC = ‘src/*.js’; var DEST = ‘dist’;
gulp.task(‘default’, function () { gulp.src(SRC) .pipe(changed(DEST)) //配置DEST目录 // ngmin will only get the files that // changed since the last time it was run .pipe(ngmin()) .pipe(gulp.dest(DEST)); }); </code> </pre> 这两个模块有点问题,尚在研究,还没有跑起来增量编译,不过从速度上来讲,确实很不错,而且代码很易懂
刚接触Grunt,最近又听到这个,发展好快啊,一起研究吧
好东西!项目中用下,不错的
还是觉得Grunt好一些,插件比较丰富,自定义也方便点
关于插件,gulp不是问题,因为大部分的库都支持 Stream,所以可以直接拿来当作 Gulp的插件来用
gulp 用起来很顺手
很爽
gulp-changed 怎么理解?
Nodejs 最近看了下gulp.js,着实不错。Gulp.js 是一个基于流(stream)的自动化构建工具,相比 Grunt.js 更为简单且高效。Gulp 通过简单的配置可以完成很多常见的任务,比如压缩、合并、检查等。
Gulp 的一个简单示例
首先,确保你已经安装了 Node.js 和 npm。然后通过 npm 安装 Gulp 全局和本地:
npm install --global gulp-cli
npm install --save-dev gulp
接下来创建一个简单的 Gulp 文件 gulpfile.js
:
// gulpfile.js
const gulp = require('gulp');
const uglify = require('gulp-uglify');
const concat = require('gulp-concat');
const paths = {
scripts: {
src: 'app/scripts/**/*.js',
dest: 'app/build/js'
}
};
gulp.task('scripts', function() {
return gulp.src(paths.scripts.src)
.pipe(uglify()) // 压缩 JS 文件
.pipe(concat('all.min.js')) // 合并所有 JS 文件
.pipe(gulp.dest(paths.scripts.dest)); // 输出到目标目录
});
gulp.task('default', gulp.series('scripts')); // 默认任务调用 scripts 任务
以上代码展示了如何使用 Gulp 来压缩和合并多个 JavaScript 文件。首先定义文件路径,然后通过 gulp.src
获取源文件,接着使用 pipe
方法将文件流传递给不同的处理插件(如 uglify
和 concat
),最后输出到指定目录。
实际项目中需要的模块
在实际项目中,我们经常需要处理增量编译的问题,以提高构建效率。这时可以使用 gulp-changed
和 gulp-cached
模块。
安装这些模块:
npm install --save-dev gulp-changed gulp-cached
然后你可以这样使用它们:
const gulp = require('gulp');
const changed = require('gulp-changed');
const cached = require('gulp-cached');
const uglify = require('gulp-uglify');
const SRC = 'src/**/*.js';
const DEST = 'dist';
gulp.task('scripts', function() {
return gulp.src(SRC)
.pipe(cached('scripts')) // 只处理上次更改过的文件
.pipe(changed(DEST)) // 只输出发生变化的文件
.pipe(uglify()) // 压缩 JS 文件
.pipe(gulp.dest(DEST)); // 输出到目标目录
});
gulp.task('default', gulp.series('scripts'));
这段代码展示了如何结合 gulp-cached
和 gulp-changed
插件来实现增量编译,从而提升构建效率。