简体   繁体   English

如何使用Webpack将所有子目录中的JSON文件合并为一个?

[英]How to use Webpack to combine JSON files from all subdirectories into one?

Say I have a directory structured like this: 假设我有一个像这样结构化的目录:

1. folder A
  1a. some_file.js
  1b. data.json
2. folder B
  2a. folder B1
     2a1. other_file.json
     2a2. data.json
  2b. folder B2
     2b1. data.json
3. output.json

Is there a webpack loader that can combine data.json in all subfolders, and output it to output.json ? 是否有一个webpack加载器可以在所有子文件夹中组合data.json ,并将其输出到output.json

I've found https://www.npmjs.com/package/json-files-merge-loader that seems to do something similar, but it seems to ask for each path to data.json , while I need something that goes through all folders and subfolders for data.json . 我发现https://www.npmjs.com/package/json-files-merge-loader似乎做了类似的事情,但似乎要求data.json每条路径,而我需要通过的东西data.json所有文件夹和子文件夹。 All data.json keys are unique, and I want output.json to be one JSON object containing all key/value pair from all data.json . 所有data.json键都是唯一的,我希望output.json是一个包含所有data.json中所有键/值对的JSON对象。

webpack is not really suited for the use case you have. webpack并不适合您的用例。 Many people think that webpack can be a replacement for a build system, but it's just a module bundler, it doesn't handle every task. 许多人认为webpack可以替代构建系统,但它只是一个模块捆绑器,它不能处理每个任务。 Specifically: 特别:

  1. webpack traverses require() and import statements, meaning it needs the modules to be statically defined. webpack遍历require()import语句,这意味着它需要静态定义模块。 It's possible to get around this by writing a plugin or using a file generated from a template, but even if you did that... 可以通过编写插件或使用从模板生成的文件来解决这个问题,但即使你这样做了......
  2. webpack would have a hard time combining the JSON files in the way you want. webpack很难以你想要的方式组合JSON文件。 webpack is good at bundling files into some sort of modules system (CommonJS or AMD). webpack擅长将文件捆绑到某种模块系统(CommonJS或AMD)。 What you want is not a bundle containing modules, but a file containing arbitrary contents. 你想要的不是包含模块的包,而是包含任意内容的文件。

webpack might be able to do these things using some fancy plugins, but it's likely not worth it. webpack可能能够使用一些花哨的插件来做这些事情,但它可能不值得。 In your case, you probably just want to write a Node script. 在您的情况下,您可能只想编写一个Node脚本。 If you want, you can use a plugin like this to run the code before build. 如果需要,可以使用这样的插件在构建之前运行代码。

const fs = require('fs');
// https://github.com/isaacs/node-glob
const glob = require('glob');

const output = {};

glob('src/**/*.json', (error, files) => {
    files.forEach((filename) => {
        const contents = JSON.parse(fs.readFileSync(filename, 'utf8'));
        Object.assign(output, contents);
    });

    fs.writeFileSync('output.json', JSON.stringify(output));
});

Is there a webpack loader that can combine data.json in all subfolders, and output it to output.json? 是否有一个webpack加载器可以在所有子文件夹中组合data.json,并将其输出到output.json?

Use the merge-webpack-plugin . 使用merge-webpack-plugin

This is differ from to run the code before build. 这与在构建之前运行代码不同。 Because deep webpack integration allow to follow file changes and update results immidiately while hot reloading: 因为深度webpack集成允许在热重新加载时跟踪文件更改并立即更新结果:

MergePlugin = require("merge-webpack-plugin");
module.exports = {
  module: {
    rules: [
      {
        test: /\.(json)$/i,
        use: [
          MergePlugin.loader()
        ]
      }
    ]
  },
  plugins: [
    new MergePlugin({
      search: './src/**/*.json',
    })
  ]
}
  • if you need only one target file output.json for all folders in your project describe them all in search param, and that is all 如果您只需要一个目标文件output.json ,那么项目中的所有文件夹都会在search参数中描述它们,这就是全部
  • if you need to join separated files for each top level folders (output_A.json, output_B.json ...) then: 如果你需要为每个顶级文件夹(output_A.json,output_B.json ...)加入分隔文件,那么:
    • if your do not need to lookup through subfolders try to play with group param with value [path] - read more about grouping 如果您不需要通过子文件夹查找尝试使用值[path]组参数进行播放 - 请阅读有关分组的更多信息
    • if you need to join all files through each folder and its subfolders you need to create multiple plugin instances for each top level folder; 如果您需要通过每个文件夹及其子文件夹加入所有文件,则需要为每个顶级文件夹创建多个插件实例; but you can group files in each plugin instance by name or by ext (see grouping) 但您可以按名称或分组对每个插件实例中的文件进行分组(请参阅分组)

and some more 还有一些

@dee Please check this plugin https://www.npmjs.com/package/merge-jsons-webpack-plugin @dee请检查此插件https://www.npmjs.com/package/merge-jsons-webpack-plugin

You can pass array of files/patterns as an input and it will emit single file as json. 您可以将文件/模式数组作为输入传递,它将以json的形式发出单个文件。

My multi-json-loader might be helpful here. 我的multi-json-loader在这里可能会有所帮助。 It accepts a glob and combines the files into a single JSON blob while also retaining relative paths in the output object. 它接受glob并将文件合并为单个JSON blob,同时还保留输出对象中的相对路径。

Ie, dirA/a.json, dirB/b.json, and dirB/c.json get output as 即,dirA / a.json,dirB / b.json和dirB / c.json输出为

{
  "dirA": {
    "a": /*parsed contents of a.json*/
  },
  "dirB": {
    "b": /*parsed contents of b.json*/,
    "c": /*parsed contents of b.json*/
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM