今天,删除我的node_modules并使用npm install重新安装它们后,我的项目似乎无法正常工作。
这是我的webpack配置
const webpack = require('webpack');
const path = require('path');
const srcPath = path.join(__dirname, './client');
const nodeEnv = process.env.NODE_ENV || 'development';
const isProd = nodeEnv === 'production';
module.exports = {
devtool: isProd ? 'hidden-source-map' : 'cheap-module-eval-source-map',
context: path.join(__dirname, './client'),
entry: {
js: './index.js',
vendor: ['react']
},
output: {
path: path.join(__dirname, './static'),
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.html$/,
loader: 'file',
query: {
name: '[name].[ext]'
}
},
{
test: /\.scss$/,
loaders: ['style', 'css', 'sass']
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loaders: ['babel-loader']
},
{
test: /\.(png|jpg|gif|otf|eot|svg|ttf|woff|woff2)/,
loader: 'url-loader'
},
{
test: /\.(txt|json)/,
loader: 'raw-loader'
}
],
},
resolve: {
extensions: ['', '.js', '.jsx'],
modules: [
path.resolve('./client'),
'node_modules'
],
alias: {
stores: `${srcPath}/stores/`,
components: `${srcPath}/components/`,
services: `${srcPath}/services`,
models: `${srcPath}/models`,
constants: `${srcPath}/constants`,
sources: `${srcPath}/sources`,
images: `${srcPath}/assets/images`,
appConstants: isProd ? `${srcPath}/constants/_prod` : `${srcPath}/constants/_dev`
}
},
plugins: [
new webpack.IgnorePlugin(/regenerator|nodent|js\-beautify/, /ajv/),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: Infinity,
filename: 'vendor.bundle.js'
}),
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: isProd
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: !isProd
},
output: {
comments: !isProd
},
sourceMap: !isProd
}),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.optimize.OccurrenceOrderPlugin()
],
devServer: {
contentBase: './client',
hot: true,
port: 3000,
historyApiFallback: true
}
};
带有index.html的我的“client”文件夹和我的其余代码与webpack config位于同一个文件夹中。
Webpack确实成功构建,但转到localhost:3000
,我收到错误消息:“无法获取/”
转到localhost:3000/client/index.html
确实为我的index.html提供服务,但我的构建文件是使用
<script src="./vendor.bundle.js"></script>
<script src="./bundle.js"></script>
无法加载(GET到“http://localhost:3000/client/bundle.js”会导致404)
有谁知道发生了什么事?我无法解决这个问题,我想我已经尝试了一切,从更改路径,publicPath到更改contentBase以及将我的静态文件移动到不同的文件夹。 这很奇怪,因为只有在重新安装我的项目依赖项后才出现此问题。
非常感谢每一点帮助。感谢。
答案 0 :(得分:1)
webpack-dev-server@2.1.0-beta.3
中存在问题,导致contentBase
选项被忽略。您可以尝试升级到2.1.0-beta.4
吗?它刚刚发布。