diff --git a/packages/core/build/comm.js b/packages/core/build/comm.js index 82ebf74..1da8c54 100644 --- a/packages/core/build/comm.js +++ b/packages/core/build/comm.js @@ -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 选项 @@ -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 + }, {}) } // 创建文件过滤器 @@ -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) @@ -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 } diff --git a/packages/core/build/html.js b/packages/core/build/html.js index a33be17..9e69b7a 100644 --- a/packages/core/build/html.js +++ b/packages/core/build/html.js @@ -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 模板引擎 @@ -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) @@ -107,6 +99,7 @@ module.exports = function htmlTask(options = {}, done) { } // 6. 文件指纹处理 + const manifest = readManifest(options) if (manifest) { processes.push(revRewrite({ manifest })) } diff --git a/packages/core/build/style.js b/packages/core/build/style.js index 29b8b04..b4e6d69 100644 --- a/packages/core/build/style.js +++ b/packages/core/build/style.js @@ -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) { @@ -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`) * 流程分开处理 为了解决合并文件的问题 @@ -128,6 +120,7 @@ module.exports = function styleTask(options = {}, done) { } // 3. 文件指纹处理 + const manifest = readManifest(options) if (manifest) { processes.push(revRewrite({ manifest })) }