2

I have been trying many different configurations but I now feel like I have been staring at this problem for too long and can't figure it out anymore. I am trying to use webpack file-loader for simple images, while a normal configuration works like a charm in dev, I encounter the error mentioned in the title any time I run npm run start with SSR. To provide additional info here are the rules in my webpack config:

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;

Here are me dependencies:

  "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"
  }

Just for completion my public folder is structured like this public/js and public/images, and webpack properly outputs the images in this folder. I feel like I made a distraction error somewhere, so please let me know if you need any additional info to help me figure this out. This is a brand new setup so every package is at its latest version.

EDIT

I actually noticed that unless I run webpack -p first (which creates the public/images folder with the used images inside), in dev mode the console tells me it can't find any image, the dev build doesn't throw any error but there's something off with the paths, I can only make it work if I do not specify any path. In production this configuration "works" but I get the Unexpected character error.

G4bri3l
  • 4,996
  • 4
  • 31
  • 53
  • can you post your package.json dependencies and devDependencies. Also are you running webpack on the production machine, or are you building and pushing a built file to production to be ran? – thomasmeadows Aug 13 '17 at 13:41
  • I'll post the dependencies in a minute, but I'm just running npm start locally and simulating a production environment. I build then run npm start and I get the unexpected character error. – G4bri3l Aug 13 '17 at 16:39
  • @thomasmeadows I added my dependencies as well – G4bri3l Aug 13 '17 at 16:56
  • can you show your file structure, with the unexpected character filename, and your package.json scripts might help and the rest of your webpack file. – thomasmeadows Aug 13 '17 at 17:34
  • also I think file-loader's public path should be /images not /js/. /js/ implies your image urls are going to be at www.yourdomain.com/js/ – thomasmeadows Aug 13 '17 at 17:40
  • For the sake of simplicity I removed all subfolders and I am now outputting everything in `/public`, I updated the question with more info. Thanks for helping out! – G4bri3l Aug 13 '17 at 17:51
  • you could try instead of doing webpack -p . In your plugins array for production add new webpack.optimize.UglifyJsPlugin({mangle: false}); -p is simply a shortcut to add uglification. – thomasmeadows Aug 13 '17 at 18:17
  • Sure thanks, although that doesn't help solving the images issue. – G4bri3l Aug 13 '17 at 18:29
  • If that didnt help, I would need more info about the error/problem that is occurring because everything else looks fine in your configuration and package.json. Usually that solves problems when there is a discrepancy between production and dev builds. – thomasmeadows Aug 13 '17 at 18:49
  • Alright I'll do this, I will create a smaller very basic version of what I have now and see if the file loader works, then go from there until I find out if it's the loader or something else conflicting. I'll keep you posted. – G4bri3l Aug 13 '17 at 19:26
  • Nothing still no luck, I will file an issue directly in the repo at this point. – G4bri3l Aug 13 '17 at 20:37
  • I missed a key detail, I am doing SSR. So it boils down to babel trying to transpile images as js files I guess, I am trying a `transform-assets` babel plugin now which almost solves this issue. – G4bri3l Aug 13 '17 at 22:15

0 Answers0