5

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',
}]
3
  • 1
    did you try adding file-loader rule? like '{ type: 'javascript/auto', test: /\.(json)/, exclude: /(node_modules|bower_components)/, use: [{ loader: 'file-loader', options: { name: '[name].[ext]' }, }], }' Commented Mar 18, 2019 at 13:13
  • 2
    Is there a rogue semicolon at the end of your JSON file? Commented Mar 18, 2019 at 13:15
  • @madebydavid No, the JSON file is definitely correct. Commented Mar 18, 2019 at 21:15

1 Answer 1

7

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.