Skip to content

Instantly share code, notes, and snippets.

@movii
Created June 2, 2018 06:39
Show Gist options
  • Save movii/6c822682fb221073a454aed2e826af82 to your computer and use it in GitHub Desktop.
Save movii/6c822682fb221073a454aed2e826af82 to your computer and use it in GitHub Desktop.
笔记:最近需求中的 Gulp Task 1
var gulp = require('gulp');
var path = require('path');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
gulp.task('minify-js', function() {
// 首先去除我们之后想要合并的 base*.js 和 package 文件的监听
// 然后对剩下文件进行 watch
return gulp.watch([
'**/*.js',
'!static/src/base*.js',
'!node_modules/',
], function(ev) {
// Watch for changes to the files.
if (ev.type !== 'changed') return;
// 通过上文提到的 event 对象
// 配合 path.basename 和 path.extname
// 来获取正在编辑的文件的文件名
var basename = path.basename(ev.path);
var extname = path.extname(basename);
var minname = basename.replace(extname, '.min' + extname);
// 最后第一步进行合并
// 然后压缩输出
return gulp.src([ 'static/src/base*.js', ev.path ])
.pipe(concat(minname))
.pipe(uglify())
.pipe(gulp.dest('static/dist/'));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment