node实现前端静态资源的cdn自动化

/*

将静态资源css,js上传至cdn并替换

*/
const fs = require('fs');
const rp = require('request-promise');
const glob = require('glob');

//上传文件并获取url
async function uploadFile(filePath) {
  var options = {
    method: 'POST',
    url: 'http://xxxx.xxx.com/api/upload', //换成你自己的cdn
    formData: {
      contentType: 'text/css',
      needHttps: 'true',
      file: fs.createReadStream(filePath),
    },
  };
  //   console.log(rp(options))
  return await rp(options).then(res => JSON.parse(res).data);
}

//读取文件,并且替换文件中指定的字符串
function replaceFile(filePath, sourceRegx, targetStr) {
  fs.readFile(filePath, function(err, data) {
    if (err) {
      return err;
    }
    let str = data.toString();
    str = str.replace(sourceRegx, targetStr);
    fs.writeFile(filePath, str, function(err) {
      if (err) {
        console.log(err);
        return err;
      }
    });
  });
}
//遍历statics文件夹,找到main_*.js
function findFile(dir, filenameReg, cb) {
  fs.readdir(dir, function(err, files) {
    if (err) {
      return err;
    }
    if (files.length != 0) {
      files.forEach(item => {
        let path = dir + '/' + item;
        //判断文件的状态,用于区分文件名/文件夹
        fs.stat(path, function(err, status) {
          if (err) {
            return err;
          }
          let isFile = status.isFile(); //是文件
          let isDir = status.isDirectory(); //是文件夹
          if (isFile) {
            if (item.match(new RegExp(filenameReg))) {
              //  console.log('找到了', path);
              //   arr.push(path);
              cb(path);
            }
          }
          if (isDir) {
            findFile(path, filenameReg, cb);
          }
        });
      });
    }
  });
}

function test() {
  findFile('./build', '^main.*css$', function(css) {
    uploadFile(css).then(url => {
      console.log('----', url);
    });
  });
}

//  替换静态资源
function replaceMainCss(htmls) {
  findFile('./build', '^main.*css$', function(path) {
    console.log(path);
    uploadFile(path).then(url => {
      console.log(url);
      htmls.forEach(html => {
        replaceFile(html, /\/static\/css\/main.[a-z0-9]{8}.css/, url);
      });
    });
  });
}
function replaceMainJs(htmls) {
  findFile('./build', '^main.*js$', function(path) {
    console.log(path);
    uploadFile(path).then(url => {
      console.log(url);
      htmls.forEach(html => {
        replaceFile(html, /\/static\/js\/main.[a-z0-9]{8}.js/, url);
      });
    });
  });
}
function replaceBaseJs(htmls) {
  findFile('./build', '^base.*js$', function(path) {
    console.log(path);
    uploadFile(path).then(url => {
      console.log(url);
      htmls.forEach(html => {
        replaceFile(html, /\/static\/js\/base.[a-z0-9]{8}.js/, url);
      });
    });
  });
}
let htmls = [];
glob.sync('./build/**/index.html').forEach(html => {
  htmls.push(html);
});
// console.log(htmls)
replaceMainCss(htmls);
replaceMainJs(htmls);
replaceBaseJs(htmls);
// test()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值