Webpack文件加载器'意外字符'错误生成

时间:2017-08-13 06:53:36

标签: webpack webpack-file-loader survivejs

我一直在尝试许多不同的配置,但我现在觉得我已经盯着这个问题太久了,再也无法理解了。我正在尝试使用webpack file-loader来获取简单的图像,而正常的配置就像dev中的一个魅力一样,每当我使用SSR运行npm run start时,我都会遇到标题中提到的错误。要在此处提供其他信息,请参阅我的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/jspublic/images,并且webpack正确输出此文件夹中的图像。我觉得我在某个地方造成了分心错误,所以如果您需要任何其他信息来帮我解决这个问题,请告诉我。这是一个全新的设置,因此每个包都是最新版本。

修改

我实际上注意到,除非我首先运行webpack -p(创建带有使用过的图像的public/images文件夹),在开发模式下,控制台告诉我它找不到任何图像,开发构建不会抛出任何错误,但路径有一些东西,我只能在没有指定任何路径的情况下使其工作。在生产中,此配置“有效”,但我收到Unexpected character错误。

0 个答案:

没有答案