代码压缩

This commit is contained in:
gaoshuaixing
2021-12-08 18:56:09 +08:00
parent 500cc0eae5
commit 7eb2bb5e46
2 changed files with 107 additions and 20 deletions

View File

@@ -15,7 +15,9 @@
"web-start": "egg-scripts start --daemon --title=electron-egg --ignore-stderr --env=prod --workers=1",
"web-stop": "egg-scripts stop --title=electron-egg",
"web-dev": "egg-bin dev",
"rd": "node ./tools/replace_dist --dist_dir=./frontend/dist"
"rd": "node ./tools/replace_dist --dist_dir=./frontend/dist",
"compress": "node ./tools/code_compress --compress",
"restore": "node ./tools/code_compress --restore"
},
"build": {
"productName": "electron-egg",

View File

@@ -5,41 +5,117 @@ const fs = require('fs');
const fsPro = require('fs-extra');
const UglifyJS = require('uglify-js');
console.log('[electron] [code_compress] moving frontend asset to egg public dir');
console.log('[electron] [code_compress] start');
class codeCompress {
constructor(options = {}) {
class CodeCompress {
constructor() {
this.dirs = [
'app',
'electron'
'electron',
'config'
];
this.basePath = path.normalize(__dirname + '/..');
this.backupCodeDir = path.join(this.basePath, 'run/backup_code');
}
/**
* 备份 app、electron目录代码
*/
backup () {
let backupCodeDir = path.normalize(__dirname + '/../backup_code');
if (!fs.existsSync(backupCodeDir)) {
this.mkdir(backupCodeDir);
this.chmodPath(backupCodeDir, '777');
}
this.rmBackup();
for (let i = 0; i < this.dirs.length; i++) {
// check code dir
let codeDirPath = path.normalize(__dirname + '/../' + this.dirs[i]);
if (!this.fileExist(codeDirPath)) {
console.log('[electron] [code_compress] ERROR %s not exist', codeDirPath);
let codeDirPath = path.join(this.basePath, this.dirs[i]);
if (!fs.existsSync(codeDirPath)) {
console.log('[electron] [code_compress] [backup] ERROR: %s is not exist', codeDirPath);
return
}
// copy
let targetDir = path.join(backupCodeDir, this.dirs[i]);
fsPro.copySync(codeDirPath, targetDir);
let targetDir = path.join(this.backupCodeDir, this.dirs[i]);
console.log('[electron] [code_compress] [backup] targetDir:', targetDir);
if (!fs.existsSync(targetDir)) {
this.mkdir(targetDir);
this.chmodPath(targetDir, '777');
}
fsPro.copySync(codeDirPath, targetDir);
}
console.log('[electron] [code_compress] [backup] success');
}
/**
* 还原代码
*/
restore () {
for (let i = 0; i < this.dirs.length; i++) {
let codeDirPath = path.join(this.backupCodeDir, this.dirs[i]);
let targetDir = path.join(this.basePath, this.dirs[i]);
fsPro.copySync(codeDirPath, targetDir);
}
console.log('[electron] [code_compress] [restore] success');
};
/**
* 压缩代码
*/
compress () {
for (let i = 0; i < this.dirs.length; i++) {
let codeDirPath = path.join(this.basePath, this.dirs[i]);
this.compressLoop(codeDirPath);
}
console.log('[electron] [code_compress] [compress] success');
};
compressLoop (dirPath) {
let files = [];
if (fs.existsSync(dirPath)) {
files = fs.readdirSync(dirPath);
files.forEach((file, index) => {
let curPath = dirPath + '/' + file;
if (fs.statSync(curPath).isDirectory()) {
this.compressLoop(curPath);
} else {
if (path.extname(curPath) === '.js') {
this.miniFile(curPath);
}
}
});
}
}
miniFile (file) {
let code = fs.readFileSync(file, "utf8");
const options = {
mangle: {
toplevel: false,
},
};
let result = UglifyJS.minify(code, options);
fs.writeFileSync(file, result.code, "utf8");
}
/**
* 格式化参数
*/
formatArgvs () {
// argv
let argvs = [];
for (let i = 0; i < process.argv.length; i++) {
const tmpArgv = process.argv[i]
if (tmpArgv.indexOf('--') !== -1) {
argvs.push(tmpArgv.substr(2))
}
}
return argvs;
}
/**
* 移除备份
*/
rmBackup () {
fs.rmdirSync(this.backupCodeDir, {recursive: true});
return;
}
/**
@@ -73,8 +149,6 @@ class codeCompress {
fs.mkdirSync(dirpath);
}
}
console.log('==> dir end', dirpath);
};
chmodPath (path, mode) {
@@ -94,6 +168,17 @@ class codeCompress {
};
}
const cc = new CodeCompress();
let argvs = cc.formatArgvs();
console.log('[electron] [code_compress] argvs:', argvs);
if (argvs.indexOf('compress') != -1) {
cc.backup();
cc.compress();
} else if (argvs.indexOf('restore') != -1) {
cc.restore();
}
module.exports = CodeCompress;