简体   繁体   English

为什么React需要webpack-dev-server来运行?

[英]Why React needs webpack-dev-server to run?

I'm new to React and I followed Facebook's Installation (Create a New App). 我是React的新手,我遵循了Facebook的Installation (创建新应用)。

So every time I need to run the app, it needs to start a server. 因此,每次我需要运行该应用程序时,都需要启动服务器。 When I try to open the build version in chrome( opening HTML directly ), nothing gets displayed. 当我尝试在chrome中打开构建版本(直接打开HTML)时,什么都没有显示。

Then I tried to setup React environment myself from scratch using codecademy tutorial. 然后,我尝试使用codecademy教程从头开始设置React环境。 Here, after I build the project, I can directly open the HTML in chrome and the contents are displayed. 在这里,构建项目后,可以直接在chrome中打开HTML,并显示内容。

My question is: 我的问题是:

Why webpage doesn't get displayed in the 1st method but 2nd method runs without starting server? 为什么在第一种方法中没有显示网页,而第二种方法在没有启动服务器的情况下运行?


Edit : 编辑

package.json for 2nd method: 第二种方法的package.json

{
  "name": "practice",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.6.1",
    "react-dom": "^15.6.1"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-react": "^6.24.1",
    "html-webpack-plugin": "^2.30.1",
    "webpack": "^3.6.0",
    "webpack-dev-server": "^2.8.2"
  }
}

webpack.config.js : webpack.config.js

var HTMLWebpackPlugin = require('html-webpack-plugin');
var HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
  template: __dirname + '/app/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = {
  entry: __dirname + '/app/index.js',
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  },
  output: {
    filename: 'transformed.js',
    path: __dirname + '/build'
  },
  plugins: [HTMLWebpackPluginConfig]
};

The problem is: path to the other files in the HTML . 问题是: HTML中其他文件的路径

When the webpack-dev-server is run from the directory, it assumes it is the root of the server. 当从目录运行webpack-dev-server ,将假定它是服务器的根目录。

So, if you open your app's HTML( build/index.html ) created with Facebook tutorial, you can see that the path to other files are given as absolute path but not as a relative path. 因此,如果打开应用程序的使用Facebook教程创建的HTML( build/index.html ),则可以看到其他文件的路径是绝对路径,而不是相对路径。

Like in HTML you can see /static/pathToFile but not ./static/pathToFile . 就像在HTML中一样,您可以看到/static/pathToFile但是看不到./static/pathToFile

在此处输入图片说明

So, according to your 2nd method, try to give the path to transformed.js as /transformed.js in your HTML. 因此,根据您的/transformed.js方法,尝试在HTML中以/transformed.js提供transformed.js的路径。 It doesn't display anything. 它不显示任何内容。 But if you run npm run start and then open your localhost with given port number(just like in the 1st method). 但是,如果您运行npm run start ,然后使用给定的端口号打开本地主机(就像第一种方法一样)。 Now you can see your React app. 现在您可以看到您的React应用程序。

Opinion : Always try to setup your environment by yourself from scratch. 意见 :始终尝试从头开始自行设置环境。 Don't try easy to setup methods like Facebook's "create new app". 不要尝试轻松设置诸如Facebook的“创建新应用”之类的方法。 Instead try Facebook's "Adding React to an Existing Application". 而是尝试Facebook的“将React添加到现有应用程序”。 You can learn how things actually work, like today. 您可以像今天一样了解事物的实际运作方式。

Tip : 提示

Try to debug the app always in your web browser! 尝试始终在网络浏览器中调试应用程序!

For example, open your 1st method HTML in your chrome and open developer tools. 例如,在chrome中打开您的第一种方法HTML,然后打开开发人员工具。

Head over to the network tab and reload. 转到“网络”标签并重新加载。 在此处输入图片说明

Hover-over the failed file to see what is the error. 将鼠标悬停在失败的文件上以查看错误是什么。 You can see ERR_FILE_NOT_FOUND . 您可以看到ERR_FILE_NOT_FOUND

Click on it to see return status, url requested etc. 单击它以查看返回状态,请求的URL等。

在此处输入图片说明

Hope it helps! 希望能帮助到你!

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

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