【发布时间】:2018-09-10 00:39:12
【问题描述】:
我在安装 webpack 后遇到了这个异常错误,搜索了整个 web 尝试了所有解决方案,但没有任何效果,
//我的 webpack.config.js 文件
const webpack = require("webpack");
const path = require("path");
const config = {
entry : path.resolve(__dirname,"src/index.js"),
output : {
path : path.resolve(__dirname,"dist/assets"),
filename : "bundle.js",
publicPath : "assets"
},
devServer : {
inline : true,
contentBase : path.resolve(__dirname,"dist"),
port : 3000
},
module : {
rules : [
{
test : /\.js$/,
exclude : path.resolve(__dirname,"node_modules"),
loader : "babel-loader",
query: {
presets: ["env","latest","react","stage-0","es2015"]
}
},
{
test : /\.css$/,
loader: 'style-loader!css-loader!autoprefixer-loader'
},
{
test : /\.scss$/,
loader: 'style-loader!css-loader!autoprefixer-loader!sass-loader'
}
]
}
};
module.exports = config;
我的 babelrc 文件
{
"presets" : ["env","latest","react","stage-0","es2015"]
}
Index.js 文件
import React from 'react'
import ReactDOM from 'react-dom'
import { hello, goodbye } from './lib'
ReactDOM.render(
<div>
{hello}
{goodbye}
</div>,
document.getElementById('root')
);
lib.js 文件
import React from 'react'
import text from './titles.json'
import './stylesheets/hello.css'
import './stylesheets/goodbye.scss'
export const hello = (
<h1 id="title"
className="hello">
{text.hello}
</h1>
);
export const goodbye = (
<h2 id="goodbye"
className="goodbye">
{text.bye}
</h2>
);
titles.json
{
"hello" : "Bonjour",
"bye" : "Au Revoir"
}
我没有在 webpack.config 文件中包含 json 加载器,因为我发现 webpack 中默认添加了 json 加载器,当我在控制台中检查浏览器时出现此错误 -> ReferenceError: ReactDOM is not defined.
我在 CLI 中遇到的错误
//文件夹结构
【问题讨论】:
标签: javascript reactjs webpack webpack-dev-server babel-loader