modify source webpack plugin

2025-12-11 0 843

修改 – 源webpackplugin

用于修改模块源的WebPack插件

兼容性

webpack版本 插件版本 地位
^5.0.0 ^4.0.0

^4.37.0 ^4.0.0

版本3的迁移指南

安装

NPM

npm i -D modify-source-webpack-plugin

yarn add -D modify-source-webpack-plugin

进口

ES6/打字稿

 import { ModifySourcePlugin } from \'modify-source-webpack-plugin\' ;

CJS

 const { ModifySourcePlugin } = require ( \'modify-source-webpack-plugin\' ) ; 

用法

webpack.config.js

 module . exports = {
  plugins : [ new ModifySourcePlugin ( options ) ]
} ; 

选项

规则[]。测试

类型:Regexp | ((模块:webpack.normalmodule)=> boolean)

必需的

测试是REGEXP或功能,用于确定应修改哪些模块。

REGEXP将应用于完整的模块路径(基于UserRequest)。

函数将应用于正常模块。

Regexp的示例

plugins: [
  new ModifySourcePlugin ( {
    rules : [
      {
        test : / i n d e x \\. j s $ /
      }
    ]
  } )
] ; 

具有函数的示例

plugins: [
  new ModifySourcePlugin ( {
    rules : [
      {
        test : module =>
          module . source ( ) . source ( ) . includes ( \'my-secret-module-marker\' )
      }
    ]
  } )
] ;

规则[]。操作

类型:AbstractOperation [](支持的串联,更换)

必需的

描述如何修改模块的操作列表。

配x操作应进行语法兼容的更改。例如,所有不支持的语法都会破坏您的构建或在运行时创建错误。

Concat操作的示例

 import {
  ModifySourcePlugin ,
  ConcatOperation
} from \'modify-source-webpack-plugin\' ;

module . exports = {
  plugins : [
    new ModifySourcePlugin ( {
      rules : [
        {
          test : / m y - f i l e \\. j s $ / ,
          operations : [
            new ConcatOperation (
              \'start\' ,
              \'// Proprietary and confidential.\\n\\n\'
            ) ,
            new ConcatOperation (
              \'end\' ,
              \'\\n\\n// File is written by me, January 2022\'
            )
          ]
        }
      ]
    } )
  ]
} ; 

替换操作的示例

 import {
  ModifySourcePlugin ,
  ReplaceOperation
} from \'modify-source-webpack-plugin\' ;

module . exports = {
  plugins : [
    new ModifySourcePlugin ( {
      rules : [
        {
          test : / m y - f i l e \\. j s $ / ,
          operations : [
            new ReplaceOperation ( \'once\' , \'searchValue\' , \'replaceValue\' ) ,
            new ReplaceOperation ( \'all\' , \'searchValue\' , \'replaceValue\' )
          ]
        }
      ]
    } )
  ]
} ; 

坏例子

 module . exports = {
  plugins : [
    new ModifySourcePlugin ( {
      rules : [
        {
          test : / m y - f i l e \\. j s $ / ,
          operations : [
            new ConcatOperation ( \'start\' , \'Haha I break your build LOL\' )
          ]
        }
      ]
    } )
  ]
} ;

调试

类型:布尔值

为了更容易调试。在控制台中打印一些日志。

高级用法

编译时常数

与我们更改的有关文件的信息有关的常数。

持续的 描述
$ file_path 文件路径
$ file_name 文件名
plugins: [
  new ModifySourcePlugin ( {
    rules : [
      {
        test : / m y - f i l e \\. j s $ / ,
        operations : [
          new ConcatOperation (
            \'end\' ,
            \'\\n\\n // This file is on the path - $FILE_PATH and filename - $FILE_NAME\'
          )
        ]
      }
    ]
  } )
] ;

在文件内容之前和之后放置内容

my-file.js(可单击)
 console . log ( \'Hello world!\' ) ;

webpack.config.js

plugins: [
  new ModifySourcePlugin ( {
    rules : [
      {
        test : / m y - f i l e \\. j s $ / ,
        operations : [
          new ConcatOperation ( \'start\' , \'// Something before file contents.\\n\' ) ,
          new ConcatOperation ( \'end\' , \'\\n// Something after file contents.\' )
        ]
      }
    ]
  } )
] ;
结果my-file.js(可单击)
 // Something before file contents.
console . log ( \'Hello world!\' ) ;
// Something after file contents.

用内容替换插头

my-component.jsx(可单击)
 function HelloMessage ( props ) {
  return (
    < div >
      Hello, $NAME
      < button
        onClick = { ( ) => {
          props . userLogout ( ) ;
          alert ( \'Goodbye, $NAME!\' ) ;
        } }
      >
        $EXIT_LABEL
      </ button >
    </ div >
  ) ;
}

webpack.config.js

plugins: [
  new ModifySourcePlugin ( {
    rules : [
      {
        test : / m y - c o m p o n e n t \\. j s x $ / ,
        operations : [
          new ReplaceOperation ( \'all\' , \'$NAME\' , \'Artem Batura\' ) ,
          new ReplaceOperation ( \'once\' , \'$EXIT_LABEL\' , \'Exit\' )
          // new ReplaceOperation(\'once\', \'$EXIT_LABEL\', \'Leave\')
        ]
      }
    ]
  } )
] ;
结果my-component.jsx(可单击)
 function HelloMessage ( props ) {
  return (
    < div >
      Hello, Artem Batura
      < button
        onClick = { ( ) => {
          props . userLogout ( ) ;

          alert ( \'Goodbye, Artem Batura!\' ) ;
        } }
      >
        Exit
      </ button >
    </ div >
  ) ;
}

将代码/文本片段放在所需位置

my-component.jsx(可单击)
 function HelloMessage ( props ) {
  $MY_DEBUG_CODE ;

  return (
    < div >
      Hello, user! $MY_USER_COMPONENT
      < button onClick = { ( ) => props . userLogout ( ) } > Exit </ button >
    </ div >
  ) ;
}

webpack.config.js

plugins: [
  new ModifySourcePlugin ( {
    rules : [
      {
        test : / m y - c o m p o n e n t \\. j s $ / ,
        operations : [
          new ReplaceOperation (
            \'once\' ,
            \'$MY_DEBUG_CODE\' ,
            \'console.log(\"props\", props)\'
          ) ,
          new ReplaceOperation (
            \'once\' ,
            \'$MY_USER_COMPONENT\' ,
            \'<div>compilation-time markup</div>\'
          )
        ]
      }
    ]
  } )
] ;
结果my-component.jsx(可单击)
 function HelloMessage ( props ) {
  console . log ( \'props\' , props ) ;

  return (
    < div >
      Hello, user!
      < div > compilation-time markup </ div >
      < button onClick = { ( ) => props . userLogout ( ) } > Exit </ button >
    </ div >
  ) ;
}

下载源码

通过命令行克隆项目:

git clone https://github.com/artembatura/modify-source-webpack-plugin.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 建站资源 modify source webpack plugin https://www.zuozi.net/35102.html

Newtrodit
上一篇: Newtrodit
similar english words
下一篇: similar english words
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务