我正在尝试按照react教程进行操作,我的 webpack.config.js 文件如下:
var webpack = require("webpack");
var pth = require("path");
module.exports = {
entry: "./src/index.js",
output: {
path: __dirname + "/dist",
filename: "bundle.js"
},
devServer: {
inline: true,
contentBase: './dist',
port: 3000
},
module: {
rules: [
{ test: /\.js$/, exclude: /(node_modules)/, use: 'babel-loader' },
{ test: /\.json$/, exclude: /(node_modules)/, use: 'json-loader' }
]
}
}
虽然我的代码文件如下:我在 lib.js 中制作了组件,并且在 index.js 中完成渲染,最终在 index.html 的 HTML div 中调用
库.js
import React from 'react'
import text from './titles.json'
export const hello = (
<h1 id='title'
className='header'
style={{backgroundColor: 'purple', color: 'yellow'}}>
{text.hello}
</h1>
)
export const goodbye = (
<h1 id='title'
className='header'
style={{backgroundColor: 'white', color: 'red'}}>
{text.goodbye}
</h1>
)
索引.js
import React from 'react'
import {render} from 'react-dom'
import {hello, goodbye} from './lib'
const design = {
backgroundColor: 'red',
color: 'white',
fontFamily:'verdana'
}
render(
<div>
{hello}
{goodbye}
</div>,
document.getElementById('react-container')
)
当我运行webpack -w
命令时,我遇到以下错误
ERROR in ./src/titles.json
Module parse failed: Unexpected token m in JSON at position 0
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token m in JSON at position 0
at Object.parse (native)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:447:3)
@ ./src/lib.js 12:14-38
@ ./src/index.js
ERROR in chunk main [entry]
bundle.js
Cannot read property 'replace' of undefined
我的 JSON 文件如下:titles.json
{
"hello": "Bonjour!",
"goodbye": "Au Revoir"
}
我的module版本如下: webpack 4.1.1 json-loader 0.5.7
我已经使用 npm install TIA 全局安装了 webpack 和 json-loader