NodeJS Webpack CopyPlugin添加嵌套目录和文件

xurqigkl  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(127)

在Webpack CopyPlugin中添加所有嵌套目录和所述嵌套目录的文件的模式是什么?

directory
-- subdirectory-1
---- file-1.ext
-- subdirectory-2
---- file-2.ext

字符串

所需构建输出

lib
-- directory
---- subdirectory-1
------ file-1.ext
---- subdirectory-2
------ file-2.ext


我在webpack CopyPlugin config中尝试过这个:

new CopyPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, "src/styles/directory"),
          to: "./lib/styles/directory/[folder]/[name][ext]",
        },
      ],
    }),

sczxawaw

sczxawaw1#

to选项文档中,只需指定它的相对路径。
例如:
项目结构:

$ tree -L 5 -I node_modules
.
├── dist
│   ├── lib
│   │   └── dir
│   │       ├── sub-1
│   │       │   └── file-1.txt
│   │       └── sub-2
│   │           └── file-2.txt
│   └── main.js
├── package-lock.json
├── package.json
├── src
│   ├── index.js
│   └── styles
│       └── dir
│           ├── sub-1
│           │   └── file-1.txt
│           └── sub-2
│               └── file-2.txt
└── webpack.config.js

10 directories, 9 files

字符串
webpack.config.js

const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
    mode: 'production',
    entry: './src/index.js',
    output: {
        clean: true,
    },
    plugins: [
        new CopyPlugin({
            patterns: [{ from: path.resolve(__dirname, './src/styles'), to: 'lib' }],
        }),
    ],
};


构建日志:

> webpack

asset lib/dir/sub-1/file-1.txt 2 bytes [compared for emit] [from: src/styles/dir/sub-1/file-1.txt] [copied]
asset lib/dir/sub-2/file-2.txt 2 bytes [compared for emit] [from: src/styles/dir/sub-2/file-2.txt] [copied]
asset main.js 0 bytes [compared for emit] [minimized] (name: main)
./src/index.js 1 bytes [built] [code generated]
webpack 5.88.2 compiled successfully in 153 ms


package.json

{
    "version": "1.0.0",
    "scripts": {
        "build": "webpack"
    },
    "devDependencies": {
        "copy-webpack-plugin": "^11.0.0",
        "webpack": "^5.80.0",
        "webpack-cli": "^5.0.2"
    }
}

相关问题