Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import JSON file in Javascript using Webpack 4

I have a file called generic-map.json containing the following:

[
    {
        "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"
        }
    }
]

In a Javascript file I'm trying to import this file:

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

Webpack gives me the following error:

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

I do know that webpack does no longer need json-loader, beginning with Webpack version 2.0.0. I'm using Webpack 4.20.2.

This is what my loaders configuration looks like:

[{
    test: /(\.js)/,
    exclude: /(node_modules)/,
    loaders: ['babel-loader'],
}, {
    test: /(\.jpg|\.png)$/,
    loader: 'url-loader?limit=10000',
}]
like image 522
connexo Avatar asked Mar 04 '23 08:03

connexo


1 Answers

Replace test: /(\.js)/ to test: /\.js$/ or test: /\.jsx?$/

babel-loader process a .json file because of wrong test expression.

like image 105
Artem Bozhko Avatar answered Mar 16 '23 22:03

Artem Bozhko