简体   繁体   English

(React Webpack)在开发模式下运行项目时,不会加载src文件夹下的文件

[英](React webpack) Files under the src folder are not loaded when run project on development mode

I'm new in React.js. 我是React.js的新手。 I created project with 'create-react-app' command. 我使用“ create-react-app”命令创建了项目。 I wanted to add server, so eject project. 我想添加服务器,所以弹出项目。 But files that located under the src folder don't appear on localhost:4000. 但是位于src文件夹下的文件不会出现在localhost:4000上。 I can see contents in index.html. 我可以在index.html中看到内容。 What is my problem? 我怎么了

Here is my package.json scripts 这是我的package.json脚本

"scripts": {
    "development": "cross-env NODE_ENV=development nodemon --exec babel-node --presets=es2015 ./server/main.js --watch server"
}

And this is webpack.dev.config.js 这是webpack.dev.config.js

  var webpack = require('webpack');
    var path = require('path');

    module.exports = {

    entry: [
        'babel-polyfill',
        './src/index.js',
        'webpack-dev-server/client?http://0.0.0.0:4000',
        'webpack/hot/only-dev-server',
        './src/style.css'
    ],

    output: {
        path: '/',
        filename: 'bundle.js'
    },

    devServer: {
        hot: true,
        filename: 'bundle.js',
        publicPath: '/',
        historyApiFallback: true,
        contentBase: './public',
        proxy: {
            "*": "http://localhost:3000"
        }
    },

    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ],

    resolve: {
      modules: [path.resolve("./src"), 'node_modules']
    },

    module: {
        loaders: [
            {
                test: /\.js$/,
                loaders: ['babel-loader?' + JSON.stringify({
                    cacheDirectory: true,
                    presets: ['es2015', 'react']
                })],
                exclude: /node_modules/,
            },
            {
                test: /\.css$/,
                loader: 'style-loader!css-loader'
            }
        ]
    }
};

webpack-dev-server loads only the content of index.html because it doesn't see the bundle file bundle.js . webpack-dev-server只加载index.html的内容,因为它看不到捆绑文件bundle.js In your configuration, the destination of the bundle file is the directory '/' . 在您的配置中,捆绑文件的目的地是目录'/'

 output: {
        path: '/',
        filename: 'bundle.js'
    }

But webpack-dev-server serve contents from './public' directory ( contentBase: './public' ). 但是webpack-dev-server'./public'目录( contentBase: './public' )提供内容。 You should tell webpack to look at public directory and root directory like this 您应该告诉webpack这样查看公共目录和根目录

devServer: {
        hot: true,
        filename: 'bundle.js',
        publicPath: '/',
        historyApiFallback: true,
        contentBase: ['/', '/public'],
        proxy: {
            "*": "http://localhost:3000"
        }
    }

Don't forget to reference the bundle file in index.html. 不要忘记在index.html中引用捆绑文件。

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

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