提交 cf7dc5f0 作者: 毛细亚

更新代码

上级 0d3894c7
module.exports = { module.exports = {
simple: { simple: {
compact: true, compact: true,
controlFlowFlattening: false, deadCodeInjection: true,
deadCodeInjection: false, deadCodeInjectionThreshold: 1,
debugProtection: false, // controlFlowFlattening: false,
debugProtectionInterval: false, // deadCodeInjection: false,
disableConsoleOutput: true, // debugProtection: false,
identifierNamesGenerator: "hexadecimal", // debugProtectionInterval: false,
log: false, // disableConsoleOutput: true,
renameGlobals: false, // identifierNamesGenerator: "hexadecimal",
rotateStringArray: true, // log: false,
selfDefending: true, // renameGlobals: false,
stringArray: true, // rotateStringArray: true,
stringArrayEncoding: false, // selfDefending: true,
stringArrayThreshold: 0.75, // stringArray: true,
unicodeEscapeSequence: false, // stringArrayEncoding: false,
// stringArrayThreshold: 0.75,
// unicodeEscapeSequence: false,
}, },
ordinary: { ordinary: {
compact: true, compact: true,
......
const fileutil = require('./file-util');
const path = fileutil.path;
const fs = fileutil.fs;
const WXFS = wx.getFileSystemManager();
class BinaryProcessor {
onLoadStart(host, resource) {
const {
root,
url
} = resource;
return new Promise((resolve, reject) => {
let xhrURL = url.indexOf('://') >= 0 ? url : root + url;
RES['getVirtualUrl']?xhrURL = RES['getVirtualUrl'](xhrURL):'';
if (!path.isRemotePath(xhrURL)&&true) {
//本地加载
try {
const content = WXFS.readFileSync(xhrURL);
resolve(content);
} catch (e) {
resolve(null);
}
return;
}
if (needCache(xhrURL)) {
//缓存加载
const targetFilename = path.getLocalFilePath(xhrURL);
if (fs.existsSync(targetFilename)) {
//缓存命中
let data = WXFS.readFileSync(path.getWxUserPath(targetFilename));
resolve(data);
return;
}
loadBinary(xhrURL).then((content) => {
//写入本地
const dirname = path.dirname(targetFilename);
fs.mkdirsSync(dirname);
fs.writeSync(targetFilename, content);
let needRead = needReadFile();
if (needRead) {
content = WXFS.readFileSync(path.getWxUserPath(targetFilename));
}
resolve(content);
}).catch((e) => {
reject(e);
});
} else {
//不用缓存直接加载
loadBinary(xhrURL).then((content) => {
resolve(content);
}).catch((e) => {
reject(e);
});
}
});
}
onRemoveStart(host, resource) {
return Promise.resolve();
}
}
let wxSystemInfo;
function needReadFile() {
if (!wxSystemInfo) {
wxSystemInfo = wx.getSystemInfoSync();
}
let sdkVersion = wxSystemInfo.SDKVersion;
let platform = wxSystemInfo.system.split(" ").shift();
return (sdkVersion <= '2.2.3') && (platform == 'iOS');
}
function loadBinary(xhrURL) {
return new Promise((resolve, reject) => {
wx.request({
url: xhrURL,
method: 'get',
responseType: 'arraybuffer',
success: function success(_ref) {
resolve(_ref.data)
},
fail: function fail(_ref2) {
const error = new RES.ResourceManagerError(1001, xhrURL);
console.error('load binary error', xhrURL);
reject(error)
}
});
});
}
/**
* 由于微信小游戏限制只有50M的资源可以本地存储,
* 所以开发者应根据URL进行判断,将特定资源进行本地缓存
*/
function needCache(url) {
if (url.indexOf(`map${js_gameVars.resVersion}`) >= 0) {
return true;
} else {
return false;
}
}
const processor = new BinaryProcessor();
RES.processor.map("bin", processor);
...@@ -8,11 +8,17 @@ ...@@ -8,11 +8,17 @@
"webpack": "^5.64.0" "webpack": "^5.64.0"
}, },
"devDependencies": { "devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"clean-webpack-plugin": "^4.0.0", "clean-webpack-plugin": "^4.0.0",
"cross-env": "^5.1.1", "cross-env": "^5.1.1",
"cross-port-killer": "^1.0.1", "cross-port-killer": "^1.0.1",
"javascript-obfuscator": "^3.0.0", "javascript-obfuscator": "^3.0.0",
"uglifyjs-webpack-plugin": "^2.2.0", "uglifyjs-webpack-plugin": "^2.2.0",
"webpack": "^5.64.0",
"webpack-cli": "^4.9.1", "webpack-cli": "^4.9.1",
"webpack-obfuscator": "^3.5.0" "webpack-obfuscator": "^3.5.0"
} }
......
let obj = {name:'小明',age:'12',action(){
console.log('[ 在上学 ]')
}}
obj.action()
\ No newline at end of file
/**
* 封装微信小游戏的文件系统
*/
const wxFs = wx.getFileSystemManager();
const WX_ROOT = wx.env.USER_DATA_PATH + "/";
const { gameVersion } = "../config/config.js";
function walkFile(dirname, callback) {
const files = wxFs.readdirSync(dirname);
for (let f of files) {
const file = dirname + "/" + f;
const stat = wxFs.statSync(file);
if (stat.isDirectory()) {
walkFile(file, callback);
} else {
callback(file);
// console.log("当前文件被清理: " + file);
}
}
}
function walkDir(dirname, callback) {
// console.log(dirname + "执行 walkDir--");
const files = wxFs.readdirSync(dirname)
// console.log(dirname + "目录中有: ");
// console.log(files);
for (let f of files) {
const file = dirname + "/" + f;
try {
const stat = wxFs.statSync(file);
if (stat.isDirectory()) {
walkDir(file, callback);
callback(file)
}
} catch (e) {
console.warn(file + "不存在");
console.warn(e);
}
}
}
let fs_cache = {};
export const fs = {
/**
* 遍历删除文件夹
* removeRoot:根目录是否删除
*/
remove: (dirname, removeRoot) => {
if (!fs.existsSync(dirname))
return;
const globalDirname = WX_ROOT + dirname;
walkFile(globalDirname, (file) => {
wxFs.unlinkSync(file);
let p = file.replace(WX_ROOT, "");
p = path.normailze(p);
if (fs_cache[p]) {
fs_cache[p] = 0;
}
})
walkDir(globalDirname, (dir) => {
wxFs.rmdirSync(dir);
let p = dir.replace(WX_ROOT, "");
p = path.normailze(p);
if (fs_cache[p]) {
fs_cache[p] = 0;
}
})
if (removeRoot) {
try {
wxFs.rmdirSync(globalDirname, false);
} catch (e) {
console.warn("removeRoot fail");
console.error(e)
}
}
},
/**
* 检查文件是否存在
*/
existsSync: (p) => {
const cache = fs_cache[p];
if (cache == 0) {
return false;
} else if (cache == 1) {
return true;
} else {
try {
wxFs.accessSync(WX_ROOT + p);
p = path.normailze(p);
if (p) {
fs_cache[p] = 1;
}
return true;
} catch (e) {
p = path.normailze(p);
fs_cache[p] = 0;
return false;
}
}
},
writeSync: (p, content) => {
p = path.normailze(p);
fs_cache[p] = 1;
wxFs.writeFileSync(WX_ROOT + p, content);
},
readSync: (p, format) => {
format = format || 'utf-8';
return wxFs.readFileSync(WX_ROOT + p, format);
},
/**
* 创建文件夹
*/
mkdirsSync: (p) => {
// console.log(`mkdir: ${p}`)
const time1 = Date.now();
if (!fs.existsSync(p)) {
const dirs = p.split('/');
let current = "";
for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i]
current += dir + "/";
if (!fs.existsSync(current)) {
let p = path.normailze(current);
fs_cache[p] = 1;
wxFs.mkdirSync(WX_ROOT + current)
}
}
} else {
return;
}
const time2 = Date.now() - time1;
// console.log(`mkdir: ${p} ${time2} ms`)
},
/**
* 解压 zip 文件
*/
unzip: (zipFilePath, targetPath) => {
zipFilePath = WX_ROOT + zipFilePath;
targetPath = WX_ROOT + targetPath;
return new Promise((resolve, reject) => {
//console.log(zipFilePath)
wxFs.unzip({
zipFilePath,
targetPath,
success: () => {
//console.log('success')
resolve();
},
fail(e) {
//console.log(e)
reject(e)
}
})
})
},
/////
setFsCache: (p, value) => {
fs_cache[p] = value;
}
}
export const path = {
dirname: (p) => {
const arr = p.split("/");
arr.pop();
return arr.join('/');
},
isRemotePath: (p) => {
return p.indexOf("http://") == 0 || p.indexOf("https://") == 0;
},
normailze: (p) => {
let arr = p.split("/");
let original = p.split("/");
for (let a of arr) {
if (a == '' || a == null) {
let index = original.indexOf(a);
original.splice(index, 1);
}
}
if (original.length > 0) {
return original.join('/');
}
},
// 根据key值表获取本地缓存路径
// 通过本函数可将网络地址转化为本地缓存地址
// 可通过编辑key值表来创建多个缓存路径
getLocalFilePath: (p) => {
const remoteFiles = path.remoteFiles;
const len = remoteFiles.length;
for(let i = 0; i < len; i++) {
const key = remoteFiles[i];
if (p.indexOf(key) >= 0) {
p = p.replace(key, path.localFiles[i]);
let nIndex = p.indexOf("?");
if (nIndex > -1) {
p = p.substr(0, nIndex);
}
return path.normailze(p);
}
}
//未设置key值,将按照地址名整理出资源路径,进行存储
if (p.indexOf(":") >= 0 || p.indexOf('#') >= 0 || p.indexOf('?') >= 0) {
p = p.replace(/[^a-z0-9.]/gi, "/");
}
return path.normailze(p);
},
// 获取微信的用户缓存地址
getWxUserPath: (p) => {
return WX_ROOT + p;
},
// 本地资源文件key值表
// 可按照网络资源地址分配本地地址,可修改
// 以下为示例,开发者可根据需要进行修改
remoteFiles: [
`${js_gameVars.qufuCdnServer}qufu_resource1/`, //区服资源
`${js_gameVars.qufuCdnServer}img/`, //区服资源
`${js_gameVars.qufuCdnServer}${gameVersion}/resource/`, //前端资源
`${js_gameVars.qufuCdnServer}${gameVersion}/`
],
localFiles: [
'temp_qufu3/', //区服资源
'temp_img2/', //区服资源
'temp_local/', //前端资源
'temp_root/'
]
}
\ No newline at end of file
const path = require('path') const path = require('path')
const fs = require('fs') // 导入nodefs对象 const fs = require('fs') // 导入nodefs对象
const webpack = require('webpack')
const UglifyjsPlugin = require('uglifyjs-webpack-plugin')
const WebpackObfuscator = require('webpack-obfuscator'); const WebpackObfuscator = require('webpack-obfuscator');
const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const resolve = (p) => path.resolve(__dirname, "./", p) const resolve = (p) => path.resolve(__dirname, "./", p)
...@@ -35,14 +37,28 @@ function configInfi() { ...@@ -35,14 +37,28 @@ function configInfi() {
configInfi() configInfi()
module.exports={ module.exports={
mode:'development', mode:'development',
optimization:{ optimization: {
noEmitOnErrors: true, //编译时出现错误跳过 noEmitOnErrors: true,
}, minimizer: [
new UglifyjsPlugin({
// 使用缓存
cache: true
}),
]
},
entry, entry,
output, output,
plugins:[ plugins:[
new CleanWebpackPlugin(),
// new WebpackObfuscator (ObfuscatorType) new webpack.NoEmitOnErrorsPlugin(),
new CleanWebpackPlugin( {
root: resolve(""), // 设置根节点
verbose: true, //开启在控制台输出信息
dry: false,
}),
new WebpackObfuscator (ObfuscatorType),
// new webpack.IgnorePlugin(/\.\/locale/, /moment/)
] ]
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论