如何添加?

解读

面试官抛出“如何添加?”这一极简提问,在国内前端面试场景中往往是一种“压力测试”。它既可能指“如何新增一条 Gulp 任务”,也可能指“如何向现有管道中追加新插件”,甚至考察你对工程化目录、团队协作规范、CI/CD 集成的理解。回答时必须先澄清语境,再给出可落地的工程级方案,切忌只贴一段代码片段。

知识点

  1. Gulp 核心概念:task、series/parallel、src/dest、watch、Vinyl 文件对象、Node Transform Stream。
  2. 插件生态:gulp-load-plugins 自动加载、gulp-if 条件分支、gulp-plumber 错误捕获、gulp-rename 重命名等。
  3. 工程化规范:任务拆分(dev/build/lib)、配置文件环境化(gulpfile.esm.js + cross-env)、npm script 映射。
  4. 国内落地细节:
    • 公司内网 Nexus 私有源,需配置 .npmrc 镜像与 cafile 证书;
    • 政企项目要求国产化操作系统(麒麟、UOS)+ Node 14 LTS,插件必须预编译或提供 node-gyp 代理;
    • 代码审查平台(如腾讯 TGit、阿里 Aone)强制 eslint-gulp 规则集,提交前需 gulp lint 通过。
  5. 性能优化:gulp-cache、gulp-remember、hard-source 缓存、pnpm 虚拟 store 减少重复依赖。
  6. CI/CD 集成:GitLab Runner 缓存 ~/.pnpm-store、Docker 分层构建、产物哈希上传到阿里云 OSS 并刷新 CDN。

答案

“为了确认您的具体场景,我先补充三个前提:

  1. 您说的‘添加’是指新增一条独立任务,还是在现有管道里追加插件?
  2. 项目是否已使用 gulp4 与 ESM 语法?
  3. 构建产物是否需要兼容国产操作系统与内网部署?

假设您需要新增一个‘压缩 WebP 并生成雪碧图’任务,且项目已升级到 gulp4,我的标准步骤如下:

  1. 安装依赖(内网场景)
    在项目根目录新建 .npmrc,写入

    registry=https://内网.nexus.xxx.com/repository/npm-group/
    strict-ssl=false
    cafile=/etc/pki/ca-trust/source/anchors/company.pem
    

    执行

    pnpm add -D gulp gulp-imagemin imagemin-webp gulp.spritesmith gulp-if
    
  2. 任务文件拆分
    按国内大厂规范,将任务拆到 build/tasks/ 目录,新建 imgSprite.js:

    import { src, dest, series } from 'gulp';
    import imagemin from 'gulp-imagemin';
    import webp from 'imagemin-webp';
    import spritesmith from 'gulp.spritesmith';
    import gulpif from 'gulp-if';
    import { pipeline } from 'stream/promises';
    
    const isProd = process.env.NODE_ENV === 'production';
    
    async function compressWebp() {
      return pipeline(
        src('src/img/**/*.{png,jpg}'),
        imagemin([webp({ quality: 85 })]),
        dest('dist/img')
      );
    }
    
    function generateSprite() {
      return src('src/icons/*.png')
        .pipe(spritesmith({
          imgName: 'sprite.png',
          cssName: '_sprite.scss',
          cssFormat: 'scss',
          algorithm: 'binary-tree',
          padding: 4
        }))
        .pipe(gulpif('*.png', dest('dist/img'), dest('src/styles')));
    }
    
    export const imgSprite = series(compressWebp, generateSprite);
    imgSprite.displayName = 'img:sprite';
    imgSprite.description = '压缩 WebP 并生成雪碧图';
    
  3. 在 gulpfile 中注册

    import { series, parallel } from 'gulp';
    import { imgSprite } from './build/tasks/imgSprite.js';
    
    export const build = series(
      clean,
      parallel(
        compileScss,
        bundleJs,
        imgSprite   // 这里完成“添加”
      ),
      revHash,
      uploadOss
    );
    
  4. 本地验证

    npx gulp img:sprite
    

    确认产物路径、文件体积、雪碧图 SCSS 变量名符合团队 BEM 规范。

  5. CI 集成
    在 .gitlab-ci.yml 中增加缓存与产物上报:

    cache:
      key: $CI_COMMIT_REF_SLUG
      paths:
        - node_modules/
        - ~/.pnpm-store/
    script:
      - pnpm install --frozen-lockfile
      - npx gulp build
    artifacts:
      paths:
        - dist/
      expire_in: 7 days
    

通过以上五步,既完成了任务添加,也满足了国内企业的合规、缓存、CI/CD 要求。”

拓展思考

  1. 动态任务注册:若项目采用“微前端”子包架构,可基于 fast-glob 扫描 build/tasks/*.js,自动 export * 到 gulpfile,实现“零配置”添加任务。
  2. 插件异常降级:国内网络偶发 node-gyp 编译失败,可预置 gulp-replace-image-by-cdn 插件,在 postinstall 阶段检测 sharp 是否安装成功,失败则自动切换为 jimp 纯 JS 方案,保障构建稳定性。
  3. 性能基线:在 GitLab MR 阶段引入 gulp-size 与 performance-budget 插件,若产物体积增长超过 5% 则自动阻断合并,从源头避免“随意添加”导致的包体膨胀。