简体   繁体   English

Webpack - Yaml -> JSON -> 提取文件

[英]Webpack - Yaml -> JSON -> Extract file

I have a YAML file with a few translations.我有一个包含一些翻译的 YAML 文件。 I need to transform these files into a JSON file.我需要将这些文件转换为 JSON 文件。 I've tried using yaml-import-loader and json-loader but I get an error.我曾尝试使用yaml-import-loaderjson-loader但出现错误。

Here's my setup:这是我的设置:

const ExtractTextPlugin = require('extract-text-webpack-plugin');

const extractEnglish = new ExtractTextPlugin('lang/en.js');

module.exports = {
  entry: [
    './src/locales/application.en.yml',
  ],
  output: {
    filename: 'english.js',
  },
  module: {
    strictExportPresence: true,
    rules: [
      {
        test: /\.en\.yml$/,
        use: extractEnglish.extract({
          use: [
            // { loader: 'json-loader' },
            {
              loader: 'yaml-import-loader',
              options: {
                output: 'json',
              },
            }],
        }),
      },
    ],
  },
  plugins: [
    extractEnglish,
  ],
};

And the error I get:我得到的错误:

Users/xxx/Documents/Project/node_modules/extract-text-webpack-plugin/dist/index.js:188
            chunk.sortModules();
                  ^

TypeError: chunk.sortModules is not a function
    at /Users/xxx/Documents/Project/node_modules/extract-text-webpack-plugin/dist/index.js:188:19

Same error whether or not the json-loader is commented or not.无论json-loader是否被注释,都会出现相同的错误。

I really don't understand what is going wrong.我真的不明白出了什么问题。

Versions: "webpack": "2.6.1", "extract-text-webpack-plugin": "^3.0.0", "json-loader": "^0.5.7",版本: "webpack": "2.6.1", "extract-text-webpack-plugin": "^3.0.0", "json-loader": "^0.5.7",

Not sure if this will help your situation but I recently found a solution to my i18n loading problem.不确定这是否对您的情况有帮助,但我最近找到了解决 i18n 加载问题的方法。 I do this to extract YAML into JSON files upfront as I use angular-translate and needed to load files dynamically and on-demand.我这样做是为了将 YAML 提取到 JSON 文件中,因为我使用 angular-translate 并且需要动态和按需加载文件。 I avoid extract-text-webpack-plugin and use only loaders: file-loader and yaml-loader.我避免使用 extract-text-webpack-plugin 并且只使用加载器:file-loader 和 yaml-loader。

First I setup the import of my .yaml files near the beginning of source (in my case a specific chain of import files for webpack to process)首先,我在源代码的开头附近设置了我的 .yaml 文件的导入(在我的例子中是 webpack 处理的特定导入文件链)

 import "./i18n/en.user.yaml";

I updated webpack config to translate YAML to JSON and have it available to load dynamically (everything originates from my 'src' directory, hence the context):我更新了 webpack 配置以将 YAML 转换为 JSON 并使其可以动态加载(所有内容都来自我的“src”目录,因此是上下文):

 rules: [{
   test: /.\.yaml$/,
   use: [{
     loader: 'file-loader',
     options: {
       name: '[path][name].json',
       context: 'src'
      }
    },{
      loader: 'yaml-loader'
    }]
  }]

This will translate my yaml file(s) and export them to my public directory, in this case at '/i18n/en.user.json'.这将翻译我的 yaml 文件并将它们导出到我的公共目录,在本例中为“/i18n/en.user.json”。

Now when angular-translate uploads my configured i18n settings via $http on-demand, it already has the parsed YAML and avoids having to parse it with js-yaml (or similar) on the front end.现在,当 angular-translate 通过 $http 按需上传我配置的 i18n 设置时,它已经具有解析的 YAML 并避免在前端使用 js-yaml(或类似的)解析它。

A relatively old question, but I found it while searching for a solution to the same problem, so I thought it worth to chip in.一个相对较旧的问题,但我在寻找相同问题的解决方案时发现了它,所以我认为值得一试。

If you're not really using translation files in your code (ie you never import and use them directly) then using a Webpack loader is not the most elegant solution (you'd be forced to import them just so that the loader could be triggered and perform the conversion).如果你没有真正在你的代码中使用翻译文件(即你从不直接import和使用它们),那么使用 Webpack 加载器并不是最优雅的解决方案(你将被迫导入它们只是为了触发加载器并执行转换)。

An alternative would be to use the CopyWebpackPlugin instead: it supports a transform option , which takes a function receiving the content of the file as a Buffer.另一种方法是使用CopyWebpackPlugin代替:它支持transform选项,它将接收文件内容的函数作为缓冲区。

With a YAML parser (like js-yaml ) as an additional dependency, adding this to your Webpack configuration would work:使用 YAML 解析器(如js-yaml )作为附加依赖项,将其添加到您的 Webpack 配置中即可:

const yaml = require('js-yaml');

module.exports = {
  // OTHER WEBPACK CONFIG HERE

  plugins: [
    new CopyWebpackPlugin([
      {
        from: 'i18n/**/*',
        to: 'i18n/[name].json',
        transform: (content) => Buffer.from(
          JSON.stringify(
            yaml.safeLoad(content.toString('utf8'), { schema: yaml.JSON_SCHEMA })
          ),
          'utf8'
        )
      },
  ])
}

The i18n folder in the above example would contain your .yml translations.上面示例中的i18n文件夹将包含您的.yml翻译。 The Copy plugin would load them, convert them to JSON, and save them in the output folder under i18n/ (as specified by the to option). Copy 插件会加载它们,将它们转换为 JSON,并将它们保存在i18n/下的输出文件夹中(由to选项指定)。

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

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