CSS won't load when using CSS modules - css

I am trying to write a simple react component in rescript and have a CSS file for the component. I am trying to do this with Webpack.
Here is my entire project code
Webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const outputDir = path.join(__dirname, "build/");
module.exports = {
mode: "development",
entry: "./src/Index.bs.js",
output: {
path: outputDir,
filename: "index.js"
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
inject: false
})
],
devServer: {
compress: true,
static: outputDir,
port: 8000
},
module: {
rules: [
{
test: /\.css/,
use: [
'style-loader',
{
loader: "css-loader",
options: {
importLoaders: 1,
modules: true,
}
}
]
}
]
}
}
My component
%%raw("import './Tutorial.css'")
#react.component
let make = () => {
<div>
<h1>{"Welcome to my app" -> React.string}</h1>
<ul className="menu">
<li className="menu-item">{"Company2" -> React.string}</li>
<li className="menu-item">{"Mission" -> React.string}</li>
<li className="menu-item">{"Values" -> React.string}</li>
<li className="menu-item">{"Leadership" -> React.string}</li>
</ul>
</div>
}
The CSS file I am trying to load
.menu {
list-style-type: none;
}
.menu-item {
display: inline;
color: red;
padding: 0 20px 0 20px;
}
my bsconfig.json
{
"name": "react-tutorial",
"version": "0.1.0",
"sources": {
"dir" : "src",
"subdirs" : true
},
"package-specs": {
"module": "es6",
"in-source": true
},
"suffix": ".bs.js",
"jsx": {"version": 4, "mode": "classic"},
"bs-dependencies": ["#rescript/react"],
"reason": {"react-jsx": 3}
}
Here is package.json
{
"name": "react-tutorial",
"version": "0.1.0",
"scripts": {
"clean": "npx rescript clean -with-deps",
"build": "npx rescript build && webpack",
"watch": "npx rescript build -w",
"server": "webpack-dev-server"
},
"keywords": [
"ReScript"
],
"author": "",
"license": "MIT",
"devDependencies": {
"css-loader": "^6.7.3",
"html-webpack-plugin": "^5.5.0",
"rescript": "^10.0.1",
"style-loader": "^3.3.1",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.11.1"
},
"dependencies": {
"#rescript/react": "^0.10.3"
}
}
However it just won't load the CSS. I think I followed the documentation and the steps to configure webpack correctly but it just doesn't work.
Here is my full code if anyone is interested. https://github.com/abhsrivastava/react-tutorial

You have CSS modules enabled, but you're not importing it as a CSS module. The CSS is being loaded, but the point of CSS modules is module isolation, which means the CSS class names need to be mangled. Instead of using the class names directly you need to do so via a lookup table, which is returned when you're importing the CSS module.
So you need to do two things:
Import the CSS module as a module, binding the lookup table to a name:
#module("./Tutorial.css") #val external css: {..} = "default"
Use the lookup table to get the mangled class name. That is, instead of className="menu", use className=css["menu"].
The full working code is this:
#module("./Tutorial.css") #val external css: {..} = "default"
#react.component
let make = () => {
<div>
<h1>{"Welcome to my app" -> React.string}</h1>
<ul className=css["menu"]>
<li className=css["menu-item"]>{"Company2" -> React.string}</li>
<li className=css["menu-item"]>{"Mission" -> React.string}</li>
<li className=css["menu-item"]>{"Values" -> React.string}</li>
<li className=css["menu-item"]>{"Leadership" -> React.string}</li>
</ul>
</div>
}

Related

How can I get Bootstrap to work in my build too, not only in dev version?

In my simple React example with SASS I apply some CSS from bootstrap.min.css. If I serve the example in dev mode everything works fine. However when I serve a build made in production mode, the Bootstrap CSS is not getting applied, although bootstrap.min.css is part of the bundle. You can see this effect in Component with and without Bootstrap CSS applied.
My App.js routes to component "SearchPage" containing an "input" and a "button" html element. The Bootstrap CSS in these elements is not being applied in the build.
I have included SearchPage\index.js and SearchPage\index.scss at the bottom of this post. The complete project can be viewed at GitHub at https://github.com/TomJsDev/search-address.
When looking at the build's sourcemaps in Chrome dev tools, I can see that the requested CSS is present inside bootstrap.min.css. When I inspect the style rules for e.g the button, bootstrap.min.css is mentioned several times but the rules for color etc. are just not present.
I spent many hours trying out everything I could find on the internet. Making all sorts of imports at the entry point in index.js and index.html, trying different paths for the imports, reinstalling all needed packages, creating and importing an index.scss with #import '~bootstrap/scss/bootstrap'; inside. Nothing worked.
What am I doing wrong? Are the loaders in webpack.config.prod.js perhaps the issue?
Thanks for the help.
Files
index.html
<html lang="en">
<head>
<title>Search address</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
</head>
<body>
<div id="app"></div>
</body>
</html>
index.js
import { render } from 'react-dom'
import { BrowserRouter as Router } from 'react-router-dom'
import App from './components/App' // eslint-disable-line import/no-named-as-default
import 'bootstrap/dist/css/bootstrap.min.css'
// import './index.scss'
render(
<Router>
<App />
</Router>,
document.getElementById('app')
)
package.json
{
"name": "search-address",
"description": "Search address",
"scripts": {
"start": "npm run start:dev",
"start:dev": "webpack serve --config webpack.config.dev.js --port 2121",
"clean:build": "rimraf build && mkdir build",
"prebuild": "npm run clean:build",
"build": "webpack --config webpack.config.prod.js",
"postbuild": "npm run serve:build",
"serve:build": "http-server ./build"
},
"dependencies": {
"bluebird": "^3.7.2",
"bootstrap": "5.1.3",
"classnames": "^2.3.1",
"prop-types": "15.7.2",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-redux": "7.2.4",
"react-router-dom": "5.2.0",
"sass": "1.51.0"
},
"devDependencies": {
"#babel/core": "7.14.6",
"#testing-library/react": "^12.0.0",
"#wojtekmaj/enzyme-adapter-react-17": "^0.6.2",
"autoprefixer": "10.4.7",
"babel-eslint": "10.1.0",
"babel-loader": "8.2.2",
"babel-preset-react-app": "10.0.0",
"css-loader": "5.2.6",
"cssnano": "5.0.6",
"enzyme": "3.11.0",
"eslint": "7.30.0",
"eslint-loader": "4.0.2",
"eslint-plugin-import": "2.23.4",
"eslint-plugin-react": "7.24.0",
"html-webpack-plugin": "5.3.2",
"http-server": "^14.1.0",
"mini-css-extract-plugin": "2.1.0",
"node-fetch": "^2.6.1",
"npm-run-all": "4.1.5",
"postcss": "^8.3.5",
"postcss-loader": "6.1.1",
"react-test-renderer": "17.0.2",
"sass-loader": "^12.6.0",
"style-loader": "3.0.0",
"webpack": "5.44.0",
"webpack-bundle-analyzer": "4.4.2",
"webpack-cli": "^4.9.0",
"webpack-dev-server": "^4.7.4"
},
"engines": {
"node": ">=8"
},
"babel": {
"presets": [
"babel-preset-react-app"
]
},
"eslintConfig": {
"root": true,
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:import/errors",
"plugin:import/warnings"
],
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true
},
"rules": {
"no-debugger": "off",
"no-console": "off",
"no-unused-vars": "warn",
"react/prop-types": "warn",
"react/no-unescaped-entities": [
"error",
{
"forbid": []
}
],
"no-mixed-spaces-and-tabs": [
"warn",
"smart-tabs"
]
},
"settings": {
"react": {
"version": "detect"
}
}
}
}
webpack.config.prod.js
const webpack = require('webpack')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const webpackBundleAnalyzer = require('webpack-bundle-analyzer')
process.env.NODE_ENV = 'production'
module.exports = {
mode: 'production',
target: 'web',
devtool: 'source-map',
entry: './src/index',
output: {
path: path.resolve(__dirname, 'build'),
publicPath: '/',
filename: 'bundle.js'
},
stats: 'minimal',
performance: {
maxEntrypointSize: 512000,
maxAssetSize: 512000
},
plugins: [
new webpackBundleAnalyzer.BundleAnalyzerPlugin({ analyzerMode: 'static' }),
new MiniCssExtractPlugin({ filename: '[name].[contenthash].css' }),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new HtmlWebpackPlugin({
template: 'src/index.html',
favicon: 'src/images/search-address-icon.png',
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
})
],
module: {
rules: [
{
test: /\.(js|jsx)$/i,
exclude: /node_modules/,
use: ['babel-loader', 'eslint-loader']
},
{
test: /\.(css|scss|less)$/i,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: { sourceMap: true, modules: true }
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [() => require('autoprefixer'), () => require('cssnano')]
},
sourceMap: true
}
},
{
loader: 'sass-loader'
}
]
},
{
test: /\.(png|svg|jpg|jpeg|gif|ico)$/i,
type: 'asset/resource'
}
]
}
}
SearchPage/index.js
import React from 'react'
import styles from './index.scss'
import join from 'classnames'
export const SearchPage = () => (
<div className={styles.searchPage}>
<h1>Find address</h1>
<form id='searchComponent' className={styles.searchComponent}>
<input
type='search'
placeholder='Type postal code'
pattern='[a-zA-Z0-9]{6}'
maxLength='6'
className={join('form-control', styles.searchInput)}
></input>
<button type='submit' form='searchComponent' className={'btn btn-primary'}>
Zoeken
</button>
</form>
<p>No address to show.</p>
</div>
)
export default SearchPage
SearchPage/index.scss
.searchComponent {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
column-gap: 0.5rem;
width: 100%;
max-width: 21.5rem;
}
.searchContainer {
display: flex;
flex-direction: column;
row-gap: 1.5rem;
}
.searchInput {
padding: 0.375rem 0.5rem !important;
}
.searchPage {
display: flex;
flex-direction: column;
align-items: center;
row-gap: 1.5rem;
}

Semicolon or block expected error svelte with postcss

I have set up a project with snowpack for svelte in which I'm trying to use tailwind for styling, but using states like hover or focus results in vs code throwing the error
Semicolon or block is expected
If you expect this syntax to work, here are some suggestions:
If you use less/SCSS with `svelte-preprocess`, did you add `lang=\"scss\"`/`lang=\"less\"` to your `style` tag? If you use SCSS, it may be necessary to add the path to your NODE runtime to the setting `svelte.language-server.runtime`, or use `sass` instead of `node-sass`.
Did you setup a `svelte.config.js`?
See https://github.com/sveltejs/language-tools/tree/master/docs#using-with-preprocessors for more info.
One code example that is causing the problem is
<style lang="postcss">
button {
#apply py-2 px-6 rounded-lg shadow-md;
}
.hoverable {
#apply hover:opacity-90;
}
</style>
This is the package.json
{
"scripts": {
"start": "run-p routify snp",
"build": "routify -b && routify export && snowpack build",
"test": "web-test-runner \"src/**/*.test.ts\"",
"routify": "routify",
"snp": "snowpack dev"
},
"dependencies": {
"#snowpack/plugin-run-script": "^2.3.0",
"postcss-import": "^14.0.2",
"svelte": "^3.37.0"
},
"devDependencies": {
"#roxi/routify": "^2.18.3",
"#snowpack/plugin-dotenv": "^2.2.0",
"#snowpack/plugin-postcss": "^1.4.3",
"#snowpack/plugin-svelte": "^3.6.1",
"#snowpack/plugin-typescript": "^1.2.1",
"#snowpack/web-test-runner-plugin": "^0.2.2",
"#testing-library/svelte": "^3.0.3",
"#tsconfig/svelte": "^1.0.10",
"#types/chai": "^4.2.17",
"#types/mocha": "^8.2.2",
"#types/snowpack-env": "^2.3.3",
"#web/test-runner": "^0.13.3",
"autoprefixer": "^10.4.0",
"chai": "^4.3.4",
"concurrently": "^6.4.0",
"cross-env": "^7.0.3",
"npm-run-all": "^4.1.5",
"postcss": "^8.3.11",
"postcss-cli": "^9.0.2",
"postcss-load-config": "^3.1.0",
"snowpack": "^3.8.7",
"svelte-preprocess": "^4.7.2",
"tailwindcss": "^2.2.19",
"typescript": "^4.3.4"
},
"routify": {
"extensions": "svelte,html,svx,md",
"dynamicImports": false,
"routifyDir": "src/.routify"
}
}
As you can see, I have installed svelte-preprocess which should be responsible for processing the postcss, as well as the other needed packages. The configuration for the project is as follows:
svelte.config.js
const sveltePreprocess = require("svelte-preprocess");
module.exports = {
preprocess: sveltePreprocess({
defaults: {
script: "typescript",
style: "postcss",
},
postcss: true,
}),
};
snowpack.config.js
/** #type {import("snowpack").SnowpackUserConfig } */
module.exports = {
mount: {
public: { url: "/", static: true },
src: { url: "/dist" },
},
plugins: [
"#snowpack/plugin-svelte",
"#snowpack/plugin-dotenv",
"#snowpack/plugin-typescript",
"#snowpack/plugin-postcss",
],
routes: [
/* Enable an SPA Fallback in development: */
{ match: "routes", src: ".*", dest: "/index.html" },
],
optimize: {
/* Example: Bundle your final build: */
// "bundle": true,
},
packageOptions: {
knownEntrypoints: ["#roxi/routify/runtime/buildRoutes"],
},
devOptions: {
/* ... */
},
buildOptions: {
/* ... */
},
};
postcss.config.js
module.exports = {
plugins: [
require("postcss-import"),
require("tailwindcss"),
require("autoprefixer"),
],
};
tailind.config.js
const production = process.env.NODE_ENV === "production";
module.exports = {
mode: "jit",
future: {
purgeLayersByDefault: true,
removeDeprecatedGapUtilities: true,
},
purge: { content: ["./src/**/*.svelte"], enabled: production },
darkMode: false, // or 'media' or 'class'
theme: {
extend: {/*...*/},
},
variants: {
extend: {},
},
plugins: [],
};
Do you have any idea if it's a configuration problem or is something related to the editor?

My SASS variables into :root are not interpolated

I'm fairly new to the Nuxt ecosystem. Awesome package that makes our lives much easier.
I'm trying to add sass to my project. After following the documentation my build runs perfectly but my scss files are not being compiled. An example of the problem:
Notice that --thm-font is set to $primaryTypography and not the actual value from the .scss.
I'm expecting to see --thm-font: 'Barlow', sans-serif. I'm assuming that the sass is not being compiled. It is important to note I'm not looking for a component base style but I'm trying to have a main.scss where I will import the component, layouts and many other styles.
_variables.scss
// Base colors
$base: #ee464b;
$baseRgb: (238, 70, 75);
$black: #272839;
$blackRgb: (39, 40, 57);
$grey: #f4f4f8;
// Typography
$primaryTypography: 'Barlow', sans-serif;
#debug $primaryTypography; // -> this one outputs the correct value
:root {
--thm-font: $primaryTypography;
--thm-base: $base;
--thm-base-rgb: $baseRgb;
--thm-black: $black;
--thm-black-rgb: $blackRgb;
--thm-gray: $grey;
}
nuxt.config.js
export default {
mode: 'universal',
loading: { color: '#fff' },
css: [
'~assets/scss/main.scss'
],
plugins: [
],
buildModules: [
],
modules: [
],
optimizedImages: {
optimizeImages: true
},
build: {
extend (config, ctx) {
},
loaders: {
sass: {
prependData: '#import "~#/assets/scss/main.scss";'
}
}
},
server: {
port: process.env.APP_PORT
}
}
package.json
{
"name": "zimed",
"version": "1.1.0",
"description": "Zimed - Vue Nuxt App Landing Page Template",
"author": "Layerdrops",
"private": true,
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"generate": "nuxt generate"
},
"dependencies": {
"#bazzite/nuxt-optimized-images": "^0.3.0",
"nuxt": "^2.0.0",
"sass-loader": "10"
},
"devDependencies": {
"fibers": "^5.0.0",
"sass": "^1.38.2"
}
}
Which configuration am I missing so that .scss files get compiled?
You need to interpolate the variable like this --thm-font: #{$primaryTypography}; in the scope of :root.
Not sure the why of this behavior, but this answer was my way of finding this out.

Getting an error - " You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file"

I'm a beginner in React and stuck with some problem.Can't able to import the external css file in react app. And while importing I'm getting an error as " You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file". I have added a css loader and style loader in the webpack configbut still getting this error.
**Webpack.config.js**
var path = require('path')
var webpack = require('webpack')
var nodeExternals = require('webpack-node-externals')
var browserConfig = {
entry: './src/browser/index.js',
output: {
path: path.resolve(__dirname, 'public'), // create a public folder
// and inside this we'll create file named bundle.js which contain
// the whole app contained in one file
filename: 'bundle.js',
publicPath: '/'
},
module: {
rules: [
{ test: /\.(js)$/, use: 'babel-loader'}, // whenever webpack found
// any .js file then it will call babel-loader to convert JSX into
// vanilla js
{
test:/\.css$/,
use:['style-loader','css-loader']
}
]
},
plugins: [
new webpack.DefinePlugin({
__isBrowser__: "true" // plugin help to know whether we are in
// browser rendered page or server rendered page.
})
]
}
var serverConfig = {
entry: './src/server/index.js',
target: 'node',
externals: [nodeExternals()],
output: {
path: __dirname,
filename: 'server.js',
publicPath: '/'
},
module: {
rules: [
{ test: /\.(js)$/, use: 'babel-loader' },
{
test:/\.css$/,
use:['style-loader','css-loader']
}
]
},
plugins: [
new webpack.DefinePlugin({
__isBrowser__: "false"
})
]
}
module.exports = [browserConfig, serverConfig]
package.json
{
"name": "React-App",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev-server": "nodemon --exec babel-node src/server/server.js --ignore public/",
"dev-bundle": "webpack -w -d"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#babel/core": "^7.10.2",
"#babel/node": "^7.10.1",
"#babel/preset-react": "^7.10.1",
"babel-loader": "^8.1.0",
"#babel/preset-env": "^7.9.6",
"express": "^4.17.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-node-externals": "^1.7.2"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"cors": "^2.8.5",
"css-loader": "^3.5.3",
"eslint": "^7.2.0",
"eslint-plugin-react": "^7.20.0",
"eslint-plugin-react-hooks": "^4.0.4",
"nodemon": "^2.0.4",
"serialize-javascript": "^3.0.0",
"style-loader": "^1.2.1"
}
}
babel.config.js
module.exports = {
presets: ['#babel/preset-env', '#babel/preset-react'],
};
Home.js
import React from 'react';
import {Link} from 'react-router-dom';
import './../styles/NavBar.css';
export default function Home () {
const myStyle = {
"margin-left":"25%",
"padding":"1px 16px",
"height":"1000px"
};
return (
<>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
<div style={myStyle}>
<h2>Fixed Full-height Side Nav</h2>
</div>
</>
)
}
NavBar.css
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li a.active {
background-color: #4CAF50;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
}
Error
C:\React-App\src\styles\NavBar.css:1
ul {
^
SyntaxError: Unexpected token '{'
at wrapSafe (internal/modules/cjs/loader.js:1047:16)
at Module._compile (internal/modules/cjs/loader.js:1097:27)
at Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Object.newLoader [as .js] (C:\React-App\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\React-App\src\shared\/Home.js:3:1)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
Well your setup looks good , not sure about Home.js file. Haven't used it like that till now. May be its new way of writing.
Other vise try changing the file to Home.jsx and your webpack to
{ test: /\.(js|jsx)$/, use: 'babel-loader' }

Failing to load background-image with sass-loader (Webpack)

I'm having trouble trying to get the browser to successfully find a background image whilst using webpack and sass-loader/style-loader/css-loader.
The path seems to be right, because whenever I change it the compile process fails, but as it is, it is ok.
So far I have...
Component
import React from 'react'
const Image = () => (
<div className='sprite-container sprite-1'>
</div>
)
export default Image
CSS
.sprite-container {
width: 100px;
height: 100px;
border-radius: 100%;
border: 1px solid blue; // I put this just to confirm the container div is there, which it is.
background-image: url('/img/spritesheet.png');
background-repeat: no-repeat;
position: absolute;
top: 250px;
right: 20px;
}
.sprite-1 {
background-position: -100px, -100px;
}
As it is, the div is transparent. The container is there but the background image fails to load. I'm new to compiling SASS in Webpack, so maybe this is something to do with my file structure.
This is the relevant part of my file tree:
- src
- static (all static assets are served from this folder)
- img
-- spritesheet.png
- styles
-- app.scss
-- app-client.js (importing app.scss here)
I'm importing the app.scss into my main js file, app-client.js (which React mounts to the application).
Does the path given in the background-image css property need to be relative the root directory or the stylesheet? I'm assuming the root directory (/static).
Any help appreciated.
UPDATE
File tree
- src
- static (all static assets are served from this folder)
- img
-- spritesheet.png
- js
-- bundle.js
- styles
-- app.scss
-- app-client.js (importing app.scss here)
webpack.config.js
const debug = process.env.NODE_ENV !== "production";
const webpack = require('webpack');
const path = require('path');
// const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
devtool: debug ? 'inline-sourcemap' : null,
entry: path.join(__dirname, 'src', 'app-client.js'),
devServer: {
inline: true,
port: 3333,
contentBase: "src/static/",
historyApiFallback: {
index: '/index-static.html'
}
},
output: {
path: path.join(__dirname, 'src', 'static', 'js'),
publicPath: "/js/",
filename: 'bundle.js'
},
module: {
loaders: [
{
test: path.join(__dirname, 'src'),
loader: ['babel-loader'],
query: {
cacheDirectory: 'babel_cache',
presets: debug ? ['react', 'es2015', 'react-hmre'] : ['react', 'es2015']
}
},
{
test: /\.scss$/,
loaders: [ 'style', 'css?sourceMap', 'sass?sourceMap' ]
// loader: ExtractTextPlugin.extract(
// 'style', // The backup style loader
// 'css?sourceMap!sass?sourceMap'
// )
},
{
test: /\.png$/,
loader: "url-loader?limit=10000&minetype=image/jpg"
}
]
},
plugins: debug ? [] : [
// new ExtractTextPlugin('styles.css'),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
mangle: true,
sourcemap: false,
beautify: false,
dead_code: true
}),
]
};
package.json
{
"name": "***",
"version": "1.0.0",
"description": "***",
"main": "src/server.js",
"repository": "**REPO**",
"scripts": {
"start": "NODE_ENV=production node_modules/.bin/babel-node --presets 'react,es2015' src/server.js",
"start-dev": "npm run start-dev-hmr",
"start-dev-single-page": "node_modules/.bin/http-server src/static",
"start-dev-hmr": "node_modules/.bin/webpack-dev-server --progress --inline --hot",
"build": "NODE_ENV=production node_modules/.bin/webpack -p"
},
"author": "***",
"license": "UNLICENSED",
"dependencies": {
"axios": "^0.15.3",
"babel-cli": "^6.11.4",
"babel-core": "^6.13.2",
"babel-loader": "^6.2.5",
"babel-plugin-react-html-attrs": "^2.0.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-object-rest-spread": "^6.22.0",
"babel-preset-es2015": "^6.13.2",
"babel-preset-react": "^6.11.1",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-2": "^6.22.0",
"ejs": "^2.5.1",
"express": "^4.14.0",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"react-redux": "^5.0.2",
"react-router": "^2.6.1",
"redux": "^3.6.0",
"redux-logger": "^2.7.4",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"css-loader": "^0.26.1",
"file-loader": "^0.9.0",
"http-server": "^0.9.0",
"node-sass": "^4.3.0",
"react-hot-loader": "^1.3.0",
"sass-loader": "^4.1.1",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.14.1"
}
}
I ran into the same problem. I found that you can include ?url=false in your css-loader to disable url handling. Then you can just place an image in your public folder for css to access. The image won't be run through webpack, but it will get you past webpack's compile errors and still allow images to be accessed.
So here is my new object in the module.loaders array:
{
test: /\.s?css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?url=false', 'sass-loader']
})
}
Ok, I found the answer on another stack overflow question: Webpack - background images not loading
It turns out it was a bug, caused by the sourceMap of css-loader.
I changed this...
{
test: /\.scss$/,
loaders: [ 'style', 'css?sourceMap', 'sass?sourceMap' ]
}
...to this:
{
test: /\.scss$/,
loaders: [ 'style', 'css', 'sass?sourceMap' ]
}
Hope that helps anyone else who faces this problem as it wasted a good few hours for me!

Resources