简体   繁体   English

Webpack output 将同一个目录下的所有文件放到不同的文件夹中

[英]Webpack output all the files within a directory with the same name into a different folder

I have the following directory:我有以下目录:

  - src/
    - js/
      - profile1/
        - script1.js
        - script2.js
      - profile2/
        - script3.js
        - script4.js
      - script5.js
  - dist/
    - js/
  package.js
  webpack.config.js

I am trying to webpack all JS files from /src/js into dist/js folder conserving the same naming and folder structure.我正在尝试将/src/js中的所有 JS 文件 webpack 放入dist/js文件夹中,并保留相同的命名和文件夹结构。

I could write a different config for each file and webpack each JS file, but I am wondering if there is a set of config that I can write to do all the operations in one shot.我可以为每个文件和 webpack 每个 JS 文件编写一个不同的配置,但我想知道是否有一组配置可以让我一次性完成所有操作。

I have tried:我努力了:

const path = require('path');

module.exports = {
  entry: './src/js/*.js',
  output: {
    filename: '*.js',
    path: path.resolve(__dirname, 'dist'),
  }
}

failed miserably.惨败。

You can match the entry file first like using glob (install it with npm i glob ).您可以像使用glob一样首先匹配入口文件(使用npm i glob安装它)。 Then you can change the output dynamically by appending [name] to output.filename .然后,您可以通过将[name]附加到 output.filename 来动态更改output.filename Here's the complete code:这是完整的代码:

const path = require('path');
const glob = require('glob');

module.exports = {
  entry: Object.fromEntries(glob.sync(path.resolve(__dirname, 'src/js/**/*.js')).map((v) => [
    v.split('src/js/')[1], v,
  ])),
  output: {
    filename: '[name]',
    path: path.resolve(__dirname, 'dist/js'),
  },
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用webpack保留输出目录中的文件夹结构 - Preserving folder structure in output directory with webpack 使用webpack将html文件中的图像复制到输出目录 - Copy images in html files to output directory with webpack Webpack:output.path,publicPath和chunkFilename为html和js文件不在同一文件夹中的项目选择概念 - Webpack: output.path, publicPath and chunkFilename selecting concept for projects where html and js files are not in the same folder 从文件夹webpack重新导出所有文件 - reexport all files from a folder webpack 使用 webpack 将所有 js 文件构建到目标文件夹 - Build all js files to target folder with webpack Webpack解决src文件夹中的所有文件 - Webpack Resolve all Files in the src folder Webpack:使用目录中文件的所有名称获取地图 - Webpack: Get map with all names of files in a directory Webpack 2-具有相同内容的多个输出文件 - Webpack 2 - multiple output files with the same content 尝试将文件夹中的所有谷歌表格文件导入电子表格,所有列标题都相同 - Trying to import all google sheets files within a folder to a spreadsheet, all column headers are the same 检查JQUERY的同一个文件夹中是否存在1个或多个同名但扩展名不同的文件 - Check if 1 or more files with the same name, but with different extensions, exist in a same folder in JQUERY
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM