gulpfile.js文件中的代码

const {
	watch, src, dest, parallel
} = require('gulp');
const connect = require('gulp-connect');
const htmlclean = require('gulp-htmlclean');
const cssnano = require('cssnano');
const autoprefixer = require('autoprefixer');
const postcss = require('gulp-postcss');
const less = require('gulp-less');
const imgMin = require('gulp-imagemin');
const strip = require('gulp-strip-debug');
const uglify = require('gulp-uglify');
const concat = require('gulp-concat');

let devMode = process.env.NODE_ENV == 'development';
let folder = {
	src: "src/", //开发目录文件夹
	dist: "dist/" //压缩打包后目录
}

function html() {
	let page = src('src/html/*.html');
	page.pipe(connect.reload());
	if (!devMode) {
		page.pipe(htmlclean());
	}
	return page.pipe(dest('dist/html'));
}

function images() {
	let page = src('src/images/*');
	page.pipe(imgMin())
	return page.pipe(dest('dist/images/'));
}

function js() {
	let page = src('src/js/*');
	page.pipe(connect.reload());
	if (!devMode) {
		page.pipe(strip())
			.pipe(uglify());
	}
	page.pipe(concat('main.js'));
	return page.pipe(dest('dist/js/'));
}

function css() {
	let options = [autoprefixer(), cssnano()];
	let page = src('src/css/*');
	page.pipe(connect.reload())
		.pipe(less());
	if (!devMode) {
		page.pipe(postcss([cssnano()]));
	}
	page.pipe(postcss([autoprefixer()]))
	return page.pipe(dest('dist/css'));
}

function server() {
	return connect.server({
		port: '8090',
		livereload: true
	});
}

watch('src/html/*', html);
watch('src/images/*', images);
watch('src/js/*', js);
watch('src/css/*', css);

exports.default = parallel(html, css, images, js, server);

博主联系方式:

  • 微信:34419369
  • QQ: 34419369
  • 公众号:前方录
  • 有什么不懂的地方欢迎联系我,帮到你是我会很开心

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注