繁体   English   中英

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

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

我是React.js的新手。 我使用“ create-react-app”命令创建了项目。 我想添加服务器,所以弹出项目。 但是位于src文件夹下的文件不会出现在localhost:4000上。 我可以在index.html中看到内容。 我怎么了

这是我的package.json脚本

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

这是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只加载index.html的内容,因为它看不到捆绑文件bundle.js 在您的配置中,捆绑文件的目的地是目录'/'

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

但是webpack-dev-server'./public'目录( contentBase: './public' )提供内容。 您应该告诉webpack这样查看公共目录和根目录

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

不要忘记在index.html中引用捆绑文件。

暂无
暂无

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

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