怎么用gulp压缩js和css文件
1个回答
展开全部
/步骤
首先要确保pc装nodeglobal环境项目文件都install gulp
npm install gulp -g (global环境)
npm install gulp --save-dev (项目环境)
项目install需要gulp插件般压缩需要
npm install gulp-minify-css gulp-concat gulp-uglify gulp-rename del --save-dev
更插件链接找
项目根目录新建gulpfile.jsrequire需要module
var gulp = require('gulp'),
minifycss = require('gulp-minify-css'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
del = require('del');
压缩css
gulp.task('minifycss', function() {
return gulp.src('src/*.css') //压缩文件
.pipe(gulp.dest('minified/css')) //输文件夹
.pipe(minifycss()); //执行压缩
});
压缩js
gulp.task('minifyjs', function() {
return gulp.src('src/*.js')
.pipe(concat('main.js')) //合并所jsmain.js
.pipe(gulp.dest('minified/js')) //输main.js文件夹
.pipe(rename({suffix: '.min'})) //rename压缩文件名
.pipe(uglify()) //压缩
.pipe(gulp.dest('minified/js')); //输
});
执行压缩前先删除文件夹内容
gulp.task('clean', function(cb) {
del(['minified/css', 'minified/js'], cb)
});
默认命令cmd输入gulp执行命令
gulp.task('default', ['clean'], function() {
gulp.start('minifycss', 'minifyjs');
});
要cmd执行gulp即
首先要确保pc装nodeglobal环境项目文件都install gulp
npm install gulp -g (global环境)
npm install gulp --save-dev (项目环境)
项目install需要gulp插件般压缩需要
npm install gulp-minify-css gulp-concat gulp-uglify gulp-rename del --save-dev
更插件链接找
项目根目录新建gulpfile.jsrequire需要module
var gulp = require('gulp'),
minifycss = require('gulp-minify-css'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
del = require('del');
压缩css
gulp.task('minifycss', function() {
return gulp.src('src/*.css') //压缩文件
.pipe(gulp.dest('minified/css')) //输文件夹
.pipe(minifycss()); //执行压缩
});
压缩js
gulp.task('minifyjs', function() {
return gulp.src('src/*.js')
.pipe(concat('main.js')) //合并所jsmain.js
.pipe(gulp.dest('minified/js')) //输main.js文件夹
.pipe(rename({suffix: '.min'})) //rename压缩文件名
.pipe(uglify()) //压缩
.pipe(gulp.dest('minified/js')); //输
});
执行压缩前先删除文件夹内容
gulp.task('clean', function(cb) {
del(['minified/css', 'minified/js'], cb)
});
默认命令cmd输入gulp执行命令
gulp.task('default', ['clean'], function() {
gulp.start('minifycss', 'minifyjs');
});
要cmd执行gulp即
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询