2022年vue项目使用go.js 2.1去水印

背景

网上找了一大堆去除 gojs 水印的方法,要么针对新版已经失效(搜关键字7eba17a4ca3b1a8346,搜不到),要么是修改 node_modules 中的文件(CI\CD构建发版到生产环境无效)。
无意中看到一个老哥通过写 webpack loader 的方案解决,觉得很棒,记录下我的代码。

项目环境

vue 2.6 + vue cli
go.js版本:2.1.55

解决方案

根目录新建一个 loader 目录,在loader目录下创建 gojs-hack-loader.js 文件,代码如下:

const pattern = /String\.fromCharCode\(a\.charCodeAt\(g\)\^b\[\(b\[c\]\+b\[d\]\)%256\]\);/gm;

const hack = `String.fromCharCode(a.charCodeAt(g)^b[(b[c]+b[d])%256]);if(f.indexOf('GoJS 2.1 evaluation')>-1|| f.indexOf('(c) 1998-2021 Northwoods Software')>-1|| f.indexOf('Not for distribution or production use')>-1|| f.indexOf('gojs.net')>-1){return '';}else{return f};`;

module.exports = function(source) {
    
    
  const index = source.search(pattern);
  if (index < 0) {
    
    
    throw new Error('gojs hack failed');
  }

  return source.replace(pattern, hack);
};

vue.config.js 中的chainWebpack配置中添加自己写的loader

chainWebpack: config => {
    
    
	...
    // go.js 去水印
    config.module
      .rule('gojs-hack')
      .test(/go.js/)
      .use('./loader/gojs-hack-loader')
      .loader('./loader/gojs-hack-loader')
      .end();
 }

参考文章
https://blog.csdn.net/weixin_41611635/article/details/120017814
https://www.cnblogs.com/languanghao/p/12868170.html

猜你喜欢

转载自blog.csdn.net/qq_39633494/article/details/122301567