我是新来的React,我跟着Facebook's Installation(创建一个新的应用程序)。为什么React需要webpack-dev-server运行?
所以每次我需要运行的应用程序时,它需要启动服务器。当我尝试在chrome中打开构建版本(直接打开HTML)时,什么都不显示。
然后我试图安装使用教程codecademy从头开始作出反应的环境自己。在这里,在我构建项目之后,我可以直接在Chrome中打开HTML并显示内容。
我的问题是:
为什么网页不会被显示在第一个方法,但第2个方法,而无需启动服务器运行?
编辑:
的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:
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]
};
简单的答案..确定你为什么在你的codeacademy中运行'npm run build'和它做了什么。你应该能够弄清楚这一点。如果读完后仍然有问题,请回复 – Panther
@Panther在我的package.json中,构建脚本是'webpack',启动脚本是'webpack-dev-server'。所以,在我的第二种方法中,我只是调用'webpack'来调用加载器。我没有调用'start'启动服务器。我用package.json编辑了我的问题。 – unknownymouse
我的意思是'webpack'实际上是运行(使用babel),uglify并建立你的'build/dist'文件夹。因此,从它们打开索引文件将显示所有必需的js/css存在的内容。在使用'dev-server'时,不会生成acutal文件。所需的js/css大多是来自文件系统的服务器。 – Panther