前端进阶之路-四、如何利用gulp创建web前端开发框架(终)
大家好,我是“廖某某前端日志”,今天为大家讲讲前端的进阶技能。
scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
package.json
文件的一个片段,里面的scripts
字段是一个对象。它的每一个属性,对应一段脚本。比如,test
命令对应的脚本是输出一串报错终止脚本。
npm run test
"scripts": {
"test": "gulp dev --env development"
},
改成gulp执行开发环境的命令,然后在控制台中用npm再执行一下上面的命令,
"scripts": {
"dev": "gulp dev --env development",
"build": "gulp dev --env production"
},
const gulp = require('gulp');
const stylus = require('gulp-stylus');
const uglify = require('gulp-uglify');
const cleanCss = require('gulp-clean-css');
const postcss = require('gulp-postcss');
const postcssrc = require('postcss-load-config');
const connect = require('gulp-connect');
const plumber = require('gulp-plumber'); // 避免出错task终止
const minimist = require('minimist'); // 用于命令行传参数
const gulpif = require('gulp-if'); // 用于命令行传参
const cleanCSS = require('gulp-clean-css'); // 缩小css文件
const changed = require('gulp-newer'); // 增量更新
const babel = require('gulp-babel');
const tinypng_nokey = require('gulp-tinypng-nokey'); //压缩图片--免key
const opn = require('opn'); // 开启浏览器
const del = require('del'); // 删除dist文件夹
const { getPort } = require('./utils');
const Config = require('./config');
// 命令行传参数
const knownOptions = {
string: 'env',
default: { env: process.env.NODE_ENV || 'production' }
};
const options = minimist(process.argv.slice(2), knownOptions);
// 检查端口冲突
const checkPort = async () => {
Config.connect.port = await getPort();
};
// 开启文件服务器
const openServer = async () => await connect.server(Config.connect);
// 自动打开浏览器
const openBrowser = async () => {
console.log('打开浏览器');
const { host, port } = Config.connect;
const url = `http://${host}:${port}/view`;
opn(url);
};
// HTML文件
const htmls = () => {
return gulp
.src('src/view/**/*.html')
.pipe(changed('src/view/**/*.html'))
.pipe(plumber())
.pipe(gulp.dest('dist/view/'))
.pipe(connect.reload());
};
// CSS文件
const styles = () => {
return gulp
.src('src/css/**/*.styl')
.pipe(changed('src/css/**/*.styl'))
.pipe(plumber())
.pipe(stylus())
.pipe(gulpif(options.env === 'production', cleanCSS()))
.pipe(gulp.dest('dist/css/'))
.pipe(connect.reload());
};
// 图片文件
const images = () => {
return gulp
.src('src/images/**/*')
.pipe(changed('src/images/**/*'))
.pipe(plumber())
.pipe(gulp.dest('dist/images/'))
.pipe(connect.reload());
};
// 图片压缩
const imgTiny = () => {
return gulp
.src('src/images/**/*')
.pipe(changed('src/images/**/*'))
.pipe(tinypng_nokey())
.pipe(gulp.dest('dist/images/'))
.pipe(connect.reload());
}
// JS文件
const scripts = () => {
return gulp
.src('src/js/**/*.js')
.pipe(changed('src/js/**/*.js'))
.pipe(plumber())
.pipe(babel()) //ES6转换
.pipe(gulpif(options.env === 'production', uglify()))
.pipe(gulp.dest('dist/js/'))
.pipe(connect.reload());
};
//检测文件是否有更新
const watchFiles = () => {
gulp.watch('src/view/**/*.html', htmls); //检测到html文件更新,更新版本号
gulp.watch('src/css/**/*.styl', styles);
gulp.watch('src/images/*', images);
gulp.watch('src/js/**/*.js', scripts);
};
// 删除dist目录
const clean = () => {
return del(['dist']);
};
// 开发环境-不压缩JS、css和图片
const devbuild = gulp.series(
checkPort,
gulp.parallel(openServer, htmls, styles, images, scripts),
openBrowser,
watchFiles
);
// 生产环境-不运行服务、不开启浏览器、代码压缩和图片压缩
const prodbuild = gulp.series(
clean,
gulp.parallel(htmls, styles, imgTiny, scripts),
);
const build = options.env === 'production' ? prodbuild : devbuild; //判断是开发环境还是生产环境
options.env === 'production' ? null : watchFiles();
module.exports = build;
const gulp = require('gulp');
const build = require('./main/gulpfile.js');
gulp.task('dev', build);
const gulp = require('gulp');
const build = require('./main/main.js');
gulp.task('dev', build);
const { host } = require('./utils');
const folder = {
src: 'src/', // 源文件目录
dist: 'dist/' // 文件处理目录
};
const distFiles = folder.dist + '**'; // 目标路径下的所有文件
const Config = {
connect: {
root: 'dist',
livereload: true,
port: 1234,
host
},
src: folder.src,
dist: folder.dist,
distFiles: distFiles,
html: {
src: folder.src + 'view/**/*.html',
dist: folder.dist + 'view/'
},
css: {
src: folder.src + 'css/**/*.styl',
avoid: '!' + folder.src + 'css/stylus/*',
dist: folder.dist + 'css/'
},
js: {
src: folder.src + 'js/**/*.js',
dist: folder.dist + 'js/'
},
images: {
src: folder.src + 'images/**/*',
dist: folder.dist + 'images/'
},
libs: {
src: folder.src + 'common/**/*',
dist: folder.dist + 'common/'
}
};
Config; =
const gulp = require('gulp');
const stylus = require('gulp-stylus');
const uglify = require('gulp-uglify');
const cleanCss = require('gulp-clean-css');
const postcss = require('gulp-postcss');
const postcssrc = require('postcss-load-config');
const connect = require('gulp-connect');
const plumber = require('gulp-plumber'); // 避免出错task终止
const minimist = require('minimist'); // 用于命令行传参数
const gulpif = require('gulp-if'); // 用于命令行传参
const cleanCSS = require('gulp-clean-css'); // 缩小css文件
const changed = require('gulp-newer'); // 增量更新
const babel = require('gulp-babel');
const tinypng_nokey = require('gulp-tinypng-nokey'); //压缩图片--免key
const opn = require('opn'); // 开启浏览器
const del = require('del'); // 删除dist文件夹
const { getPort } = require('./utils');
const Config = require('./config');
// 命令行传参数
const knownOptions = {
string: 'env',
default: { env: process.env.NODE_ENV || 'production' }
};
const options = minimist(process.argv.slice(2), knownOptions);
// 检查端口冲突
const checkPort = async () => {
Config.connect.port = await getPort();
};
// 开启文件服务器
const openServer = async () => await connect.server(Config.connect);
// 自动打开浏览器
const openBrowser = async () => {
console.log('打开浏览器');
const { host, port } = Config.connect;
const url = `http://${host}:${port}/view`;
opn(url);
};
// HTML文件
const htmls = () => {
return gulp
.src(Config.html.src)
.pipe(changed(Config.html.src))
.pipe(plumber())
.pipe(gulp.dest(Config.html.dist))
.pipe(connect.reload());
};
// CSS文件
const styles = async () => {
const config = await postcss();
return gulp
.src([Config.css.src, Config.css.avoid])
.pipe(changed(Config.css.src))
.pipe(plumber())
.pipe(stylus())
.pipe(postcss(config.plugins, config.options))
.pipe(gulpif(options.env === 'production', cleanCSS()))
.pipe(gulp.dest(Config.css.dist))
.pipe(connect.reload());
};
// 图片文件
const images = () => {
return gulp
.src(Config.images.src)
.pipe(changed(Config.images.src))
.pipe(plumber())
.pipe(gulp.dest(Config.images.dist))
.pipe(connect.reload());
};
// 图片压缩
const imgTiny = () => {
return gulp
.src(Config.images.src)
.pipe(tinypng_nokey())
.pipe(gulp.dest(Config.images.dist))
.pipe(connect.reload());
}
// JS文件
const scripts = () => {
return gulp
.src(Config.js.src)
.pipe(changed(Config.js.src))
.pipe(plumber())
.pipe(babel()) //ES6转换
.pipe(gulpif(options.env === 'production', uglify()))
.pipe(gulp.dest(Config.js.dist))
.pipe(connect.reload());
};
//检测文件是否有更新
const watchFiles = () => {
gulp.watch(Config.html.src, htmls); //检测到html文件更新,更新版本号
gulp.watch(Config.css.src, styles);
gulp.watch(Config.images.src, images);
gulp.watch(Config.js.src, scripts);
};
// 删除dist目录
const clean = () => {
return del(['dist']);
};
// 开发环境-不压缩JS、css和图片
const devbuild = gulp.series(
checkPort,
gulp.parallel(openServer, htmls, styles, images, scripts),
openBrowser,
watchFiles
);
// 生产环境-不运行服务、不开启浏览器、代码压缩和图片压缩
const prodbuild = gulp.series(
clean,
gulp.parallel(htmls, styles, imgTiny, scripts),
);
const build = options.env === 'production' ? prodbuild : devbuild; //判断是开发环境还是生产环境
options.env === 'production' ? null : watchFiles();
module.exports = build;
node_modules/
dist/
格言:人生没有彩排,每天都是现场直播!加油!