string-replace-loader Webpack字符串替换加载器

想快速在 Webpack 中执行字符串替换?string-replace-loader是个不错的选择。这款加载器支持普通文本和正则表达式的替换,安装也挺简单:只需要运行yarn add --dev string-replace-loader就行了。适用于 Node v4 及以上版本。注意,如果你还在用较旧版本的 Node,得用 1.3.0 版。到 Webpack v5+时,3.0.0 版是最佳搭档,支持更强的功能和更好的性能。用法也蛮直观,你可以在 Webpack 配置中定义替换规则,一切都在构建时自动搞定。举个例子,如果你要在文件中替换掉所有的foo字符串,就可以这样配置:

module: {
  rules: [
    {
      test: /\.js$/,
      use: [
        { loader: 'string-replace-loader', options: { search: 'foo', replace: 'bar' } }
      ]
    }
  ]
}
如果你有特定的字符串替换需求,这个工具的灵活性蛮高的。

不过也有些小提醒,尽量别用它去替换大量文本,否则会影响性能。,替换字符串、调整配置或者执行某些构建时的预,它是个蛮有用的小工具。

zip
string-replace-loader-master.zip 预估大小:16个文件
folder
string-replace-loader-master 文件夹
file
.gitignore 103B
file
package.json 794B
file
index.js 82B
file
LICENCE 1KB
folder
node_modules 文件夹
folder
__this-loader 文件夹
file
index.js 43B
file
README.md 4KB
folder
lib 文件夹
file
processChunk.js 380B
file
getOptionsArray.js 1KB
file
replace.js 823B
file
.npmignore 77B
folder
test 文件夹
file
index.test.js 9KB
folder
source 文件夹
file
foo.js 44B
file
bar.js 44B
file
entry.js 108B
file
.editorconfig 192B
file
yarn.lock 54KB
zip 文件大小:31.28KB