webpack-dev-server 没有捆绑到输出文件
webpack-dev-server not bundling to output file
我可以按原样使用我的 webpack.config.js 进行 webpack,当我在浏览器中查看我的 index.html 而不使用 webpack-dev-server 时,更改会反映出来。
webpack.config.js
var webpack = require('webpack');
var path = require('path');
var APP_DIR = path.resolve(__dirname, 'src/client/app/');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public/');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
publicPath: BUILD_DIR,
filename: 'bundle.js'
},
watch: true,
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel-loader'
}
]
}
};
module.exports = config;
当我尝试 运行 webpack-dev-server 监视我的文件时,该服务识别出已保存,returns "Compiled Successfully" 并且应用程序指示它正在刷新我的 localhost:8080/webpack-dev-server/,但不会更新输出中指定的 bundle.js。我已经看到很多问题都是通过包含 publicPath 键值对来解决的,但是我已经包含了它,但是当我的服务器热更新时我仍然看不到编辑。
目录下安装的包如下
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": ""
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.4"
},
"devDependencies": {
"babel-core": "^6.26.0",
"webpack-dev-server": "^2.9.4"
}
}
我正在编译的 jsx 文件是一个简单的第一个反应组件
import React from 'react';
import {render} from 'react-dom';
class App extends React.Component {
render () {
return <p>Hello World!</p>;
}
}
render(<App/>, document.getElementById('app'));
将您的 webpack 配置更改为以下内容:
(注意:我更改了 APP_DIR 和 BUILD_DIR 只是为了在这里进行测试。
var webpack = require('webpack');
var path = require('path');
var APP_DIR = path.resolve(__dirname, 'src/');
var BUILD_DIR = path.resolve(__dirname, 'build/');
var config = {
entry: APP_DIR + '/index.js',
output: {
path: BUILD_DIR,
filename: 'bundle.js',
publicPath: '/'
},
watch: true,
module : {
loaders : [
{
test : /\.js?/,
include : APP_DIR,
loader : 'babel-loader'
}
]
},
devServer: {
contentBase: APP_DIR
}
};
module.exports = config;
注意我改了:
output.publicPath
:这是相对于服务器根目录,而不是绝对路径。更多信息:What does "publicPath" in webpack do?
devServer.contentBase
:我更改了这个,所以当我 运行 服务器时,/src
目录中的 html 文件会被命中。
这应该有效! =)
我可以按原样使用我的 webpack.config.js 进行 webpack,当我在浏览器中查看我的 index.html 而不使用 webpack-dev-server 时,更改会反映出来。
webpack.config.js
var webpack = require('webpack');
var path = require('path');
var APP_DIR = path.resolve(__dirname, 'src/client/app/');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public/');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
publicPath: BUILD_DIR,
filename: 'bundle.js'
},
watch: true,
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel-loader'
}
]
}
};
module.exports = config;
当我尝试 运行 webpack-dev-server 监视我的文件时,该服务识别出已保存,returns "Compiled Successfully" 并且应用程序指示它正在刷新我的 localhost:8080/webpack-dev-server/,但不会更新输出中指定的 bundle.js。我已经看到很多问题都是通过包含 publicPath 键值对来解决的,但是我已经包含了它,但是当我的服务器热更新时我仍然看不到编辑。
目录下安装的包如下
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": ""
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.4"
},
"devDependencies": {
"babel-core": "^6.26.0",
"webpack-dev-server": "^2.9.4"
}
}
我正在编译的 jsx 文件是一个简单的第一个反应组件
import React from 'react';
import {render} from 'react-dom';
class App extends React.Component {
render () {
return <p>Hello World!</p>;
}
}
render(<App/>, document.getElementById('app'));
将您的 webpack 配置更改为以下内容: (注意:我更改了 APP_DIR 和 BUILD_DIR 只是为了在这里进行测试。
var webpack = require('webpack');
var path = require('path');
var APP_DIR = path.resolve(__dirname, 'src/');
var BUILD_DIR = path.resolve(__dirname, 'build/');
var config = {
entry: APP_DIR + '/index.js',
output: {
path: BUILD_DIR,
filename: 'bundle.js',
publicPath: '/'
},
watch: true,
module : {
loaders : [
{
test : /\.js?/,
include : APP_DIR,
loader : 'babel-loader'
}
]
},
devServer: {
contentBase: APP_DIR
}
};
module.exports = config;
注意我改了:
output.publicPath
:这是相对于服务器根目录,而不是绝对路径。更多信息:What does "publicPath" in webpack do?devServer.contentBase
:我更改了这个,所以当我 运行 服务器时,/src
目录中的 html 文件会被命中。
这应该有效! =)