项目作者: yst138451

项目描述 :
Regex string-replacer for Webpack loader
高级语言: JavaScript
项目地址: git://github.com/yst138451/async-string-replacer.git
创建时间: 2019-03-21T16:43:34Z
项目社区:https://github.com/yst138451/async-string-replacer

开源协议:MIT License

下载


RegExp string-replacer for Webpack

Quoting the doc on Asynchronous Loaders:

Loaders were originally designed to work in synchronous loader pipelines, like Node.js (using enhanced-require), and asynchronous pipelines, like in Webpack. However, since expensive synchronous computations are a bad idea in a single-threaded environment like Node.js, we advise making your loader asynchronous if possible. Synchronous loaders are ok if the amount of computation is trivial.

Install

  1. $ npm i -D yst138451/async-string-replacer

Usage

In webpack.config.js add it up right before the last loader in the chain (e.g. babel-loader):

  1. module: {
  2. rules: [
  3. {
  4. test: /\.js$/,
  5. exclude: /(node_modules|bower_components)/,
  6. use: [
  7. 'babel-loader',
  8. {
  9. loader: 'async-string-replacer',
  10. options: {
  11. configFile: path.resolve(__dirname, './src/your.config.js')
  12. }
  13. }
  14. ]
  15. },
  16. //...
  17. ]
  18. }

Where your.config.js is your dedicated config file containing all the RegExp rules for the string replacements. For example the following pattern allows you to md5 hash all Vue.js Custom Event names (for whatever reason that is):

  1. module.exports = [
  2. {
  3. pattern: /(?<=\$(emit|on|once|off)\(')\S+(?=')/ig,
  4. replacement: require('md5')
  5. },
  6. // More rules here
  7. ]
Raw Hashed
vm.$emit('routes:changed'); vm.$emit('fe288a6cad4b10b92fdff65256df6713');

Once configured and run, all files ending in .js (including .vue files, since they ultimately compile down to .js) will be processed by the loader, seaching up for these custom event names to hash.

⚠ This is not meant for JS obfuscation of some sort, in which case you should instead use Javascript obfuscator or UglifyJS Webpack Plugin.

License

MIT