gulp 记录

TODO

  • typescript 编写gulpfile
  • gulpfile分割
  • eslint
  • sourcemaps

eslint https://www.cnblogs.com/jiaoshou/p/12209597.html

sourcemaps https://www.cnblogs.com/jiaoshou/p/12209597.html

安装

# 安装
npm install --save-dev gulp
# 查看版本
F:\code\gulp-study>npx gulp --version
CLI version: 2.3.0
Local version: 4.0.2

F:\code\gulp-study>

文档

https://www.gulpjs.com.cn/docs/getting-started/quick-start/

hello world

错误版

基本的hello world如下所示

const gulp = require('gulp')

gulp.task('hello', () => {
    console.log('hello world')
})

执行结果如下

F:\code\gulp-study>npx gulp hello
[07:25:46] Using gulpfile F:\code\gulp-study\gulpfile.js
[07:25:46] Starting 'hello'...
hello world
[07:25:46] The following tasks did not complete: hello
[07:25:46] Did you forget to signal async completion?

F:\code\gulp-study>

正确版

解决办法如下

参考 https://www.gulpjs.com.cn/docs/getting-started/async-completion/

const gulp = require('gulp')

gulp.task('hello', (cb) => {
    console.log('hello world')

    cb()
})

运行如下

F:\code\gulp-study>npx gulp hello
[07:26:55] Using gulpfile F:\code\gulp-study\gulpfile.js
[07:26:55] Starting 'hello'...
hello world
[07:26:55] Finished 'hello' after 3.4 ms

F:\code\gulp-study>

区别:

  • 使用gulp流时,return
  • 使用nodejs时,使用callback

gulp 基本内容

公开任务与私有任务

在这里插入图片描述

const gulp = require('gulp')

exports.hello = gulp.task('hello', (cb) => {
    console.log('hello world')
    cb()
})


gulp.task('copyfile',(cb)=>{
    console.log('copyfile')
})

执行结果

F:\code\gulp-study>npx gulp --tasks
[15:10:50] Tasks for F:\code\gulp-study\gulpfile.js
[15:10:50] ├── hello
[15:10:50] └── copyfile

F:\code\gulp-study>npx gulp hello
[15:10:58] Using gulpfile F:\code\gulp-study\gulpfile.js
[15:10:58] Starting 'hello'...
hello world
[15:10:58] Finished 'hello' after 7.96 ms

F:\code\gulp-study>npx gulp copyfile
[15:11:05] Using gulpfile F:\code\gulp-study\gulpfile.js
[15:11:05] Starting 'copyfile'...
copyfile
[15:11:05] The following tasks did not complete: copyfile
[15:11:05] Did you forget to signal async completion?

F:\code\gulp-study>

可读流与可写流

gulp.src创建可读流
gulp.dest创建可写流

顺序与并行

在这里插入图片描述

gulp 项目基本目标

项目构建类型

  • 静态页面
  • js库
  • 与php整合
  • 与spring boot整合

项目构建能力

  • 单模块
  • 多模块
  • profile
  • 增量构建

静态页面 + profile

基本版

只能用于开发环境,不考虑任何代码的合并与压缩
目录结构
在这里插入图片描述

gulpfile.js

const gulp = require('gulp')
const del = require('del')
const stylus = require('gulp-stylus')
const browserSync = require('browser-sync').create()
const reload = browserSync.reload
const autoprefixer = require('gulp-autoprefixer')
const fileinclude = require('gulp-file-include')

// 端口
const port = 8099

// 输出目录
const dist_path = './dist'
// 源码目录
const src_path = './src'
// 源文件目录
const source_path = 'source'
// 中间文件目录,编译后的css,js等文件在此目录,html文件中引用的css,js等也在此目录下
const static_path = 'static'

const html_source = 'html/**/*.html'
const html_include_path = 'html/include'
const html_prefix = '@@'
const html_dist_path = 'source/html'

const style_source_path = 'stylus/**/*'
const style_src_path = 'css'

const script_source_path = 'js/**/*'
const script_src_path = 'js'
const script_dist_path = 'js'

const libs_source_path = 'libs/**/*'
const libs_src_path = 'libs'
const libs_dist_path = 'libs'

const img_source_path = 'image/**/*'
const img_src_path = 'image'
const img_dist_path = 'image'

function joinPath(paths) {
    if (Array.isArray(paths)) {
        return paths.join('/')
    }
    throw new Error('参数不是数组')
}

exports.clean = gulp.task('clean', function () {
	return del([dist_path, joinPath([src_path,static_path])])
})

exports.html = gulp.task('html', () => {
    return gulp.src(joinPath([src_path, source_path, html_source]))
        .pipe(fileinclude({
            prefix: html_prefix,
            basepath: joinPath([src_path,source_path,html_include_path])
        }))
        .pipe(gulp.dest(joinPath([dist_path, html_dist_path])))
        .pipe(browserSync.reload({stream: true}))

})

exports.style = gulp.task('style', () => {
    return gulp.src(joinPath([src_path, source_path, style_source_path]))
        .pipe(stylus())
        .pipe(autoprefixer())
        .pipe(gulp.dest(joinPath([src_path, static_path, style_src_path])))
        .pipe(gulp.dest(joinPath([dist_path, static_path,style_src_path])))
        .pipe(browserSync.reload({stream: true}))
})

exports.script = gulp.task('script',()=>{
    return gulp.src(joinPath([src_path,source_path,script_source_path]))
        .pipe(gulp.dest(joinPath([src_path,static_path,script_src_path])))
        .pipe(gulp.dest(joinPath([dist_path,static_path,script_dist_path])))
        .pipe(browserSync.reload({stream: true}))
})

exports.copyLibs = gulp.task('copyLibs',()=>{
    return gulp.src(joinPath([src_path,source_path,libs_source_path]))
        .pipe(gulp.dest(joinPath([src_path,static_path,libs_src_path])))
        .pipe(gulp.dest(joinPath([dist_path,static_path,libs_dist_path])))
        .pipe(browserSync.reload({stream: true}))
})

exports.copyImg = gulp.task('copyImg',()=>{
    return gulp.src(joinPath([src_path,source_path,img_source_path]))
        .pipe(gulp.dest(joinPath([src_path,static_path,img_src_path])))
        .pipe(gulp.dest(joinPath([dist_path,static_path,img_dist_path])))
        .pipe(browserSync.reload({stream: true}))
})



function watchFile() {
    gulp.watch(joinPath([src_path, source_path, html_source]), gulp.parallel('html'))
    gulp.watch(joinPath([src_path, source_path, style_source_path]), gulp.parallel('style'))
    gulp.watch(joinPath([src_path, source_path, script_source_path]), gulp.parallel('script'))
    gulp.watch(joinPath([src_path, source_path, libs_source_path]), gulp.parallel('copyLibs'))
    gulp.watch(joinPath([src_path, source_path, img_source_path]), gulp.parallel('copyImg'))
}

exports.watch = gulp.task('watch', (cb) => {
    watchFile()
    cb()
})

exports.server = gulp.task('server', (done) => {
    browserSync.init({
        port: port,
        server: {
            baseDir: [dist_path, src_path]
        }
    })
    done()
})

gulp.task('default', gulp.series(gulp.parallel('html','style','script','copyLibs','copyImg'), 'server', (done) => {
    watchFile()
    gulp.watch(dist_path + '/**/*', reload)
    done()
}))

可用版

实现功能

  • eslint
  • sourcemaps
  • 增量构建
  • 命令行参数

目录结构
在这里插入图片描述

package.json

{
  "name": "gulp-study",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev" : "npx gulp clean && npx gulp --env=dev"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "browser-sync": "^2.26.14",
    "del": "^6.0.0",
    "eslint": "^7.18.0",
    "gulp": "^4.0.2",
    "gulp-autoprefixer": "^7.0.1",
    "gulp-cached": "^1.1.1",
    "gulp-clean-css": "^4.3.0",
    "gulp-debug": "^4.0.0",
    "gulp-eslint": "^6.0.0",
    "gulp-file-include": "^2.3.0",
    "gulp-remember": "^1.0.1",
    "gulp-rename": "^2.0.0",
    "gulp-sourcemaps": "^3.0.0",
    "gulp-stylus": "^2.7.0",
    "gulp-uglify": "^3.0.2",
    "through2": "^4.0.2",
    "yargs": "^16.2.0"
  }
}

gulpfile.js

const gulp = require('gulp')
const debug = require('gulp-debug')
const del = require('del')
const stylus = require('gulp-stylus')
const browserSync = require('browser-sync').create()
const reload = browserSync.reload
const autoprefixer = require('gulp-autoprefixer')
const fileinclude = require('gulp-file-include')
const cached = require('gulp-cached')
const remember = require('gulp-remember')
const uglify = require('gulp-uglify')
const rename = require('gulp-rename')
const cleanCss = require('gulp-clean-css')
const sourcemaps = require('gulp-sourcemaps')
const through = require('through2')
const arguments = require('yargs').argv
const eslint = require('gulp-eslint')


const isDev = 'dev' === arguments.env
const isTest = 'test' === arguments.env
const isProd = 'prod' === arguments.env

// 端口
const port = 8099

// 输出目录
const dist_path = './dist'
// 源码目录
const src_path = './src'
// 源文件目录
const source_path = 'source'
// 中间文件目录,编译后的css,js等文件在此目录,html文件中引用的css,js等也在此目录下
const static_path = 'static'

/* html */
const html_source = 'html/**/*.html'
const html_include_path = 'html/include'
const html_prefix = '@@'
const html_dist_path = 'source/html'

/* stylus */
const style_source_path = 'stylus/**/*'
const style_src_path = 'css'

/* javascript */
const script_source_path = 'js/**/*'
const script_src_path = 'js'
const script_dist_path = 'js'

/* 三方库 */
const libs_source_path = 'libs/**/*'
const libs_src_path = 'libs'
const libs_dist_path = 'libs'

/* 图片 */
const img_source_path = 'image/**/*'
const img_src_path = 'image'
const img_dist_path = 'image'

const joinPath = (paths) => {
    if (Array.isArray(paths)) {
        return paths.join('/')
    }
    throw new Error('参数不是数组')
}

const noop = () => {
    return through.obj()
}


const devProfile = (task) => {
    return isDev ? task : noop()
}

const testProfile = (task) => {
    return isTest ? task : noop()
}

const prodProfile = (task) => {
    return isProd ? task : noop()
}


exports.test = gulp.task('test', (cb) => {
    console.log(isDev)
    console.log(isTest)
    console.log(isProd)
    cb()
})

exports.clean = gulp.task('clean', function () {
    return del([dist_path, joinPath([src_path, static_path])])
})

exports.html = gulp.task('html', () => {
    return gulp.src(joinPath([src_path, source_path, html_source]))
        .pipe(fileinclude({
            prefix: html_prefix,
            basepath: joinPath([src_path, source_path, html_include_path])
        }))
        .pipe(gulp.dest(joinPath([dist_path, html_dist_path])))
        .pipe(browserSync.reload({stream: true}))

})

exports.style = gulp.task('style', () => {
    return gulp.src(joinPath([src_path, source_path, style_source_path]))
        .pipe(cached('styleTask'))
        .pipe(devProfile(sourcemaps.init()))
        .pipe(stylus())
        .pipe(autoprefixer())
        .pipe(rename({suffix: '.min'}))
        .pipe(cleanCss())
        .pipe(devProfile(sourcemaps.write('./', {addComment: true})))
        .pipe(remember('styleTask'))
        .pipe(gulp.dest(joinPath([src_path, static_path, style_src_path])))
        .pipe(gulp.dest(joinPath([dist_path, static_path, style_src_path])))
        .pipe(browserSync.reload({stream: true}))
})

exports.script = gulp.task('script', () => {
    return gulp.src(joinPath([src_path, source_path, script_source_path]))
        .pipe(cached('scriptTask'))
        .pipe(eslint({configFle:'./.eslintrc.js'}))
        .pipe(eslint.format())
        .pipe(devProfile(sourcemaps.init()))
        .pipe(uglify())
        .pipe(rename({suffix: '.min'}))
        .pipe(devProfile(sourcemaps.write('./', {addComment: true})))
        .pipe(remember('scriptTask'))
        .pipe(gulp.dest(joinPath([src_path, static_path, script_src_path])))
        .pipe(gulp.dest(joinPath([dist_path, static_path, script_dist_path])))
        .pipe(browserSync.reload({stream: true}))
})

exports.copyLibs = gulp.task('copyLibs', () => {
    return gulp.src(joinPath([src_path, source_path, libs_source_path]))
        .pipe(gulp.dest(joinPath([src_path, static_path, libs_src_path])))
        .pipe(gulp.dest(joinPath([dist_path, static_path, libs_dist_path])))
        .pipe(browserSync.reload({stream: true}))
})

exports.copyImg = gulp.task('copyImg', () => {
    return gulp.src(joinPath([src_path, source_path, img_source_path]))
        .pipe(gulp.dest(joinPath([src_path, static_path, img_src_path])))
        .pipe(gulp.dest(joinPath([dist_path, static_path, img_dist_path])))
        .pipe(browserSync.reload({stream: true}))
})


function watchFile() {
    gulp.watch(joinPath([src_path, source_path, html_source]), gulp.parallel('html'))
    gulp.watch(joinPath([src_path, source_path, style_source_path]), gulp.parallel('style'))
    gulp.watch(joinPath([src_path, source_path, script_source_path]), gulp.parallel('script'))
    gulp.watch(joinPath([src_path, source_path, libs_source_path]), gulp.parallel('copyLibs'))
    gulp.watch(joinPath([src_path, source_path, img_source_path]), gulp.parallel('copyImg'))
}

exports.watch = gulp.task('watch', (cb) => {
    watchFile()
    cb()
})

exports.server = gulp.task('server', (done) => {
    browserSync.init({
        port: port,
        open: false,
        server: {
            baseDir: [dist_path]
        }
    })
    done()
})

gulp.task('default', gulp.series(gulp.parallel('html', 'style', 'script', 'copyLibs', 'copyImg', 'watch'), 'server', (done) => {
    watchFile()
    gulp.watch(dist_path + '/**/*', reload)
    done()
}))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源
大学生在线租房平台管理系统按照操作主体分为管理员和用户。管理员的功能包括报修管理、报修评价管理、字典管理、房东管理、房屋管理、房屋收藏管理、房屋留言管理、房屋租赁管理、租房论坛管理、公告信息管理、留言板管理、用户管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 大学生在线租房平台管理系统可以提高大学生在线租房平台信息管理问题的解决效率,优化大学生在线租房平台信息处理流程,保证大学生在线租房平台信息数据的安全,它是一个非常可靠,非常安全的应用程序。 管理员权限操作的功能包括管理公告,管理大学生在线租房平台信息,包括房屋管理,培训管理,报修管理,薪资管理等,可以管理公告。 房屋管理界面,管理员在房屋管理界面中可以对界面中显示,可以对房屋信息的房屋状态进行查看,可以添加新的房屋信息等。报修管理界面,管理员在报修管理界面中查看报修种类信息,报修描述信息,新增报修信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息。
基于hal库的OLED显示屏驱动C语言实现源码.zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值