JsEncodePlugin.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const fs = require('fs');//node的文件系统模块,用于读写及操作文件
  2. const path = require('path');//node提供的一些用于处理文件路径的小工具
  3. var chalk = require('chalk')//用于向控制台输出带颜色的问题提示
  4. // 2、模块对外暴露的 js 函数
  5. function JsEncodePlugin(pluginOptions) {
  6. this.options = pluginOptions
  7. }
  8. // 3、原型定义一个 apply 函数,并注入了 compiler 对象
  9. JsEncodePlugin.prototype.apply = function (compiler) {
  10. const _this = this;
  11. // 4、挂载 webpack 事件钩子(这里挂载的是 after-emit 事件,在将内存中 assets 内容写到磁盘文件夹之后触发的webpack生命周期钩子)
  12. compiler.plugin('after-emit', function (compilation, callback) {
  13. // ... 内部进行自定义的编译操作
  14. // 5、操作 compilation 对象的内部数据
  15. console.log(chalk.cyan('\n jsencode start.\n'))
  16. var filePath = path.resolve(__dirname, _this.options.assetsPath);//设置需要加密的js文件路径,_this.options.assetsPath为插件配置中传过来的需要加密的js文件路径
  17. filterFile(filePath);
  18. function filterFile(fp) {
  19. fs.readdir(fp, (err, files) => {//读取该文件路径
  20. if (err) {
  21. console.log(chalk.yellow(
  22. '读取js文件夹异常:\n' +
  23. err.message + '\n'
  24. ))
  25. return;
  26. }
  27. files.forEach((filename) => {//遍历该路径下所有文件
  28. if (_this.options.jsReg.test(filename)) {//利用正则匹配我们要加密的文件,_this.options.jsReg为插件中传过来的需要加密的js文件正则,用以筛选出我们需要加密的js文件。
  29. var filedir = path.resolve(fp, filename);
  30. fs.readFile(filedir, 'utf-8', (err, data) => {//读取文件源码
  31. if (err) {
  32. console.log(chalk.yellow(
  33. '读取js文件异常:\n' +
  34. err.message + '\n'
  35. ))
  36. return;
  37. }
  38. //调用jjencode函数对源码进行jjencode加密,_this.options.global为插件配置中传过来的加密使用的全局变量名,将在jjencode函数中作为第一个参数传入
  39. let result = jjencode(_this.options.global, data);
  40. fs.writeFile(filedir, result, (err) => {//将加密后的代码写回文件中
  41. if (err) {
  42. console.log(chalk.yellow(
  43. '写入加密后的js文件异常:\n' +
  44. err.message + '\n'
  45. ))
  46. return;
  47. }
  48. console.log(chalk.cyan(' jsencode complete.\n'))
  49. })
  50. })
  51. }
  52. })
  53. })
  54. }
  55. //js加密函数
  56. function jjencode(gv, text) {
  57. var r = "";
  58. var n;
  59. var t;
  60. var b = ["___", "__$", "_$_", "_$$", "$__", "$_$", "$$_", "$$$", "$___", "$__$", "$_$_", "$_$$", "$$__", "$$_$", "$$$_", "$$$$",];
  61. var s = "";
  62. for (var i = 0; i < text.length; i++) {
  63. n = text.charCodeAt(i);
  64. if (n == 0x22 || n == 0x5c) {
  65. s += "\\\\\\" + text.charAt(i).toString(16);
  66. } else if ((0x20 <= n && n <= 0x2f) || (0x3A <= n == 0x40) || (0x5b <= n && n <= 0x60) || (0x7b <= n && n <= 0x7f)) {
  67. s += text.charAt(i);
  68. } else if ((0x30 <= n && n <= 0x39) || (0x61 <= n && n <= 0x66)) {
  69. if (s) r += "\"" + s + "\"+";
  70. r += gv + "." + b[n < 0x40 ? n - 0x30 : n - 0x57] + "+";
  71. s = "";
  72. } else if (n == 0x6c) { // 'l'
  73. if (s) r += "\"" + s + "\"+";
  74. r += "(![]+\"\")[" + gv + "._$_]+";
  75. s = "";
  76. } else if (n == 0x6f) { // 'o'
  77. if (s) r += "\"" + s + "\"+";
  78. r += gv + "._$+";
  79. s = "";
  80. } else if (n == 0x74) { // 'u'
  81. if (s) r += "\"" + s + "\"+";
  82. r += gv + ".__+";
  83. s = "";
  84. } else if (n == 0x75) { // 'u'
  85. if (s) r += "\"" + s + "\"+";
  86. r += gv + "._+";
  87. s = "";
  88. } else if (n < 128) {
  89. if (s) r += "\"" + s;
  90. else r += "\"";
  91. r += "\\\\\"+" + n.toString(8).replace(/[0-7]/g, function (c) { return gv + "." + b[c] + "+" });
  92. s = "";
  93. } else {
  94. if (s) r += "\"" + s;
  95. else r += "\"";
  96. r += "\\\\\"+" + gv + "._+" + n.toString(16).replace(/[0-9a-f]/gi, function (c) { return gv + "." + b[parseInt(c, 16)] + "+" });
  97. s = "";
  98. }
  99. }
  100. if (s) r += "\"" + s + "\"+";
  101. r =
  102. gv + "=~[];" +
  103. gv + "={___:++" + gv + ",$$$$:(![]+\"\")[" + gv + "],__$:++" + gv + ",$_$_:(![]+\"\")[" + gv + "],_$_:++" +
  104. gv + ",$_$$:({}+\"\")[" + gv + "],$$_$:(" + gv + "[" + gv + "]+\"\")[" + gv + "],_$$:++" + gv + ",$$$_:(!\"\"+\"\")[" +
  105. gv + "],$__:++" + gv + ",$_$:++" + gv + ",$$__:({}+\"\")[" + gv + "],$$_:++" + gv + ",$$$:++" + gv + ",$___:++" + gv + ",$__$:++" + gv + "};" +
  106. gv + ".$_=" +
  107. "(" + gv + ".$_=" + gv + "+\"\")[" + gv + ".$_$]+" +
  108. "(" + gv + "._$=" + gv + ".$_[" + gv + ".__$])+" +
  109. "(" + gv + ".$$=(" + gv + ".$+\"\")[" + gv + ".__$])+" +
  110. "((!" + gv + ")+\"\")[" + gv + "._$$]+" +
  111. "(" + gv + ".__=" + gv + ".$_[" + gv + ".$$_])+" +
  112. "(" + gv + ".$=(!\"\"+\"\")[" + gv + ".__$])+" +
  113. "(" + gv + "._=(!\"\"+\"\")[" + gv + "._$_])+" +
  114. gv + ".$_[" + gv + ".$_$]+" +
  115. gv + ".__+" +
  116. gv + "._$+" +
  117. gv + ".$;" +
  118. gv + ".$$=" +
  119. gv + ".$+" +
  120. "(!\"\"+\"\")[" + gv + "._$$]+" +
  121. gv + ".__+" +
  122. gv + "._+" +
  123. gv + ".$+" +
  124. gv + ".$$;" +
  125. gv + ".$=(" + gv + ".___)[" + gv + ".$_][" + gv + ".$_];" +
  126. gv + ".$(" + gv + ".$(" + gv + ".$$+\"\\\"\"+" + r + "\"\\\"\")())();";
  127. //console.log(r);
  128. return r;
  129. }
  130. // 6、执行 callback 回调
  131. callback();
  132. });
  133. };
  134. module.exports = JsEncodePlugin;