gulp 使用例子
"uglify-js": "gulp uglifyMini"
import path from "path";
import uglify from "uglify-js";
import fs from 'fs'
const outPath = "../../dist/libs/";
function readAllFilesInFolder(folderPath) {
const files = fs.readdirSync(folderPath);
const filePaths = files.map(file => path.join(folderPath, file));
return filePaths;
}
export async function uglifyMini() {
const allFiles = readAllFilesInFolder(outPath);
allFiles.forEach(workerPathName => {
if (workerPathName.includes('aaa.min.js')) {
const code = fs.readFileSync(workerPathName, 'utf8');
const minifiedCode = uglify.minify(code, {
output: {
beautify: false,
comments: false,
},
mangle: {
toplevel: true,
properties: {
regex: /^_/
}
},
compress: {
collapse_vars: true,
reduce_vars: true,
pure_funcs: ['console.log']
}
}).code;
fs.writeFileSync(workerPathName, minifiedCode);
}
})
}
export async function devMini() {
const allFiles = readAllFilesInFolder(outPath);
allFiles.forEach(workerPathName => {
if (workerPathName.includes('aaa.min.js')) {
const code = fs.readFileSync(workerPathName, 'utf8');
const minifiedCode = uglify.minify(code, {
output: {
beautify: true,
comments: false,
},
}).code;
fs.writeFileSync(workerPathName, minifiedCode);
}
})
}