assets-compress.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const fs = require('fs');
  2. const path = require('fire-path');
  3. const utils = require('./libs/utils');
  4. const setting = require('./build-setting');
  5. var child_process = require('child_process');
  6. module.exports = (function (){
  7. var _instance;
  8. function AssetsCompress (params) {
  9. return {
  10. _ready: false,
  11. _quality: null,
  12. _extList: [],
  13. init (actualPlatform) {
  14. this._ready = false;
  15. let assetsCompress = `assetsCompress`;
  16. let compress = setting.getInstance().getItem(assetsCompress);
  17. if (!compress) { Editor.warn('缺少配置,跳过资源压缩!'); return false; }
  18. if (!compress.enable) { Editor.warn('未开启资源压缩!'); return false; }
  19. // 默认全平台支持
  20. let platform = compress.platform; // 平台配置
  21. if (platform) {
  22. let supportList = platform.split(',');
  23. if (!supportList.includes(actualPlatform)) {
  24. Editor.warn(actualPlatform, '平台不支持资源压缩');
  25. return false;
  26. }
  27. }
  28. this._quality = compress.quality || '65-80';
  29. let exts = compress.exts || 'png';
  30. this._extList = exts.split(',');
  31. this._ignoreFileUuids = [];
  32. let ignores = compress.ignores;
  33. if (ignores) {
  34. for (const aPath of ignores) {
  35. let fullpath = path.join(Editor.Project.path, aPath);
  36. let stat = fs.statSync(fullpath);
  37. if (stat.isDirectory()) {
  38. utils.findFileSync(fullpath, (filePath)=> {
  39. this.addIgnoreFile(filePath);
  40. });
  41. } else if (stat.isFile()) {
  42. this.addIgnoreFile(fullpath);
  43. }
  44. }
  45. }
  46. this._ready = true;
  47. return true;
  48. },
  49. isReady () {
  50. return this._ready;
  51. },
  52. getAssetsdbPath (root) {
  53. let assetsdb = 'db://';
  54. let assetsIdx = root.indexOf('assets');
  55. if (assetsIdx !== -1) {
  56. assetsdb += root.substr(assetsIdx, root.length);
  57. } else {
  58. assetsdb += '/assets';
  59. }
  60. return assetsdb;
  61. },
  62. addIgnoreFile (filePath) {
  63. let index = filePath.lastIndexOf('.');
  64. let ext = filePath.substr(index + 1);
  65. if (ext !== 'meta') {
  66. let uuid = Editor.assetdb._path2uuid[filePath];
  67. this._ignoreFileUuids.push(uuid);
  68. }
  69. },
  70. addCompressFile (filePath) {
  71. for (const uuid of this._ignoreFileUuids) {
  72. let include = filePath.includes(uuid);
  73. if (include) {
  74. return;
  75. }
  76. }
  77. let index = filePath.lastIndexOf('.');
  78. let ext = filePath.substr(index + 1);
  79. if (this._extList.includes(ext)) {
  80. this._files.push(filePath);
  81. }
  82. },
  83. genFiles (buildDest) {
  84. this._files = [];
  85. let rootPath = path.join(buildDest, 'res/raw-assets');
  86. let stat = fs.statSync(rootPath);
  87. if (stat.isDirectory()) {
  88. utils.findFileSync(rootPath, (filePath)=> {
  89. this.addCompressFile(filePath);
  90. });
  91. // let dirs = fs.readdirSync(rootPath);
  92. // dirs.forEach((ele, index) => {
  93. // let fullpath = path.join(rootPath, ele);
  94. // let info = fs.statSync(fullpath);
  95. // if (info.isDirectory()) {
  96. // utils.findFileSync(fullpath, (filePath)=> {
  97. // this.addCompressFile(filePath);
  98. // });
  99. // } else {
  100. // this.addCompressFile(fullpath);
  101. // }
  102. // });
  103. }
  104. },
  105. compress (callback) {
  106. let pngquant = Editor.url('packages://build-helper/tools/pngquant/pngquant');
  107. this._files.forEach((filePath, index) => {
  108. let cmd = pngquant + ' --quality=' + this._quality + ' --force 256 --skip-if-larger ' + filePath + ' -o ' + filePath;
  109. child_process.exec(cmd, {timeout: 3654321}, function (error, stdout, stderr) {
  110. if (stderr) {
  111. Editor.error('压缩' + filePath + '失败');
  112. return;
  113. }
  114. callback (filePath);
  115. });
  116. });
  117. },
  118. getFilesCount () {
  119. return this._files.length;
  120. }
  121. };
  122. }
  123. return {
  124. getInstance (params) {
  125. if (!_instance){
  126. _instance = AssetsCompress(params);
  127. }
  128. return _instance;
  129. }
  130. };
  131. })();