# copy-files-plugin **Repository Path**: webpack-plugins/copy-files-plugin ## Basic Information - **Project Name**: copy-files-plugin - **Description**: 模仿copy-webpack-plugin的插件,但是也可以单独引用,处理文件复制等 - **Primary Language**: JavaScript - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2020-07-01 - **Last Updated**: 2021-12-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # copy-files-plugin ### 介绍 * 模仿copy-webpack-plugin的插件,但是也可以单独引用,处理文件复制等 * 复制整个文件夹时将递归复制其子文件和文件夹 ### 安装教程 1. cnpm install -D copy-files-plugin 2. yarn add --dev copy-files-plugin 3. npm install --save-dev copy-files-plugin ### API * new CopyFilesPlugin(options) 初始化实例,返回实例对象 * options.outputPath: {string} 默认输出文件夹名称(非路径地址),当patterns内为单一路径时输出到此目录,默认输出为dist目录,相对于项目根目录,与package.json文件同级目录。 * options.patterns array:[{from: string, to: string, forceClear: boolean}] 匹配文件,说明输入输出关系 * from: 源文件地址 * to: 目标文件地址 * forceClear: 是否清除重置目标文件夹,默认为false * new CopyFilesPlugin(options).run() 不用在webpack环境下当做插件使用,可以直接使用的方法。run()执行后处理options配置中的文件关系。 * 初始版本,待补充开发... ### 使用说明 #### 1. 非webpack插件使用 * 将test文件中的文件递归复制到dist文件夹下 ```javascript const ROOT_PATH = process.cwd(); const commander = new CopyFilesPlugin({ patterns: [ { from: path.resolve(ROOT_PATH, './test'), to: path.resolve(ROOT_PATH, './dist'), }, ] }) commander.run(); ``` #### 2. webpack插件使用 * 将test文件中的文件递归复制到dist文件夹下 ```javascript const ROOT_PATH = process.cwd(); const webpackConfig = { plugins: [ new CopyFilesPlugin({ patterns: [ { from: path.resolve(ROOT_PATH, './test'), to: path.resolve(ROOT_PATH, './dist'), }, ] }) ] } ``` * 排除文件或文件夹 exclude: 文件|文件夹[相对dist2的文件路径path] ```javascript const ROOT_PATH = process.cwd(); const webpackConfig = { plugins: [ // 将指定文件夹下的文件复制到发布指定目录下 new CopyFilesPlugin({ patterns: [ { from: path.resolve(ROOT_PATH, 'public'), to: path.resolve(ROOT_PATH, 'dist2'), exclude: ['./ENV.js', './index.html'] }, ] }) ]} ``` * 默认复制输出 patterns数组中直接传入文件地址,默认输出到dist文件夹中。 ```javascript const ROOT_PATH = process.cwd(); const webpackConfig = { plugins: [ // 将指定文件夹下的文件复制到发布指定目录下 new CopyFilesPlugin({ patterns: [ path.resolve(ROOT_PATH, 'public/ENV.js') ] }) ]} ```