const { src, dest, parallel, series, watch } = require('gulp') const del = require('del') const browserSync = require('browser-sync') //搭服务的插件 const loadPlugins = require('gulp-load-plugins') //将所有的gulp插件引入 const plugins = loadPlugins() const bs = browserSync.create() const data = { menus: [ { name: 'Home', icon: 'aperture', link: 'index.html' }, { name: 'Features', link: 'features.html' }, { name: 'About', link: 'about.html' }, { name: 'Contact', link: '#', children: [ { name: 'Twitter', link: 'https://twitter.com/w_zce' }, { name: 'About', link: 'https://weibo.com/zceme' }, { name: 'divider' }, { name: 'About', link: 'https://github.com/zce' } ] } ], pkg: require('./package.json'), date: new Date() } const clean = () => { return del(['dist', 'temp']) } const style = () => { return src('src/assets/styles/*.scss', { base: 'src' }) .pipe(plugins.sass({ outputStyle: 'expanded' })) .pipe(dest('temp')) .pipe(bs.reload({ stream: true })) //在修改信息后,推到浏览器 } const script = () => { return src('src/assets/scripts/*.js', { base: 'src' }) .pipe(plugins.babel({ presets: ['@babel/preset-env'] })) .pipe(dest('temp')) .pipe(bs.reload({ stream: true })) } const page = () => { return src('src/*.html', { base: 'src' }) .pipe(plugins.swig({ data, defaults: { cache: false } })) // 防止模板缓存导致页面不能及时更新 .pipe(dest('temp')) .pipe(bs.reload({ stream: true })) } const image = () => { return src('src/assets/images/**', { base: 'src' }) .pipe(plugins.imagemin()) .pipe(dest('dist')) } const font = () => { return src('src/assets/fonts/**', { base: 'src' }) .pipe(plugins.imagemin()) .pipe(dest('dist')) } const extra = () => { //不做修改 return src('public/**', { base: 'public' }) .pipe(dest('dist')) } const serve = () => { watch('src/assets/styles/*.scss', style) watch('src/assets/scripts/*.js', script) watch('src/*.html', page) //在开发环境时,没有必要每次都构建图片等,浪费时间,所以在开发环境直接指向源文件就好 // 要分清楚哪些需要执行,哪些不需要执行 // watch('src/assets/images/**', image) // watch('src/assets/fonts/**', font) // watch('public/**', extra) watch([ 'src/assets/images/**', 'src/assets/fonts/**', 'public/**' ], bs.reload) bs.init({ notify: false, //不会再次弹出窗口 port: 2080, // open: false, //是否自动打开浏览器 // files: 'dist/**', 监听修改相关文件夹下面的文件的修改 server: { baseDir: ['temp', 'src', 'public'], routes: { '/node_modules': 'node_modules' //开发环境时路由映射 } } }) } //useref不能单独执行,需要先执行compile,有注释才能执行 const useref = () => { return src('temp/*.html', { base: 'temp' }) .pipe(plugins.useref({ searchPath: ['temp', '.'] })) //创建转换流,将文件当中的构建注释删除, 会将引入的插件转换 // html js css 压缩文件 .pipe(plugins.if(/\.js$/, plugins.uglify())) //if插件,是什么文件,做什么操作 .pipe(plugins.if(/\.css$/, plugins.cleanCss())) .pipe(plugins.if(/\.html$/, plugins.htmlmin({ // 这个压缩html需要单独配置 collapseWhitespace: true, // 删除html内的换行符, minifyCSS: true, // 。。。 minifyJS: true, // 。。。 }))) .pipe(dest('dist')) //读写相同的文件会存在写不进去的问题,所以需要将写出的文件换一个文件夹 } const compile = parallel(style, script, page) //并行 // 上线之前执行的任务 const build = series( //串行 clean, parallel( series(compile, useref), image, font, extra ) ) const develop = series(compile, serve) module.exports = { clean, build, develop }
来源:oschina
链接:https://my.oschina.net/u/3758485/blog/4768245