Glup 构建工具

Glup 构建工具将开发流程中让人痛苦或耗时的任务自动化 从而减少你所浪费的时间 创造更大价值 环境搭建 sudo npminstallgu cli g 全局环境 npminstallgu D 本地开发环境 gulphelp 以上是对 gulp 环境的搭建项目准备你可以自己写几个文件测试文件 ps scss 文件 html 里还是要引用对应的 css 文件 我这

将开发流程中让人痛苦或耗时的任务自动化,从而减少你所浪费的时间、创造更大价值。

环境搭建

// 3.x 与 4.x版本 有区别,请注意 [ sudo] npm install gulp-cli -g // 全局环境 npm install gulp -D // 本地开发环境 gulp --help 

以上是对gulp环境的搭建

项目准备

在这里插入图片描述

你可以自己写几个文件测试文件

ps: scss 文件,html里还是要引用对应的css文件。我这块是vscode,找到插件在这里插入图片描述
编辑器下面有 Watch Sass 了解一下

确认打开html页面(css,js运行没有问题)没有问题

gulpfile.js

重头戏

task

每个 gulp 任务(task)都是一个异步的 JavaScript 函数,此函数是一个可以接收 callback 作为参数的函数或在一些gulp特定函数。

src

创建一个流,用于从文件系统读取 Vinyl 对象。

dest

创建一个用于将 Vinyl 对象写入到文件系统的流。

pipe

程序运行管道

watch

实时监听文件改变,及时更新打包

series

将任务函数和/或组合操作组合成更大的操作,这些操作将按顺序依次执行。

代码

version 1.0

简单文件导出

gulpfile.js

// node commonJS规范 const gulp = require("gulp"); const filePath = { 
    enter:"src", output:"dist" } gulp.task("html",function(){ 
    return gulp.src(filePath.enter+"/index.html") .pipe(gulp.dest(filePath.output)) }) gulp.task("image",function(){ 
    return gulp.src(filePath.enter+"/images//*.{jpg,png,jpeg}") .pipe(gulp.dest(filePath.output+"/images")) }) // 监听 gulp.task("watch",function(){ 
    gulp.watch(filePath.enter+"/index.html",gulp.series(["html"])); //监听哪些文件,执行对应的任务 gulp.watch(filePath.enter+"/images//*.{jpg,png,jpeg}",gulp.series(["image"])); }) // 一次性执行多个文件 // // Task function must be specified // watch task has to be a function (optionally generated by using gulp.parallel or gulp.series) // gulp.task("build",["html","image"],function(){ 这种写法是3.x 如果更新4.0版本用如下 gulp.series // console.log("执行完毕") // }); gulp.task("build",gulp.series(["html","image"],function(){ 
    console.log("执行完毕") })) // 运行命令 gulp [task注册的命令] 

插件

常用插件整理

npm install gulp-sass -D // 编译iscss npm install gulp-minify-css -D // 压缩文件 npm install gulp-autoprefixer -D // CSS兼容代码 npm install gulp-rename -D //文件重命名 npm install gulp-concat -D // 合并JS文件 npm install gulp-uglify -D // 压缩JS文件 npm install gulp-clean -D// 清理文件 npm install gulp-connect -D // 连接设置服务器 

version 2.0

gulpfile.js

// node commonJS规范 const gulp = require("gulp"); const sass = require("gulp-sass"); const minifyCSS = require("gulp-minify-css"); const rename = require("gulp-rename"); const uglify = require("gulp-uglify"); const concat = require("gulp-concat"); const connect = require("gulp-connect") const autoprefixer = require('gulp-autoprefixer'); const filePath = { 
    enter:"src", output:"dist" } gulp.task("html",function(){ 
    return gulp.src(filePath.enter+"/index.html") .pipe(gulp.dest(filePath.output)) .pipe(connect.reload()); }) gulp.task("image",function(){ 
    return gulp.src(filePath.enter+"/images//*.{jpg,png,jpeg}") .pipe(gulp.dest(filePath.output+"/images")) .pipe(connect.reload()); }) gulp.task("sass",function(){ 
    return gulp.src(filePath.enter+"/css/*.scss") .pipe(sass()) .pipe(autoprefixer({ 
    cascade: false })) .pipe(gulp.dest(filePath.output+"/css")) .pipe(minifyCSS()) .pipe(rename("index.min.css")) .pipe(gulp.dest(filePath.output+"/css")) .pipe(connect.reload()); }) gulp.task("script",function(){ 
    return gulp.src(filePath.enter+"/js/*.js") .pipe(concat("index.js")) .pipe(gulp.dest(filePath.output+"/js")) .pipe(uglify()) .pipe(rename("index.min.js")) .pipe(gulp.dest(filePath.output+"/js")) .pipe(connect.reload()); }) // 一次性执行多个文件 // // Task function must be specified // watch task has to be a function (optionally generated by using gulp.parallel or gulp.series) // gulp.task("build",["html","image"],function(){ 
    // console.log("执行完毕") // }); gulp.task("build",gulp.series(["html","image","script"],function(){ 
    console.log("执行完毕") })) // 监听 gulp.task("watch",function(){ 
    gulp.watch(filePath.enter+"/index.html",gulp.series(["html"])); //监听哪些文件,执行对应的任务 gulp.watch(filePath.enter+"/images//*.{jpg,png,jpeg}",gulp.series(["image"])); gulp.watch(filePath.enter+"/css/*.scss",gulp.series(["sass"])); gulp.watch(filePath.enter+"/js/*.js",gulp.series(["script"])) }) // 启动服务 gulp.task("server",function(){ 
    connect.server({ 
    root:filePath.output, port:9999, livereload:true }) }) //监听和服务同时启动 gulp.task("default",gulp.parallel(["watch","server"])) 

如果原文件路径和导出文件路径不一样,要在原文件中修改。不要动dist文件下

如果开发环境和生产环境,你可以写两个文件,两个函数,包裹对应下的方法。

简单入门就这样了 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/202755.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午11:22
下一篇 2026年3月19日 下午11:23


相关推荐

  • Qwen API调用时如何处理限流错误?

    Qwen API调用时如何处理限流错误?

    2026年3月13日
    1
  • sql根据字段去重_sql如何去重查询

    sql根据字段去重_sql如何去重查询关于sql去重,我简单谈一下自己的简介,如果各位有建议或有不明白的欢迎多多指出。关于sql去重最常见的有两种方式:DISTINCT和ROW_NUMBER(),当然了ROW_NUMBER()除了去重还有很多其他比较重要的功能,一会我给大家简单说说我自己在实际中用到的。假如有张UserInfo表,如下图:现在我们要去掉完全重复的数据:SELECTDISTINCT*FROMdbo.UserInf…

    2026年4月16日
    3
  • 软件Charle

    软件Charle软件 Charle nbsp 是一个 HTTP 代理服务器 HTTP 监视器 反转代理服务器 它允许一个开发者查看所有连接互联网的 HTTP 通信 这些包括 request response 现 HTTPheaders 包含 cookies 与 caching 信息 nbsp Charle 下载 http www charlesproxy com download nbsp 安装完成 charle

    2026年3月19日
    2
  • 关于解决Codeblocks中文乱码问题

    关于解决Codeblocks中文乱码问题最近有很多博友在问我关于另一篇博文“表白小心心”等问题,我于是又年轻了一把———把代码复制下来,运行了一下,发现了诸多问题,当然不是代码的问题,而是编译器的一些问题。1、Codeblocks乱码Codeblocks在编译时没有任何问题,但是显示出来的汉字字符是乱码的。针对这一问题,我也找了一下解决方法,在这里给大家分享一下。首先打开你的Codeblocks,设置–>编辑器,中英…

    2022年7月26日
    24
  • linux时间同步

    linux时间同步

    2021年8月18日
    74
  • windows如何远程centos桌面_windows远程桌面命令

    windows如何远程centos桌面_windows远程桌面命令作者:【吴业亮】云计算开发工程师博客:http://blog.csdn.net/wylfengyujiancheng前提:CentOS安装桌面,如果无桌面,请执行#yum-ygroupsinstall”GNOMEDesktop”#startx配置源#yuminstallepel*-y安装xrdp#yum–enablerepo=epel-yinst…

    2025年9月26日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号