Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to load css. #78

Open
Triyugi opened this issue Jul 20, 2017 · 4 comments
Open

Unable to load css. #78

Triyugi opened this issue Jul 20, 2017 · 4 comments

Comments

@Triyugi
Copy link

Triyugi commented Jul 20, 2017

When I am running code in browser, getting error: bundle.css status failed.

webpack.config.js is:

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  devtool: 'eval-source-map',
  entry: [
    'webpack-hot-middleware/client?reload=true',
    path.join(__dirname, 'app/index.js')
  ],
  output: {
    path: path.join(__dirname, '/dist/'),
    filename: '[name].js',
    publicPath: '/'
  },
  plugins: [
	**new ExtractTextPlugin('/bundle.css', { allChunks: true }),**
    new HtmlWebpackPlugin({
      template: 'app/index.html',
      inject: 'body',
      filename: 'index.html'
    }),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('production')
    })
  ],
  resolve: {
    extensions: ['', '.scss', '.css', '.js', '.json'],
    modulesDirectories: [
      'node_modules',
      path.resolve(__dirname, './node_modules')
    ]
  },
  module: {
    loaders: [{
      test: /\.jsx?$/,
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        "presets": ["react", "es2015", "stage-0", "react-hmre"]
      }
    }, {
      test: /\.json?$/,
      loader: 'json'
    }, {
		test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style', 'css?sourceMap&modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss!sass')
	}, {
		test: /\.css$/,
		loader: 'style-loader!css-loader'
    },{
		test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
        loader: "url-loader?limit=10000"
	},{
		test: /\.less$/, loader: "style-loader!css-loader!less-loader"
	},{
		test: /\.(ttf|eot|svg|woff|woff2)(\?v=[0-9]\.[0-9]\.[0-9])?$/, 
		loader: "file-loader"
	}]
  }
};

server.js file content is as follows:

const path = require("path");  
const express = require("express");  
const webpack = require("webpack");  
const webpackDevMiddleware = require("webpack-dev-middleware");  
const webpackHotMiddleware = require("webpack-hot-middleware");  
const config = require("./webpack.config.js");

const app           = express(),  
      DIST_DIR      = path.join(__dirname, "dist"),
      HTML_FILE     = path.join(DIST_DIR, "index.html"),
      isDevelopment = process.env.NODE_ENV !== "production",
      DEFAULT_PORT  = 3000,
      compiler      = webpack(config);

app.set("port", process.env.PORT || DEFAULT_PORT);

if (isDevelopment) {  
    app.use(webpackDevMiddleware(compiler, {
        publicPath: config.output.publicPath
    }));

    app.use(webpackHotMiddleware(compiler));

    app.get("*", (req, res, next) => {
        compiler.outputFileSystem.readFile(HTML_FILE, (err, result) => {
            if (err) {
                return next(err);
            }
            res.set('content-type', 'text/html');
            res.send(result);
            res.end();
        });
    });
}

else {  
    app.use(express.static(DIST_DIR));

    app.get("*", (req, res) => res.sendFile(HTML_FILE));
}

app.listen(app.get("port"));

package.json is as follows:

"main": "server.js",
  "script": {
    "start": "babel-node server-es6.js",
    "build:server": "babel server-es6.js --out-file server.js",
    "build:client": "webpack -p --config webpack.config.js --progress"
  },

Site is loading but some css are not loading. Console throw error:

GET http://bundle.css/ net::ERR_NAME_NOT_RESOLVED

@Triyugi
Copy link
Author

Triyugi commented Jul 20, 2017

Followup of #77

@Triyugi
Copy link
Author

Triyugi commented Jul 20, 2017

In fact, I am not able to access resources like, images. Image path comes correctly as http://localhost:3000/img/img1.png but its not showing in browser.
I think issue is with webpack publickpath.

@Triyugi
Copy link
Author

Triyugi commented Jul 20, 2017

When I am using <img src={require('/images/image-name.png')} />, its working fine. But I don't want to do that because its very heavy codebase and also I think its not a nice solution.

@Triyugi
Copy link
Author

Triyugi commented Jul 21, 2017

In chrome Network tool, image type is coming as text/html.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant