2024-12-02 08:48:09 +00:00
|
|
|
'use strict'
|
|
|
|
const { minify: terserMinify } = require('terser')
|
|
|
|
const { isMatch, logFn } = require('./tools')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
|
2024-12-03 08:18:51 +00:00
|
|
|
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 {
|
2024-12-03 08:18:51 +00:00
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
minifyJs
|
|
|
|
}
|