mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 11:52:07 +08:00
代码压缩
This commit is contained in:
@@ -15,7 +15,9 @@
|
|||||||
"web-start": "egg-scripts start --daemon --title=electron-egg --ignore-stderr --env=prod --workers=1",
|
"web-start": "egg-scripts start --daemon --title=electron-egg --ignore-stderr --env=prod --workers=1",
|
||||||
"web-stop": "egg-scripts stop --title=electron-egg",
|
"web-stop": "egg-scripts stop --title=electron-egg",
|
||||||
"web-dev": "egg-bin dev",
|
"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": {
|
"build": {
|
||||||
"productName": "electron-egg",
|
"productName": "electron-egg",
|
||||||
|
|||||||
@@ -5,41 +5,117 @@ const fs = require('fs');
|
|||||||
const fsPro = require('fs-extra');
|
const fsPro = require('fs-extra');
|
||||||
const UglifyJS = require('uglify-js');
|
const UglifyJS = require('uglify-js');
|
||||||
|
|
||||||
|
class CodeCompress {
|
||||||
console.log('[electron] [code_compress] moving frontend asset to egg public dir');
|
constructor() {
|
||||||
console.log('[electron] [code_compress] start');
|
|
||||||
|
|
||||||
class codeCompress {
|
|
||||||
constructor(options = {}) {
|
|
||||||
this.dirs = [
|
this.dirs = [
|
||||||
'app',
|
'app',
|
||||||
'electron'
|
'electron',
|
||||||
|
'config'
|
||||||
];
|
];
|
||||||
|
this.basePath = path.normalize(__dirname + '/..');
|
||||||
|
this.backupCodeDir = path.join(this.basePath, 'run/backup_code');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 备份 app、electron目录代码
|
* 备份 app、electron目录代码
|
||||||
*/
|
*/
|
||||||
backup () {
|
backup () {
|
||||||
let backupCodeDir = path.normalize(__dirname + '/../backup_code');
|
this.rmBackup();
|
||||||
if (!fs.existsSync(backupCodeDir)) {
|
|
||||||
this.mkdir(backupCodeDir);
|
|
||||||
this.chmodPath(backupCodeDir, '777');
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < this.dirs.length; i++) {
|
for (let i = 0; i < this.dirs.length; i++) {
|
||||||
// check code dir
|
// check code dir
|
||||||
let codeDirPath = path.normalize(__dirname + '/../' + this.dirs[i]);
|
let codeDirPath = path.join(this.basePath, this.dirs[i]);
|
||||||
if (!this.fileExist(codeDirPath)) {
|
if (!fs.existsSync(codeDirPath)) {
|
||||||
console.log('[electron] [code_compress] ERROR %s not exist', codeDirPath);
|
console.log('[electron] [code_compress] [backup] ERROR: %s is not exist', codeDirPath);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy
|
// copy
|
||||||
let targetDir = path.join(backupCodeDir, this.dirs[i]);
|
let targetDir = path.join(this.backupCodeDir, this.dirs[i]);
|
||||||
fsPro.copySync(codeDirPath, targetDir);
|
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);
|
fs.mkdirSync(dirpath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('==> dir end', dirpath);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
chmodPath (path, mode) {
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user