cocos-html-pack-3d
Version:
Single html web mobile template for Cocos verions 2.x
103 lines (102 loc) • 4.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Packer = void 0;
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
var pako_1 = __importDefault(require("pako"));
// HTML placeholder
var PlaceHolder;
(function (PlaceHolder) {
PlaceHolder["Title"] = "${title}";
PlaceHolder["Style"] = "${style}";
PlaceHolder["Orientation"] = "${orientation}";
PlaceHolder["Compressed"] = "${compressed}";
PlaceHolder["Assets"] = "${assets}";
PlaceHolder["Settings"] = "${settings.js}";
PlaceHolder["EngineJS"] = "${cocos2d-js-min.js}";
PlaceHolder["PhysicJS"] = "${physics-min.js}";
PlaceHolder["InternaJS"] = "${assets/internal/index.js}";
// ResouceJS = '${resources/index.js}', // the same internal js
PlaceHolder["MainJS"] = "${main.js}";
PlaceHolder["ProjectJS"] = "${assets/main/index.js}";
})(PlaceHolder || (PlaceHolder = {}));
var Packer = /** @class */ (function () {
function Packer(value) {
this._data = value;
}
Packer.prototype.patch = function (title, vendors, adSize, orientation, useCompress) {
if (orientation === void 0) { orientation = 'portrait'; }
var html = this._data.html;
html = html.replace(PlaceHolder.Title, title);
var regex = new RegExp("\\" + PlaceHolder.Orientation, 'g');
html = html.replace(regex, orientation); // replaceAll not supports
//Replace adnetwork
html = html.replace('__adNetwork__', vendors);
console.log(vendors);
//Replace adsize for google
if (adSize) {
var _adSize = JSON.parse(adSize);
console.log(_adSize.width + ' - ' + _adSize.height);
html = html.replace('<!--__MetaAdSize__-->', '<meta name="ad.size" content="width=' + _adSize.width + ',height=' + _adSize.height + '">');
}
else {
html = html.replace('<!--__MetaAdSize__-->', '');
}
var _a = this._data, style = _a.style, settings = _a.settings, internalJS = _a.internalJS, mainJS = _a.mainJS;
var _b = this._data, assets = _b.assets, engineJS = _b.engineJS, js = _b.js, physicJS = _b.physicJS;
// Style
var res = html.replace(PlaceHolder.Style, this._getStyleTag(style));
//Replace splash screen bằng base 64
// if (res.indexOf('./splash.png') !== -1)
// res = res.replace('./splash.png', "\"" + splash + "\"");
// Compressed
var compressed = '';
if (useCompress) {
// Compress assets, engineJS and js
var uncompressed = JSON.stringify({ assets: assets, engineJS: engineJS, js: js, physicJS: physicJS });
// Remove uncompress data to remove placeholder
assets = {};
engineJS = '';
js = '';
physicJS = '';
// Compress to base64
compressed = "window.compressed=`" + Buffer.from(pako_1.default.deflate(uncompressed)).toString('base64') + "`;\n";
// console.log(res);
// Pako source
var inflateJS = fs_1.default.readFileSync(path_1.default.join(__dirname, '../node_modules/pako/dist/pako_inflate.js'));
compressed += inflateJS;
}
res = res.replace(PlaceHolder.Compressed, this._getJSTag(compressed));
// Assets
res = res.replace(PlaceHolder.Assets, this._getJSTag("window.assets=" + JSON.stringify(assets) + ";\n"));
// Settings
res = res.replace(PlaceHolder.Settings, this._getJSTag(settings));
// Engine
res = res.replace(PlaceHolder.EngineJS, this._getJSTag(engineJS));
//Physic
res = res.replace(PlaceHolder.PhysicJS, this._getJSTag(physicJS));
// Internal
res = res.replace(PlaceHolder.InternaJS, this._getJSTag(internalJS));
// Main
res = res.replace(PlaceHolder.MainJS, this._getJSTag(mainJS));
// JS
res = res.replace(PlaceHolder.ProjectJS, this._getJSTag(js));
return res;
};
Packer.prototype._getHTMLTag = function (tag, value) {
if (!value || value.length === 0)
return '';
return "<" + tag + ">\n" + value + "</" + tag + ">";
};
Packer.prototype._getJSTag = function (value) {
return this._getHTMLTag('script', value);
};
Packer.prototype._getStyleTag = function (value) {
return this._getHTMLTag('style', value);
};
return Packer;
}());
exports.Packer = Packer;