如何解決uniapp開發環境下小程式包體積可能存在過大的問題
阿新 • • 發佈:2021-01-13
import { outputFileSync, readFileSync } from "fs-extra";
var UglifyJS = require('uglify-es');
var requireContext = require('node-require-context')
var fs = require('fs-extra');
var my_path = 'D:/專案/多平臺/dist/dev/mp-weixin'
export interface FileTool {
allFileList?: any[]
}
export class FileTool implements FileTool {
allFileList?: any[] = []
options = {
// 解析配置
parse: {},
// 壓縮配置
compress: {
drop_console: true,
},
// 混淆配置
mangle: {},
// 輸出配置
output: {
comments: false, // 移除註釋
},
sourceMap: {},
ecma: 8, // specify one of: 5, 6, 7 or 8
keep_fnames: false, // 防止丟棄或損壞函式名
keep_classnames: false,
toplevel: false, // 混淆最高作用域中的變數和函式名
warnings: false,
}
constructor() {
}
// 獲取所有檔案
getAllFileList() {
const context = requireContext(my_path, true, /\.js$/)
let arr: any[] = []
context.keys().forEach((moduleId: string, index: number) => {
let data = readFileSync(moduleId);
let str = String(data) as never;
let temp = {
path: moduleId,
code: str
}
arr.push(temp)
})
this.allFileList = arr
return this.allFileList;
}
start() {
// new Queque({
// quque: this.getAllFileList(),
// limitTime: 200,
// callBack: this.mini
// });
this.getAllFileList().forEach(ele => {
this.mini(ele)
})
}
// 壓縮檔案
mini(val: any) {
let result = UglifyJS.minify(val.code, this.options);
outputFileSync(val.path, result.code)
}
// 壓縮成功
success() {
}
// 壓縮失敗
error() {
}
}