bricks.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. type ManifestConfig = {
  4. initial: string[],
  5. game: string[]
  6. }
  7. export class BricksPlugin implements plugins.Command {
  8. constructor() {
  9. }
  10. async onFile(file: plugins.File) {
  11. const filename = file.basename;
  12. if (filename == 'manifest.json') {
  13. const contents = file.contents.toString();
  14. const jsonData: ManifestConfig = JSON.parse(contents);
  15. let content = '';
  16. content += `BK.Script.loadlib("GameRes://js/promise.js");\n`;
  17. for (let item of jsonData.initial) {
  18. if (item != 'js/promise.js' && item != 'js/promise.min.js') {
  19. content += `BK.Script.loadlib("GameRes://${item}");\n`
  20. }
  21. }
  22. for (let item of jsonData.game) {
  23. content += `BK.Script.loadlib("GameRes://${item}");\n`
  24. }
  25. content += `BK.Script.loadlib("GameRes://egret.bricks.js");\n`
  26. file.path = file.dirname + '/manifest.js'
  27. file.contents = new Buffer(content);
  28. } else if (filename == 'main.js') {
  29. const content = file.contents.toString();
  30. let result = content.replace(/RES\.loadConfig\("resource\/default\.res\.json", "resource\/"\)/gm, 'RES.loadConfig("GameRes://resource/default.res.json", "GameRes://resource/")');
  31. result = result.replace(/eui\.Theme\("resource\/default\.thm\.json", _this\.stage\)/gm, 'eui.Theme("GameRes://resource/default.thm.json", _this.stage)');
  32. result += ";global.Main = Main;";
  33. file.path = file.dirname + '/main.js'
  34. file.contents = new Buffer(result);
  35. } else if (filename == 'promise.js') {
  36. return null;
  37. }
  38. return file;
  39. }
  40. async onFinish(pluginContext: plugins.CommandContext) {
  41. //同步index.html 配置到main.js
  42. let mainJSPath = path.join(pluginContext.outputDir, 'main.js');
  43. let mainJSContent = fs.readFileSync(mainJSPath, { encoding: "utf8" });
  44. let projectConfig = pluginContext.buildConfig.projectConfig;
  45. mainJSContent = mainJSContent.replace(/frameRate: 30/gm, `frameRate: ${projectConfig.frameRate}`);
  46. mainJSContent = mainJSContent.replace(/contentWidth: 640/gm, `contentWidth: ${projectConfig.contentWidth}`);
  47. mainJSContent = mainJSContent.replace(/contentHeight: 1136/gm, `contentHeight: ${projectConfig.contentHeight}`);
  48. mainJSContent = mainJSContent.replace(/entryClassName: "Main"/gm, `entryClassName: ${projectConfig.entryClassName}`);
  49. mainJSContent = mainJSContent.replace(/scaleMode: "showAll"/gm, `scaleMode: ${projectConfig.scaleMode}`);
  50. mainJSContent = mainJSContent.replace(/orientation: "auto"/gm, `orientation: ${projectConfig.orientation}`);
  51. fs.writeFileSync(mainJSPath, mainJSContent);
  52. }
  53. }
  54. declare var egret;