错误日志:
<--- Last few GCs --->
58509 ms: Mark-sweep 676.1 (716.2) -> 675.8 (716.2) MB, 782.3 / 0 ms [allocation failure] [GC in old space requested]
.
59293 ms: Mark-sweep 675.8 (716.2) -> 675.8 (716.2) MB, 783.4 / 0 ms [allocation failure] [GC in old space requested]
.
60083 ms: Mark-sweep 675.8 (716.2) -> 675.8 (716.2) MB, 790.3 / 0 ms [last resort gc].
60883 ms: Mark-sweep 675.8 (716.2) -> 675.8 (716.2) MB, 799.7 / 0 ms [last resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
2: arguments adaptor frame: 2->3
Security context: 15F7857D <JS Object>
3: /* anonymous */(aka /* anonymous */) [D:\Program Files\Egret\EgretEngine\win\egret\tools\lib\uglify-js\parse.js:~
357] [pc=36FF87A5] (this=15F080C9 <undefined>)
4: arguments adaptor frame: 1->0
5: /* anonymous */(aka /* anonymous */) [D:\Program Files\Egret\EgretEngine\win\egret\tools\lib\uglify-js\parse.js:5
09] [pc=36FE0D80] (this=15F080C9 <undefine...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
解决方案:
windows用户:
将C:\Users\{你的用户名}\AppData\Roaming\npm\egret.cmd中的内容修改为:
@IF EXIST "%~dp0node_modules\egret\EgretEngine" (
FOR /F "usebackq delims=" %%a in ("%~dp0node_modules\egret\EgretEngine") do @(set EGRET=%%a)
) ELSE (
set EGRET=%~dp0node_modules
)
@IF EXIST "%EGRET%\node.exe" (
"%EGRET%\node.exe" --max-old-space-size=2000 "%EGRET%\selector.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node --max-old-space-size=2000 "%EGRET%\selector.js" %*
)
mac
/usr/local/lib/node_modules/egret
#!/bin/sh
basedir=`dirname "$0"`
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;
esac
if [ -f "$basedir/../lib/node_modules/egret/EgretEngine" ]; then
EGRET=`cat "$basedir/../lib/node_modules/egret/EgretEngine"`;
else
EGRET=$basedir/../lib/node_modules;
fi
EGRET_CLT=$EGRET/selector.js;
if [ -x "$EGRET/node" ]; then
"$EGRET/node" --max-old-space-size=2000 "$EGRET_CLT" "$@"
ret=$?
else
node --max-old-space-size=2000 "$EGRET_CLT" "$@"
ret=$?
fi
exit $ret
其中2000表示最大内存为2000mb,就是2G内存,可根据实际情况修改
附完整分包脚本
import * as fs from 'fs';
import * as path from 'path';
export class mainspliteplugin implements plugins.Command {
private codeString: string;
private parames = ["xx","xxx"];
constructor() {
}
async onFile(file: plugins.File) {
let filename = file.origin;
if (file.basename == "main.js") {
this.codeString = file.contents.toString();;
}
return file;
}
async onFinish(commandContext: plugins.CommandContext) {
if (this.codeString) {
let headString = this.codeString.slice(0, this.findNext());
this.parames.forEach((param,idx,arr)=>{
console.log("splite start====>"+param)
let startIndex = 0;
let code = headString;
while(true){
let sub = this.subStr(param);
if(sub == "") {
code += ";window." + param + " = " + param + ";"
commandContext.createFile("main"+param+".js", new Buffer(code));
break;
}
code += sub;
}
},this);
commandContext.createFile("main.js", new Buffer(this.codeString));
console.log("split main finish");
}
}
private findNext(start?: number): number {
start = start || 0;
let headIndex = this.codeString.indexOf("var xxxxx;",start);
this.parames.forEach((param,idx,arr)=>{
let tempIndex = this.codeString.indexOf("var " + param + ";", start);
if(tempIndex >= 0) {
headIndex = Math.min(tempIndex, headIndex);
}
},this);
return headIndex;
}
private subStr(str:string): string {
let idx = this.codeString.indexOf("var " + str + ";");
if(idx >= 0) {
let next = this.findNext(idx+1);
let sub = this.codeString.substring(idx, next);
this.codeString = this.codeString.replace(sub, "");
return sub;
}
return "";
}
}