我一直在尝试许多不同的配置,但我现在觉得我已经盯着这个问题太久了,无法再弄清楚了。我正在尝试将 webpackfile-loader
用于简单的图像,而正常的配置在开发中就像一个魅力,每当我npm run start
使用 SSR 运行时,我都会遇到标题中提到的错误。要在此处提供更多信息,请参考我的 webpack 配置中的规则:
const path = require('path');
const webpack = require('webpack');
const config = {
context: __dirname,
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./src/ClientApp.jsx'
],
devtool: 'cheap-eval-source-map',
output: {
path: path.join(__dirname, '/public/'),
filename: 'bundle.js',
publicPath: '/public/'
},
devServer: {
hot: true,
contentBase: path.join(__dirname, '/public/'),
publicPath: '/public/',
historyApiFallback: true
},
resolve: {
extensions: ['.js', '.jsx', '.json']
},
stats: {
colors: true,
reasons: true,
chunks: true
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin()
],
module: {
rules: [
{
test: /\.(png|jpg|gif)$/,
loader: 'file-loader',
},
{
enforce: 'pre',
test: /\.jsx?$/,
loader: 'eslint-loader',
exclude: /node_modules/
},
{
test: /\.jsx?$/,
loader: 'babel-loader'
}
]
}
};
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'server') {
config.entry = './src/ClientApp.jsx';
config.devtool = false;
config.plugins = [];
}
module.exports = config;
这是我的依赖项:
"scripts": {
"build": "cross-env NODE_ENV=production webpack -p",
"build:dev": "cross-env NODE_ENV=development webpack -d",
"dev": "webpack-dev-server",
"lint": "eslint **/*.{js,jsx} --quiet",
"start": "cross-env NODE_ENV=production node server/server.js",
"watch": "webpack --watch"
},
"dependencies": {
"babel-plugin-dynamic-import-webpack": "^1.0.1",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-register": "^6.24.1",
"compression": "^1.7.0",
"express": "^4.15.3",
"lodash": "^4.17.4",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-addons-perf": "^15.4.2",
"react-dom": "^15.6.1",
"react-redux": "^5.0.5",
"react-router-dom": "^4.1.2",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"styled-components": "^2.1.2",
"webpack": "^3.4.1"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^7.1.1",
"babel-plugin-dynamic-import-node": "^1.0.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"cross-env": "^5.0.3",
"eslint": "^4.3.0",
"eslint-config-airbnb": "^15.1.0",
"eslint-config-prettier": "^2.3.0",
"eslint-config-react": "^1.1.7",
"eslint-import-resolver-webpack": "^0.8.3",
"eslint-loader": "^1.9.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-prettier": "^2.1.2",
"eslint-plugin-react": "^7.1.0",
"file-loader": "^0.11.2",
"prettier": "^1.5.3",
"react-hot-loader": "^3.0.0-beta.6",
"svg-inline-loader": "^0.8.0",
"webpack-dev-server": "^2.6.1"
}
只是为了完成,我的public
文件夹的结构是这样public/js
的public/images
,并且 webpack 正确地输出了这个文件夹中的图像。我觉得我在某处犯了分心错误,所以如果您需要任何其他信息来帮助我解决这个问题,请告诉我。这是一个全新的设置,因此每个包都是最新版本。
编辑
我实际上注意到,除非我webpack -p
先运行(它会创建public/images
包含使用过的图像的文件夹),否则在开发模式下控制台告诉我它找不到任何图像,开发构建不会抛出任何错误,但是有一些问题路径,如果我不指定任何路径,我只能使其工作。在生产中,此配置“有效”,但Unexpected character
出现错误。