简体   繁体   中英

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',
}]

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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