Skip to content

Commit

Permalink
perf(core): Import 'gulp-renew' package for string replacement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinerer committed Sep 29, 2024
1 parent 3ebd8e5 commit fd7e711
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 195 deletions.
43 changes: 17 additions & 26 deletions packages/core/build/html.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const gulp = require('gulp')
const rename = require('gulp-rename')
const {
isPlainObject,
injectEnv
} = require('@pipflow/utils')
const { isPlainObject, injectEnv } = require('@pipflow/utils')

const { pipeline, onDone } = require('../base/utils')
const { htmlMinifyOptions } = require('../base/defaults')
Expand All @@ -13,7 +10,7 @@ const { createSrcOptions, outputFiles, plumber, putProcesses, readManifest } = r
* html 模板引擎
* @param {string} compiler 模板引擎name
* @param {object} compilerOptions 配置项
* @returns
* @returns
*/
function templater(compiler, compilerOptions = {}) {
const { data = {} } = compilerOptions
Expand All @@ -37,14 +34,14 @@ function templater(compiler, compilerOptions = {}) {
nunjucks() {
// nunjucks.compile(data?, options?)
return require('gulp-nunjucks')(data, compilerOptions)
}
},
}
return templaterMap[compiler]?.()
}

/**
* HTML 处理任务
*
*
* @param {Object} options - 配置项
* @param {string|string[]|string[][]|Object.<string, string|string[]>} options.input - 输入文件路径
* @param {string} [options.dest] - 输出目录
Expand All @@ -61,19 +58,12 @@ function templater(compiler, compilerOptions = {}) {
* @throws {Error} 如果`options.input`未定义,则抛出错误
*/
module.exports = function htmlTask(options = {}, done) {
const {
input,
dest,
compiler,
compilerOptions,
minify: htmlMinify,
alias
} = options

const { input, dest, compiler, compilerOptions, minify: htmlMinify, alias } = options

if (!input) {
throw new Error('input is required')
}

const processes = []
const srcOptions = createSrcOptions(options.base, htmlTask)

Expand All @@ -90,10 +80,10 @@ module.exports = function htmlTask(options = {}, done) {
processes.push(rendered)
}
}

// 4. replace 替换别名 (在模板编译后,避免路径不会被替换)
if (isPlainObject(alias) || Array.isArray(alias)) {
processes.push(require('../plugins/renew')(alias))
processes.push(require('gulp-renew')(alias))
}

// 5. 自定义处理流程
Expand All @@ -103,19 +93,23 @@ module.exports = function htmlTask(options = {}, done) {
if (options.assetsInlineLimit?.limit > 0) {
processes.push(require('gulp-dataurl')(options.assetsInlineLimit))
}

// 6. 文件指纹处理
const manifest = readManifest(options)
if (manifest) {
processes.push(require('gulp-rev-rewrite')({ manifest }))
}

// 7. 重命名
processes.push(rename({ extname: '.html' }))

// 8. 压缩处理
if (htmlMinify) {
const minifyOptions = Object.assign({}, htmlMinifyOptions, isPlainObject(htmlMinify) ? htmlMinify : {})
const minifyOptions = Object.assign(
{},
htmlMinifyOptions,
isPlainObject(htmlMinify) ? htmlMinify : {}
)
processes.push(require('../plugins/htmlMinifier')(minifyOptions))
}

Expand All @@ -126,8 +120,5 @@ module.exports = function htmlTask(options = {}, done) {
sourcemap: false,
})

return pipeline(
gulp.src(input, srcOptions),
processes
).on('end', onDone(done))
return pipeline(gulp.src(input, srcOptions), processes).on('end', onDone(done))
}
32 changes: 9 additions & 23 deletions packages/core/build/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ const uglifyjs = require('gulp-terser')
const sourcemaps = require('gulp-sourcemaps')
const filter = require('gulp-filter')
const merge = require('merge2')
const {
isPlainObject,
injectEnv
} = require('@pipflow/utils')
const { isPlainObject, injectEnv } = require('@pipflow/utils')

const { pipeline, onDone } = require('../base/utils')
const { outputFiles, createSrcOptions, plumber, putProcesses, getBasePath } = require('./comm')
Expand All @@ -18,19 +15,15 @@ const { outputFiles, createSrcOptions, plumber, putProcesses, getBasePath } = re
* 处理 script文件 (非 module)
* @param {object} options 配置项
* @param {Function} done 回调
* @returns
* @returns
*/
function compileScript(options = {}, done) {
if (!options.input) {
throw new Error('input is required')
}

const {
input,
alias,
minify: jsMinify
} = options

const { input, alias, minify: jsMinify } = options

const processes = []
const jsFilter = filter('*.{js,mjs}', { restore: true })
const srcOptions = createSrcOptions(options.base, compileScript)
Expand All @@ -39,7 +32,7 @@ function compileScript(options = {}, done) {
// 统一入口方式 (input支持 `string`, `array`, `object`)
const entries = (
isPlainObject(input)
? Object.keys(input).map(name => ({ name, file: input[name] }))
? Object.keys(input).map((name) => ({ name, file: input[name] }))
: [{ name: '', file: input }]
).map(({ name, file }) => {
const baseProcesses = []
Expand All @@ -57,18 +50,15 @@ function compileScript(options = {}, done) {

// 4. replace 别名替换
if (isPlainObject(alias) || Array.isArray(alias)) {
baseProcesses.push(require('../plugins/renew')(alias))
baseProcesses.push(require('gulp-renew')(alias))
}

// 5. 合并文件
if (name) {
baseProcesses.push(concat(path.join(basePath, `${name}.js`)))
}

return pipeline(
gulp.src(file, name ? { ...srcOptions, base: '.' } : srcOptions),
baseProcesses
)
return pipeline(gulp.src(file, name ? { ...srcOptions, base: '.' } : srcOptions), baseProcesses)
})

// 1. 自定义处理流程
Expand Down Expand Up @@ -107,17 +97,13 @@ function compileScript(options = {}, done) {
...options,
filter: jsFilter,
})

return pipeline(
merge(...entries),
processes
).on('end', done)

return pipeline(merge(...entries), processes).on('end', done)
}

/**
* Javascript 处理任务
*
*
* @param {Object} options - 配置项
* @param {string|string[]|string[][]|Object.<string, string|string[]>} options.input - 输入文件路径
* @param {string} [options.dest] - 输出目录
Expand Down
53 changes: 22 additions & 31 deletions packages/core/build/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ const filter = require('gulp-filter')
const postcss = require('../plugins/postcss')
const postcssEnv = require('postcss-preset-env')

const {
isPlainObject,
injectEnv
} = require('@pipflow/utils')
const { isPlainObject, injectEnv } = require('@pipflow/utils')

const { pipeline, onDone } = require('../base/utils')
const { sassDefaultOptions } = require('../base/defaults')
Expand All @@ -21,12 +18,12 @@ const {
plumber,
putProcesses,
getBasePath,
readManifest
readManifest,
} = require('./comm')

/**
* CSS样式 处理任务
*
*
* @param {Object} options - 配置项
* @param {string|string[]|string[][]|Object.<string, string|string[]>} options.input - 输入文件路径
* @param {string} [options.dest] - 输出目录
Expand All @@ -44,13 +41,7 @@ const {
* @throws {Error} 如果`options.input`未定义,则抛出错误
*/
module.exports = function styleTask(options = {}, done) {
const {
input,
compiler,
compilerOptions,
minify: cssMinify,
alias
} = options
const { input, compiler, compilerOptions, minify: cssMinify, alias } = options

if (!input) {
throw new Error('input is required')
Expand All @@ -70,7 +61,7 @@ module.exports = function styleTask(options = {}, done) {
*/
const entries = (
isPlainObject(input)
? Object.keys(input).map(name => ({ name, file: input[name] }))
? Object.keys(input).map((name) => ({ name, file: input[name] }))
: [{ name: '', file: input }]
).map(({ name, file }) => {
const baseProcesses = []
Expand All @@ -93,37 +84,38 @@ module.exports = function styleTask(options = {}, done) {
// 5.2 CSS预处理器
if (compiler === 'sass' || compiler === 'scss') {
const sass = require('gulp-sass')(require('sass'))
const _sassOptions = Object.assign({}, sassDefaultOptions, compilerOptions?.preprocessorOptions)
const _sassOptions = Object.assign(
{},
sassDefaultOptions,
compilerOptions?.preprocessorOptions
)
baseProcesses.push(sass(_sassOptions).on('error', sass.logError))
} else if (compiler === 'less') {
baseProcesses.push(require('gulp-less')(compilerOptions?.preprocessorOptions || {}))
} else if (compiler === 'stylus') {
baseProcesses.push(require('gulp-stylus')(compilerOptions?.preprocessorOptions || {}))
}

// 4. replace 别名替换
if (isPlainObject(alias) || Array.isArray(alias)) {
baseProcesses.push(require('../plugins/renew')(alias))
baseProcesses.push(require('gulp-renew')(alias))
}

// 6. 合并文件
if (name) {
baseProcesses.push(concat(path.join(basePath, `${name}.css`)))
}

return pipeline(
gulp.src(file, name ? { ...srcOptions, base: '.' } : srcOptions),
baseProcesses
)
return pipeline(gulp.src(file, name ? { ...srcOptions, base: '.' } : srcOptions), baseProcesses)
})

/***************************************************************
* 统一自定义处理流程
*/
*/

// 1. 自定义处理流程
putProcesses(processes, options.plugins)

// 2. base64 处理
if (options.assetsInlineLimit?.limit > 0) {
processes.push(require('gulp-dataurl')(options.assetsInlineLimit))
Expand All @@ -142,20 +134,19 @@ module.exports = function styleTask(options = {}, done) {
const minifyOptions = isPlainObject(cssMinify) ? cssMinify : {}
postcssPlugins.push(require('cssnano')(minifyOptions))
}
processes.push(postcss({
_plugins: postcssPlugins
}))
processes.push(
postcss({
_plugins: postcssPlugins,
})
)

// 3. 文件指纹处理 & sourcemaps & 输出文件
outputFiles(processes, {
...options,
filter: cssFilter
filter: cssFilter,
})

return pipeline(
merge(...entries),
processes
).on('end', onDone(done))
return pipeline(merge(...entries), processes).on('end', onDone(done))
}

/** 任务整体流程
Expand Down
3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"gulp-plumber": "^1.2.1",
"gulp-pug": "^5.0.0",
"gulp-rename": "^2.0.0",
"gulp-renew": "workspace:^",
"gulp-rev": "^9.0.0",
"gulp-rev-rewrite": "^5.0.0",
"gulp-sass": "^5.1.0",
Expand All @@ -71,8 +72,6 @@
"gulp-terser": "^2.1.0",
"gulp-zip": "^5.1.0",
"html-minifier-terser": "^7.2.0",
"istextorbinary": "^9.5.0",
"replacestream": "^4.0.3",
"merge2": "^1.4.1",
"plugin-error": "^2.0.1",
"postcss": "^8.4.47",
Expand Down
Loading

0 comments on commit fd7e711

Please sign in to comment.