代码之家  ›  专栏  ›  技术社区  ›  connexo

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

  •  0
  • connexo  · 技术社区  · 6 年前

    我有个文件叫 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"
            }
        }
    ]

    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>)
    

    json-loader ,从Webpack 2.0.0版开始。我在用 .

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

    [{
        test: /(\.js)/,
        exclude: /(node_modules)/,
        loaders: ['babel-loader'],
    }, {
        test: /(\.jpg|\.png)$/,
        loader: 'url-loader?limit=10000',
    }]
    
    0 回复  |  直到 6 年前
        1
  •  3
  •   Artem Bozhko    6 年前

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