Skip to content

Commit

Permalink
perf(core): Extract readManifest() & optimize transformHash()
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinerer committed Dec 29, 2023
1 parent 95b715e commit 98f2b48
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
43 changes: 31 additions & 12 deletions packages/core/build/comm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ const {
gulp,
logger,
colors,
symbols
symbols,
readJsonFilesSync
} = require('@pipflow/utils')

// gulp-rev `manifest.json` 保存目录
const revDir = 'revManifest'

const hashRegex = /(-\w{5,}\.\w+)$/

/************************************************
* 生成 gulp.src 选项
* @param {object} options 选项
Expand Down Expand Up @@ -112,14 +115,12 @@ const plumber = (function() {
* 转换文件 hash 方式
*/
function transformHash(json) {
let newObj = {}
for (const key in json) {
if (json.hasOwnProperty(key)) {
let hash = path.basename(json[key]).split('-').pop().split('.')[0]
newObj[key] = key + '?' + hash
}
}
return newObj
return Object.keys(json).reduce((obj, key) => {
const fileName = path.basename(json[key])
const hash = hashRegex.test(fileName) ? fileName.split('-').pop().split('.')[0] : ''
obj[key] = key + '?' + hash
return obj
}, {})
}

// 创建文件过滤器
Expand Down Expand Up @@ -159,7 +160,7 @@ function outputFiles(processes, {
const fileFilter = createFilter(filter)
// 2. 是否生成 hash文件
// fileHash === '?' && processes.push(gulp.dest(dest))
processes.push(gulp.dest(dest)) //!复制原始文件,防止 hash 文件未被替换后导致找不到文件
processes.push(gulp.dest(dest)) //!复制原始文件,防止 hash 文件未被替换后导致找不到文件 (容错)
// 2.2 是否过滤指定文件
if (fileFilter) {
processes.push(fileFilter)
Expand Down Expand Up @@ -194,13 +195,31 @@ function outputFiles(processes, {
}
}

/**
* 获取 manifest文件hash表, 并返回字符串形式内容
*
* @param {object} options - 选项
* @param {string} options.dest - 目标目录
* @param {boolean} options.fileHash - 该任务是否支持 fileHash
* @return {string|null} - hash文件表
*/
function readManifest(options) {
if (options.fileHash) {
// path.posix 统一路径, 兼容window平台
const json = readJsonFilesSync(path.posix.join(options.dest, revDir, '*.json'), { merge: true })
return JSON.stringify(json)
}
return null
}

module.exports = {
revDir,
outputFiles,
createSrcOptions,
getCommonPath,
getBasePath,
plumber,
putProcesses
putProcesses,
transformHash,
outputFiles,
readManifest
}
13 changes: 3 additions & 10 deletions packages/core/build/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ const njkTemplate = require('gulp-nunjucks')
const artTemplate = require('gulp-art-tpl')
const base64 = require('gulp-dataurl')
const {
gulp,
_,
readJsonFilesSync,
gulp,
injectEnv
} = require('@pipflow/utils')

const { pipeline, onDone } = require('../base/utils')
const { htmlMinifyOptions } = require('../base/defaults')
const { revDir, createSrcOptions, outputFiles, plumber, putProcesses } = require('./comm')
const { createSrcOptions, outputFiles, plumber, putProcesses, readManifest } = require('./comm')

/**
* html 模板引擎
Expand Down Expand Up @@ -67,13 +66,6 @@ module.exports = function htmlTask(options = {}, done) {
throw new Error('input is required')
}

let manifest
if (options.fileHash) {
// path.posix 统一路径, 兼容window平台
const json = readJsonFilesSync(path.posix.join(dest, revDir, '*.json'), { merge: true })
manifest = JSON.stringify(json)
}

const processes = []
const srcOptions = createSrcOptions(options)

Expand Down Expand Up @@ -107,6 +99,7 @@ module.exports = function htmlTask(options = {}, done) {
}

// 6. 文件指纹处理
const manifest = readManifest(options)
if (manifest) {
processes.push(revRewrite({ manifest }))
}
Expand Down
15 changes: 4 additions & 11 deletions packages/core/build/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ const {
gulp,
_,
merge,
injectEnv,
readJsonFilesSync
injectEnv
} = require('@pipflow/utils')

const { pipeline, onDone } = require('../base/utils')
const { sassDefaultOptions } = require('../base/defaults')
const {
revDir,
createSrcOptions,
outputFiles,
plumber,
putProcesses,
getBasePath
getBasePath,
readManifest
} = require('./comm')

module.exports = function styleTask(options = {}, done) {
Expand All @@ -50,13 +49,6 @@ module.exports = function styleTask(options = {}, done) {
const basePath = getBasePath(input, options.base || '.') //合并文件后的基础路径
const cssFilter = filter('**/*.css', { restore: true })

let manifest
if (options.fileHash) {
// path.posix 统一路径, 兼容window平台
const json = readJsonFilesSync(path.posix.join(options.dest, revDir, '*.json'), { merge: true })
manifest = JSON.stringify(json)
}

/**
* 统一入口方式 (input支持 `string`, `array`, `object`)
* 流程分开处理 为了解决合并文件的问题
Expand Down Expand Up @@ -128,6 +120,7 @@ module.exports = function styleTask(options = {}, done) {
}

// 3. 文件指纹处理
const manifest = readManifest(options)
if (manifest) {
processes.push(revRewrite({ manifest }))
}
Expand Down

0 comments on commit 98f2b48

Please sign in to comment.