简体   繁体   English

Webpack-dev-server在HTML页面中注入包,但未呈现任何内容

[英]Webpack-dev-server injects bundle in HTML page, but nothing is rendered

When I run webpack-dev-server (Command: "webpack-dev-server --mode development --open --progress --hot"), my bundle is injected into the html page, but nothing is rendered. 当我运行webpack-dev-server(命令:“ webpack-dev-server --mode development --open --progress --hot”)时,我的包被注入到html页面中,但是什么也没有呈现。

Webpack.config.js file: Webpack.config.js文件:

var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    devtool: "cheap-eval-source-map",
    entry: path.join(__dirname, 'src', 'App.js'),
    output: {
        path: path.join(__dirname, 'dist'),
        filename: "bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: path.join(__dirname, 'src')
            }
        ]
    },
    devServer: {
        contentBase: path.join(__dirname, 'dist'),
        inline: true
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: path.join(__dirname, 'src', 'index.html'),
            hash: true,
        })
    ]
}

Index.js file: Index.js文件:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('app'));

App.js file: App.js文件:

import React from 'react'; 从'react'导入React;

export default class App extends React.Component {
    render() {
        return (
            <div className="container">
                <h1>React Starter App</h1>
            </div>
        );
    }
}

The thing is if I change App.js to: 问题是,如果我将App.js更改为:

alert("test");

It will display the alert on the page. 它将在页面上显示警报。

In your webpack.config.js , change your entry point to index.js instead of App.js since index.js handles your initial rendering: webpack.config.js ,将入口点更改为index.js而不是App.js因为index.js处理了初始呈现:

// webpack.config.js      
entry: path.join(__dirname, 'src', 'index.js');

The alert in App.js was being displayed because App.js was still getting bundled. 在警报App.js正在显示,因为App.js仍然得到捆绑在一起。 However, it wasn't being rendered as the render logic in index.js wasn't being bundled and executed by webpack. 但是,它没有被渲染,因为index.js的渲染逻辑没有被webpack捆绑和执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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