filemanager-webpack-plugin 对构建前后文件进行操作

该文章介绍了如何使用filemanager-webpack-plugin这个Webpack插件来在构建过程中执行文件和目录的操作,如安装、配置以及在构建后复制、移动、删除、创建目录和归档文件。通过示例代码展示了如何处理多个文件操作任务,包括按顺序执行和处理压缩文件。
摘要由CSDN通过智能技术生成

目录

1. 安装

2. 使用

3. 合并文件或目录 


这个 Webpack 插件允许您在构建前或构建后复制、归档(.zip/.tar/.tar.gz)、移动、删除文件和目录。

1. 安装

npm install filemanager-webpack-plugin --save-dev

2. 使用

webpack.config.js  基本使用

const FileManagerPlugin = require('filemanager-webpack-plugin');

module.exports = {
  // ...
  // ...
  plugins: [
    new FileManagerPlugin({
      events: {    // 不加会报错!!!
        onEnd: {
        copy: [
          { source: '/path/from', destination: '/path/to' }
        ],
        move: [
          { source: '/path/from', destination: '/path/to' }
        ],
        delete: [
         '/path/to/file.txt'
        ],
        mkdir: [
         '/another/directory/'
        ],
        archive: [
          { source: '/path/from', destination: '/path/to.zip' },
          { 
             source: '/path/fromfile.txt', 
             destination: '/path/to.tar.gz', 
             format: 'tar',
             options: {
               gzip: true,
               gzipOptions: {
                level: 1
               },
               globOptions: {
                nomount: true
               }
             }
           }

        ]
      }
     }
    })
  ],
  // ...
}

想要按顺序运行操作,可以将onStartonEnd事件设置为数组。

3. 合并文件或目录 

 以下是 打包后的文件中,将两个文件合并为一个文件后压缩

const FileManagerPlugin = require('filemanager-webpack-plugin');

module.exports = {
  // ...
  // ...
  plugins: [
    new FileManagerPlugin({
      events: {
       onEnd: [  // 将onEnd设置为数组
         {
           copy: [
             { source: "./dist/assets", destination: "./newDist" }
           ]
         },
         {
           copy: [
             { source: "./dist/lease-item", destination: "./newDist" }
           ]
         },
         {
           delete: [
             "./dist"
           ]
         },
         {
           archive: [
              {
                source: "./newDist",
                destination: "./newDist" + moment().format("MMDDHHmm") + ".zip"
              }
            ]
         }
       ]
     }
    })
  ],
  ...
}

在以上例子中,两个copy是在两个对象中的,如果在一个对象中:

         {
           copy: [
             { source: "./dist/assets", destination: "./newDist" },
             { source: "./dist/lease-item", destination: "./newDist" }
           ]
         }
         

会报错,说明目标目录已经存在!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值