编辑:链接到主持此示例的github repo是here,以防有人想要运行它
我遇到了与另一个用户几乎完全相同的问题(你可以找到问题here),因为运行webpack-dev-server确实可以正确编译和监视文件(看到控制台输出)终端),但浏览器仍然无法正确查看我的网站。这是我的webpack.config.js文件:
var webpack = require('webpack'),
path = require('path'),
// webpack plugins
CopyWebpackPlugin = require('copy-webpack-plugin');
var config = {
context: path.join(__dirname,'app'),
entry: './index.js',
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js',
publicPath: path.join(__dirname, 'public')
},
devServer: {
// contentBase: './public/'
},
plugins: [
// copies html to public directory
new CopyWebpackPlugin([
{ from: path.join(__dirname, 'app', 'index.html'),
to: path.join(__dirname, 'public')}
]),
// required bugfix for current webpack version
new webpack.OldWatchingPlugin()
],
module: {
loaders: [
// uses babel-loader which allows usage of ECMAScript 6 (requires installing babel-preset-es2015)
{test: /\.js$/, loader: 'babel', exclude: /node_modules/, query: { presets: ['es2015']}},
// uses the css-loader (loads css content) and style-loader (inserts css from css-loader into html)
{test: /\.css$/, loader: 'style!css', exclude: /node_modules/}
]
}
};
module.exports = config;
这是我的目录结构:
+--- webpack/
+--- app/
+--- index.html
+--- index.js
+--- styles.css
+--- package.json
+--- webpack.config.js
目前,运行webpack-dev-server
会在浏览器中输出以下内容(请注意缺少public/
目录,这是webpack通常输出我的html和javascript包的地方):
编辑:添加devServer.contentBase属性并将其设置为public
会让浏览器返回403错误,如下所示:
答案 0 :(得分:2)
好的,所以我能够重现你在我的项目中遇到的问题。为了解决这个问题,我改变了一些事情。
这是我设置的内容。我在输出中定义了一点,并使用jsx而不是js,但结果应该是相同的。您可以将我的src
替换为您的源代码。
const config = {
entry: './src/App.jsx',
output: {
filename: 'app.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react', 'stage-0'],
plugins: ['add-module-exports']
}
},
{
include: /\.json$/, loaders: ['json-loader']
},
{
test: /\.scss$/,
loaders: ['style', 'css?modules', 'sass']
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file?name=fonts/[name].[ext]'
}
]
},
plugins: [
new webpack.ProvidePlugin({
'Promise': 'exports?module.exports.Promise!es6-promise',
'fetch': 'imports?self=>global!exports?global.fetch!isomorphic-fetch'
}),
new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
})
],
resolve: {
root: path.resolve('./src')
},
devServer: {
contentBase: 'src'
}
};
所以基本上你想在终端输出这个输出:
webpack result is served from /
- 告诉我们,无论我们建立什么,都将在根content is served from src
- 告诉我们它正在从该目录构建希望这有帮助