使用 Webpack 4 在 Javascript 中导入 JSON 文件

Import JSON file in Javascript using Webpack 4

我有一个名为 generic-map.json 的文件,其中包含以下内容:

[
    {
        "names": ["text", "description"],
        "map": {
            "name": "textContent",
            "target": "property",
            "type": "string"
        }
    },
    {
        "names": ["checked"],
        "map": {
            "name": "checked",
            "target": "property",
            "type": "boolean"
        }
    },
    {
        "names": ["disabled", "readonly"],
        "map": {
            "name": "disabled",
            "target": "property",
            "type": "boolean"
        }
    },
    {
        "names": ["title", "tooltip"],
        "map": {
            "name": "title",
            "target": "property",
            "type": "string"
        }
    },
    {
        "names": ["cssclass", "classname"],
        "map": {
            "name": "",
            "target": "classList",
            "type": "string"
        }
    },
    {
        "names": ["tabindex"],
        "map": {
            "name": "tabIndex",
            "target": "attribute",
            "type": "string"
        }
    }
]

在 Javascript 文件中,我正在尝试导入此文件:

import generic from './generic-map.json';

Webpack 给我 以下错误:

./src/utils/property-mapping/generic-map.json
Module parse failed: Unexpected token ; in JSON at position 733 while parsing near '...type": "string" } }];'
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token ; in JSON at position 733 while parsing near '...type": "string" } }];'
    at JSON.parse (<anonymous>)

我知道 webpack 不再需要 json-loader,从 Webpack 版本 2.0.0 开始。我正在使用 Webpack 4.20.2.

这是我的加载程序配置的样子:

[{
    test: /(\.js)/,
    exclude: /(node_modules)/,
    loaders: ['babel-loader'],
}, {
    test: /(\.jpg|\.png)$/,
    loader: 'url-loader?limit=10000',
}]

test: /(\.js)/替换为test: /\.js$/test: /\.jsx?$/

babel-loader 处理一个 .json 文件,因为错误的测试表达式。