hexo-yam/lib/js.js

84 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-12-02 08:48:09 +00:00
'use strict'
const { minify: terserMinify } = require('terser')
2024-12-04 08:41:23 +00:00
const { isMatch, match, logFn } = require('./tools')
2024-12-02 08:48:09 +00:00
/**
* @param {string} str
* @param {{ path: string }} data
*/
async function minifyJs(str, data) {
const hexo = this
const options = hexo.config.minify.js
if (!str) return str
const path = data.path
const { exclude, globOptions, verbose, ...jsOptions } = options
2024-12-02 08:48:09 +00:00
if (isMatch(path, exclude, globOptions)) return str
// Terser doesn't like unsupported options
delete jsOptions.enable
delete jsOptions.priority
// Old option, retained to avoid crash when upgrading to v4
delete jsOptions.logger
try {
const code = await terserMinify(str, jsOptions).code
2024-12-02 08:48:09 +00:00
if (verbose) logFn.call(this, str, code, path, 'js')
return code
} catch (err) {
throw new Error(`Path: ${path}\n${err}`)
}
}
2024-12-04 08:41:23 +00:00
function minifyJsWithMap() {
const hexo = this
const options = hexo.config.minify.js
const { parse } = require('path')
const route = hexo.route
const routeList = route.list()
/** @type {{ exclude: string[] }} */
const { exclude, globOptions, verbose, ...jsOptions } = options
const include = ['*.js', ...exclude.map(x => `!${x}`)]
// Terser doesn't like unsupported options
delete jsOptions.enable
delete jsOptions.priority
// Old option, retained to avoid crash when upgrading to v4
delete jsOptions.logger
return Promise.all((match(routeList, include, globOptions)).map(path => {
return new Promise((/** @type {(value: void) => void} */ resolve, reject) => {
const assetPath = route.get(path)
let assetTxt = ''
assetPath.on('data', chunk => (assetTxt += chunk))
assetPath.on('end', async () => {
if (assetTxt.length) {
try {
const { base, ext, name } = parse(path)
jsOptions.sourceMap = {
...jsOptions.sourceMap,
filename: base,
asObject: true,
url: `${base}.map`
}
const { code, map } = await terserMinify(assetTxt, { ...jsOptions })
if (verbose) logFn.call(this, assetTxt, result, path, 'js')
route.set(path, code)
map.sources = [`${name}.source${ext}`]
route.set(`${path}.map`, JSON.stringify(map))
} catch (err) {
reject(new Error(`Path: ${path}\n${err}`))
}
}
resolve()
})
})
}))
}
2024-12-02 08:48:09 +00:00
module.exports = {
2024-12-04 08:41:23 +00:00
minifyJs,
minifyJsWithMap
2024-12-02 08:48:09 +00:00
}