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 }