sass-loader doesn't generate css - css

I tried to update this webpack config to generate css from scss. I added styles.scss into src folder with one css rule:
body{
font-size: 38px;
}
If I run yarn build or yarn dev, webpack doesn't generate any css files and I don't get any error.
webpack.config.js
/* global __dirname, require, module*/
const webpack = require('webpack');
const path = require('path');
const env = require('yargs').argv.env; // use --env with webpack 2
const pkg = require('./package.json');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
let libraryName = pkg.name;
let outputFile, mode;
if (env === 'build') {
mode = 'production';
outputFile = libraryName + '.min.js';
} else {
mode = 'development';
outputFile = libraryName + '.js';
}
const config = {
mode: mode,
entry: __dirname + '/src/index.js',
devtool: 'inline-source-map',
output: {
path: __dirname + '/lib',
filename: outputFile,
library: libraryName,
libraryTarget: 'umd',
umdNamedDefine: true,
globalObject: "typeof self !== 'undefined' ? self : this"
},
module: {
rules: [
{
test: /(\.jsx|\.js)$/,
loader: 'babel-loader',
exclude: /(node_modules|bower_components)/
},
{
test: /(\.jsx|\.js)$/,
loader: 'eslint-loader',
exclude: /node_modules/
},
{
test: /\.scss$/,
use: [
// fallback to style-loader in development
process.env.NODE_ENV !== 'production' ? 'style-loader' : MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}
]
},
resolve: {
modules: [path.resolve('./node_modules'), path.resolve('./src')],
extensions: ['.json', '.js', '.scss']
},
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "[name].css",
chunkFilename: "[id].css"
})
]
};
module.exports = config;
package.json
{
"name": "webpack-library-starter",
"version": "1.0.2",
"description": "Produce universal library with webpack and es6",
"main": "lib/webpack-library-starter.js",
"scripts": {
"build": "webpack --env dev && webpack --env build && npm run test",
"dev": "webpack --progress --colors --watch --env dev",
"test": "mocha --require babel-register --colors ./test/*.spec.js",
"test:watch": "mocha --require babel-register --colors -w ./test/*.spec.js",
"test:cover": "cross-env NODE_ENV=test nyc mocha --require babel-register --colors test/*.js",
"repl": "node -i -e \"$(< ./lib/webpack-library-starter.js)\""
},
"repository": {
"type": "git",
"url": "https://github.com/krasimir/webpack-library-starter.git"
},
"keywords": [
"webpack",
"es6",
"starter",
"library",
"universal",
"umd",
"commonjs"
],
"author": "Krasimir Tsonev",
"license": "MIT",
"bugs": {
"url": "https://github.com/krasimir/webpack-library-starter/issues"
},
"homepage": "https://github.com/krasimir/webpack-library-starter",
"devDependencies": {
"#babel/cli": "^7.0.0-beta.51",
"#babel/core": "^7.0.0-beta.51",
"#babel/preset-env": "^7.0.0-beta.51",
"babel-eslint": "^8.0.3",
"babel-loader": "^8.0.0-beta.4",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-istanbul": "^5.1.0",
"babel-preset-env": "^7.0.0-beta.3",
"babel-register": "^7.0.0-beta.3",
"chai": "^4.1.2",
"cross-env": "^5.2.0",
"eslint": "^5.0.1",
"eslint-loader": "^2.0.0",
"jsdom": "11.11.0",
"jsdom-global": "3.0.2",
"mocha": "^4.0.1",
"nyc": "^13.1.0",
"uglifyjs-webpack-plugin": "^1.2.7",
"webpack": "^4.12.2",
"webpack-cli": "^3.0.8",
"yargs": "^10.0.3"
},
"nyc": {
"sourceMap": false,
"instrument": false
},
"dependencies": {
"css-loader": "^2.1.1",
"mini-css-extract-plugin": "^0.7.0",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1"
}
}

Check, if you imported scss file in entry js file, for example index.js.

Related

NodeJS webpack configuration error for sass and css

I recently updated my NodeJS version to 'v16.13.2' and ever since then my template I built is breaking when trying to bundle the scss and css. Everything else works just fine when building.
I'm aware that node-sass had been deprecated and I should use sass (dart-sass). However, when I run my build I get this error:
Module parse failed: C:\Users\...\src\styles\styles.scss Unexpected token (1:3)
You may need an appropriate loader to handle this file type.
| h1 {
| color: white;
| text-align: center;
# ./src/app.js 25:0-31
# multi (webpack)-dev-server/client?http://localhost:8080 babel-polyfill ./src/app.js
I'm trying to get basic css to work but the loader doesn't seem to recognize the code.
Here is my code
package.json:
"scripts": {
"build:dev": "webpack",
"build:prod": "webpack -p --env production",
"dev-server": "webpack-dev-server",
"test": "cross-env NODE_ENV=test jest --config=jest.config.json",
"start": "node server/server.js",
"heroku-postbuild": "yarn run build:prod"
},
"dependencies": {
"babel-cli": "6.24.1",
"babel-core": "6.25.0",
"babel-loader": "7.1.1",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-object-rest-spread": "6.23.0",
"babel-polyfill": "6.26.0",
"babel-preset-env": "1.5.2",
"babel-preset-react": "6.24.1",
"css-loader": "^6.5.1",
"express": "4.15.4",
"extract-text-webpack-plugin": "3.0.0",
"firebase": "^8.9.1",
"history": "4.10.1",
"moment": "2.18.1",
"normalize.css": "7.0.0",
"numeral": "2.0.6",
"react": "^16.14.0",
"react-addons-shallow-compare": "15.6.0",
"react-dates": "12.7.0",
"react-dom": "^16.14.0",
"react-modal": "2.2.2",
"react-redux": "5.0.5",
"react-router-dom": "4.1.2",
"redux": "3.7.2",
"redux-mock-store": "1.2.3",
"redux-thunk": "2.2.0",
"sass": "^1.49.7",
"sass-loader": "7.3.1",
"style-loader": "0.18.2",
"uuid": "3.1.0",
"validator": "8.0.0",
"webpack": "3.1.0"
},
"devDependencies": {
"cross-env": "5.0.5",
"dotenv": "^14.2.0",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.6",
"enzyme-to-json": "^3.6.1",
"jest": "20.0.4",
"raf": "^3.4.1",
"react-test-renderer": "^16.14.0",
"webpack-dev-server": "2.5.1"
}
webpack.cnfig.js:
return{
entry: ['babel-polyfill','./src/app.js'],
output: {
path: path.join(__dirname, 'public', 'dist'),
filename: 'bundle.js'
},
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
},
{
test: /\.s?css$/,
use: CSSExtract.extract({
use: [
{
loader: 'css-loader',
options:{
sourceMap: true
}
},
{
loader: 'sass-loader',
options:{
sourceMap: true
}
}
]
})
}
]
},
EDIT: So after much research, I have also figured out that 'extract-text-webpack-plugin' is also deprecated and the documentation suggests that I use 'mini-css-extract-plugin'. So, I read the documentation for that and applied it but still nothing is working. I just want my webpack to bundle my .js and .css in separate files . Right now all of it is being pushed into 'bundle.js' and it does not render any css.
EDIT 2: Here is my new webpack.config.js and it is working as I wanted:
webpack.config.js:
webpack.config.js:
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
if(process.env.NODE_ENV === 'test'){
require('dotenv').config({ path: '.env.test'})
}else if(process.env.NODE_ENV === 'development'){
require('dotenv').config({ path: '.env.development'})
}
module.exports = (env) => {
const isProd = env === 'production'
return{
entry: ['babel-polyfill','./src/app.js'],
output: {
path: path.join(__dirname, 'public', 'dist'),
filename: 'bundle.js'
},
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
},
{
test: /\.(css)$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
},
{
test: /\.(s[ca]ss)$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
}
]
},
plugins:[
new MiniCssExtractPlugin({
filename:'styles.css'
}),
new webpack.DefinePlugin({
'process.env.FIREBASE_API_KEY': JSON.stringify(process.env.FIREBASE_API_KEY),
'process.env.FIREBASE_AUTH_DOMAIN': JSON.stringify(process.env.FIREBASE_AUTH_DOMAIN),
'process.env.FIREBASE_DATABASE_URL': JSON.stringify(process.env.FIREBASE_DATABASE_URL),
'process.env.FIREBASE_PROJECT_ID': JSON.stringify(process.env.FIREBASE_PROJECT_ID),
'process.env.FIREBASE_STORAGE_BUCKET': JSON.stringify(process.env.FIREBASE_STORAGE_BUCKET),
'process.env.FIREBASE_MESSAGE_SENDER_ID': JSON.stringify(process.env.FIREBASE_MESSAGE_SENDER_ID),
'process.env.FIREBASE_APP_ID': JSON.stringify(process.env.FIREBASE_APP_ID)
})
],
devtool: isProd ? 'source-map' :'inline-source-map',
devServer: {
contentBase: path.join(__dirname, 'public'),
historyApiFallback: true,
publicPath: '/dist'
}
}
}
Alright I finally have an answer for all of this. So I essentially had to update a lot of my webpack due to deprecated modules. I ended up using 'mini-css-extract-plugin' and found out how to set it up. If anyone is working on a project using Nodejs version 16 and up, try this for the webpack and see if it works. I'll post my results in the initial question.

Modifying localIdentName in css-loader doesn't work in webpack 5 react 17

Currently I am using webpack 5, react 17 and #dr.pogodin/babel-plugin-react-css-modules and all other latest packages.
I am excluding the sass/css files in assets/stylesheets which is being treated as global and using those classes inside className
Styles won't be applied changing localIdentName to something else. Tried getLocalIdent but no use.
Github Repo Link
So how to change the localIdentName ?
package.json
{
"name": "react-app",
"version": "1.0.0",
"description": "React Template App",
"author": "",
"license": "ISC",
"main": "index.js",
"scripts": {
"start": "webpack serve --mode development",
"build": "webpack --mode production"
},
"dependencies": {
"date-fns": "^2.16.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
},
"devDependencies": {
"#babel/core": "^7.12.10",
"#babel/preset-env": "^7.12.11",
"#babel/preset-react": "^7.12.10",
"#dr.pogodin/babel-plugin-react-css-modules": "^6.0.10",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.2.2",
"css-loader": "^5.0.1",
"html-webpack-plugin": "^5.0.0-beta.1",
"mini-css-extract-plugin": "^1.3.3",
"node-sass": "^5.0.0",
"postcss": "^8.2.1",
"postcss-scss": "^3.0.4",
"sass": "^1.30.0",
"sass-loader": "^10.1.0",
"style-loader": "^2.0.0",
"url-loader": "^4.1.1",
"webpack": "^5.11.0",
"webpack-cli": "^4.3.0",
"webpack-dev-server": "^3.11.0"
}
}
webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const isDev = process.env.NODE_ENV === "development";
console.log(Environment: ${isDev ? "development" : "production"});
module.exports = {
entry: "./index.js",
mode: isDev ? "development" : "production",
output: {
path: path.join(__dirname, "dist"),
publicPath: "/",
filename: "bundle.js",
},
devServer: {
compress: true,
open: true,
hot: true,
historyApiFallback: true,
quiet: false,
stats: "errors-warnings",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
{
test: /\.(css|sass|scss|less)$/,
use: [
isDev ? "style-loader" : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: {
auto: (resourcePath) =>
resourcePath.indexOf("assets/stylesheets") === -1,
localIdentName: "[path]___[name]__[local]___[hash:base64:5]",
// getLocalIdent: (context, localIdentName, localName, options) => {
// return "whatever_random_class_name";
// },
},
sourceMap: isDev,
},
},
"sass-loader"
],
}
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./public/index.html",
filename: "./index.html",
favicon: "./public/favicon.ico",
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css",
})
],
devtool: isDev ? "source-map" : false,
};
.babelrc
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
[
"#dr.pogodin/babel-plugin-react-css-modules",
{
"webpackHotModuleReloading": true,
"autoResolveMultipleImports": true,
"filetypes": {
".scss": {
"syntax": "postcss-scss"
}
}
}
]
]
}
Sounds like I forgot to tell you babel-plugin-react-css-modules has an option to configure the scoped name too which is called generateScopedName.
As long as you configured this as same as css-loader, it should work then:
.babelrc
[
"#dr.pogodin/babel-plugin-react-css-modules",
{
"generateScopedName": "[name]__[local]___[hash:base64:5]", // switch to whatever you want to
// ...
}
]
webpack.config.js:
{
loader: "css-loader",
options: {
modules: {
// ...
localIdentName: "[name]__[local]___[hash:base64:5]",
},
},
},
NOTE: In case of generating based on env, you should use js babel config file babel.config.js so you can conditionally generate the name via NODE_ENV

how to solve errors when running webpack?

webpack problem became an error because of the typecript (ts and tsx) I changed it to javascript (js and jsx). the original code is the typescript I took from Github
Image webpack error
file webpack.config.js
var webpack = require("webpack");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var releaseConfig = require("./webpack.config.release");
var isProductionEnvironment =
process.env.ASPNETCORE_ENVIRONMENT === "Production";
var path = require("path");
var merge = require("extendify")({ isDeep: true, arrays: "replace" });
var config = {
mode: "development",
entry: {
main: path.join(__dirname, "boot.jsx")
},
output: {
path: path.join(__dirname, "../api/", "wwwroot"),
filename: "[name].js",
publicPath: "/"
},
resolve: {
extensions: [".ts", ".tsx", ".jsx", ".js", ".styl", ".css"]
},
module: {
rules: [
{
test: /\.styl$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
modules: true,
camelCase: true,
importLoaders: 2,
sourceMap: false,
localIdentName: "[local]___[hash:base64:5]"
}
},
{
loader: "stylus-loader"
}
]
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: "babel-loader"
},
{ test: /\.css/, loader: "style-loader!css-loader" },
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: "url-loader?limit=100000"
}
]
},
devtool: "inline-source-map",
plugins: [
// plugins should not be empty: https://github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.SpaServices'[
new HtmlWebpackPlugin({
template: path.join(__dirname, "index.ejs"),
inject: true
})
// new webpack.NamedModulesPlugin()
// We do not use ExtractTextPlugin in development mode so that HMR will work with styles
]
};
if (isProductionEnvironment) {
// Merge production config
config = merge(config, releaseConfig);
}
module.exports = config;
and file webpack.config.relase.js
var webpack = require("webpack");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var path = require("path");
var config = {
mode: "production",
module: {
rules: [
// Use react-hot for HMR and then ts-loader to transpile TS (pass path to tsconfig because it is not in root (cwd) path)
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.styl$/,
loader: ExtractTextPlugin.extract({
fallback: "style-loader",
use:
"css-loader?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]!stylus-loader"
})
},
{
test: /\.css/,
loader: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
}
]
},
devtool: "",
externals: {
react: "React",
"react-dom": "ReactDOM"
},
plugins: [
new HtmlWebpackPlugin({
release: true,
template: path.join(__dirname, "index.ejs"),
useCdn: true,
minify: {
collapseWhitespace: true,
removeComments: true
}
}),
new ExtractTextPlugin("styles.css")
]
};
file package.json
{
"name": "aspnet.core.react.template",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"postinstall": "dotnet restore ./api && dotnet restore ./api.test",
"build": "dotnet build ./api",
"test:api": "cd ./api.test && dotnet test",
"pretest:client": "npx tsc -p ./client-react.test/",
"test:client": "mocha --require ignore-styles --recursive client-react.test/build/client-react.test",
"test": "npm run test:api && npm run test:client",
"migrate": "cd ./api/ && node ../scripts/create-migration.js && dotnet ef database update",
"prestart": "docker-compose up -d",
"start": "cd ./api && cross-env NODE_PATH=../node_modules/ ASPNETCORE_ENVIRONMENT=Development dotnet watch run",
"start:release": "npm run prerelease && cd ./api/bin/Release/netcoreapp2.1/publish/ && dotnet api.dll",
"provision:prod": "ansible-playbook -l production -i ./ops/config.yml ./ops/provision.yml",
"prerelease": "cross-env ASPNETCORE_ENVIRONMENT=Production webpack --config ./client-react/webpack.config.js && cd ./api && dotnet publish --configuration Release",
"deploy:prod": "npm run prerelease && ansible-playbook -l production -i ./ops/config.yml ./ops/deploy.yml",
"ssh:prod": "ssh `grep 'deploy_user=' ./ops/hosts | tail -n1 | awk -F'=' '{ print $2}'`#`awk 'f{print;f=0} /[production]/{f=1}' ./ops/hosts | head -n 1`"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.6",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"webpack": "^4.41.2"
},
"dependencies": {
"#babel/core": "^7.7.2",
"#types/chai": "^4.1.7",
"#types/enzyme": "^3.9.1",
"#types/enzyme-adapter-react-16": "^1.0.5",
"#types/jsdom": "^12.2.3",
"#types/mocha": "^5.2.6",
"#types/react": "^16.8.10",
"#types/react-addons-test-utils": "^0.14.24",
"#types/react-dom": "^16.8.3",
"#types/react-router-dom": "^4.3.1",
"#types/sinon": "7.0.10",
"aspnet-webpack": "^3.0.0",
"aspnet-webpack-react": "^4.0.0",
"bootstrap": "4.3.1",
"chai": "^4.2.0",
"cross-env": "^5.2.0",
"css-loader": "^2.1.1",
"dom": "^0.0.3",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.11.2",
"extendify": "^1.0.0",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^3.0.1",
"global": "^4.3.2",
"html-webpack-plugin": "^3.2.0",
"ignore-styles": "^5.0.1",
"jsdom": "^14.0.0",
"mocha": "^6.0.2",
"prop-types": "^15.7.2",
"react": "^16.8.5",
"react-addons-test-utils": "^15.6.2",
"react-dom": "^16.8.5",
"react-hot-loader": "^4.8.0",
"react-router": "^5.0.0",
"react-router-dom": "^5.0.0",
"sinon": "7.3.1",
"source-map-loader": "^0.2.4",
"style-loader": "^0.23.1",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
"ts-loader": "^5.3.3",
"typescript": "^3.3.4000",
"url-loader": "^1.1.2",
"webpack-dev-middleware": "^3.6.1",
"webpack-hot-middleware": "^2.24.3",
"whatwg-fetch": "^3.0.0"
}
}
how to resolve the error, I beg for your help ... because I don't know much about reactjs with .net core?

Webpack --watch doesn't trigger for CSS files in specific directory

Using webpack --watch, changes to .pcss (PostCSS) files are not picked up when within [src/components/Main/]. Changes to .js files are picked up fine as well as .pcss files in other directories. Because my web app is isomorphic, ExtractTextPlugin is used to squish all the CSS together and push it into a single file.
Full code on GitHub.
This is on macOS 10.12.X.
webpack.config.js:
const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const babelPresetEnvExclude = require('./config/babel-preset-env.exclude')
const babelPluginRelay = ['relay', { schema: 'data/schema.graphqls', }]
const styleRules = {
test: /\.p?css$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: { importLoaders: 1 },
},
'postcss-loader',
],
}),
}
const fileRules = {
test: /\.((pn|sv|jpe?)g|gif)$/,
use: ['file-loader'],
}
const server = {
target: 'node',
entry: './build/unbundled/server.js',
output: {
filename: 'server.js',
path: path.resolve(__dirname, 'build')
},
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: [{
loader: 'babel-loader',
options: {
plugins: [babelPluginRelay],
},
}],
},
styleRules,
fileRules,
]
},
devtool: 'source-map',
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
}),
// Overwrites the same file created by the browser webpack config. A loader
// needs to be specified to take care of the import statements and it wont
// work without also outputting a file. There has to be a better way to
// handle this, but I want to focus on other parts for now.
// #todo: make this less bad.
new ExtractTextPlugin('public/main.css'),
]
}
const browser = {
target: 'web',
entry: './build/unbundled/browser.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'build/public')
},
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: [{
loader: 'babel-loader',
options: {
presets: [
['env', {
debug: true,
useBuiltIns: true,
targets: { browsers: ['last 2 versions'] },
exclude: babelPresetEnvExclude
}]
],
plugins: [babelPluginRelay],
},
}],
},
styleRules,
fileRules,
]
},
devtool: 'source-map',
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
}),
new ExtractTextPlugin('main.css'),
]
}
console.log('NODE_ENV', JSON.stringify(process.env.NODE_ENV || 'development'))
module.exports = [browser, server]
package.json:
{
"name": "rtm-owl",
"version": "1.0.0",
"main": "index.js",
"author": "boring#example.com",
"license": "MIT",
"scripts": {
"relay": "relay-compiler --src ./build/unbundled --schema data/schema.graphqls",
"build": "tsc --pretty && npm run relay && webpack --progress",
"debug": "npm run build && node --inspect build/server.js",
"debug-brk": "npm run build && node --inspect-brk build/server.js",
"start": "node build/server.js",
"watch": "concurrently --kill-others 'tsc --pretty --watch' 'relay-compiler --src ./build/unbundled --schema data/schema.graphqls --watch' 'webpack --watch' 'nodemon build/server.js'"
},
"devDependencies": {
"#types/chart.js": "^2.6.1",
"#types/debug": "^0.0.30",
"#types/express": "^4.0.36",
"#types/fs-extra": "^4.0.0",
"#types/isomorphic-fetch": "^0.0.34",
"#types/lodash": "^4.14.71",
"#types/morgan": "^1.7.32",
"#types/react": "^16.0.0",
"#types/react-chartjs-2": "^2.0.2",
"#types/react-dom": "^15.5.1",
"#types/react-redux": "^4.4.47",
"#types/serialize-javascript": "^1.3.1",
"autoprefixer": "^7.1.2",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-plugin-relay": "^1.1.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.0",
"concurrently": "^3.5.0",
"css-loader": "^0.28.4",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^0.11.2",
"fs-extra": "^4.0.0",
"isomorphic-fetch": "^2.2.1",
"nodemon": "^1.11.0",
"postcss-css-variables": "^0.7.0",
"postcss-import": "^10.0.0",
"postcss-loader": "^2.0.6",
"postcss-nested": "^2.1.0",
"relay-compiler": "^1.1.0",
"relay-runtime": "^1.1.0",
"serialize-javascript": "^1.3.0",
"style-loader": "^0.18.2",
"typescript": "^2.4.1",
"webpack": "^3.0.0"
},
"dependencies": {
"chart.js": "^2.6.0",
"debug": "^2.6.8",
"express": "^4.15.3",
"farce": "^0.2.1",
"found": "^0.3.1",
"found-relay": "^0.3.0-alpha.4",
"lodash": "^4.17.4",
"morgan": "^1.8.2",
"react": "^15.6.1",
"react-chartjs-2": "^2.5.5",
"react-dom": "^15.6.1",
"react-redux": "^5.0.5",
"react-relay": "^1.0.0",
"redux": "^3.7.2"
}
}
I encountered similar behaviour, webpack --watch does not react to changes in css files on macOS 10.14. I used the basic style-loader and css-loader and require my css files like require('./style.css').
Solved by switching to nodemon. In my package.json the following setup runs webpack whenever js or css files become modified.
...
scripts: {
"build": "webpack",
"watchbuild": "nodemon --watch ./ --ext js,css --ignore dist --exec \"npm run build\"",
...
},
devDependencies: {
"nodemon": "^2.0.4",
"webpack": "^4.39.3",
...
}
...
The setup can be easily customized to watch more file types and to execute a series of commands. For example nodemon --watch ./ --ext js,ejs,css,pcss --ignore dist --exec 'npm run lint && npm run build' will also watch ejs templates and pcss styles and run linter before build.
Note that I had to ignore my build directory dist to prevent infinite build loop. Note also \" instead of ' to provide compatibility between macOS and Windows.

Configuration for enabling hot style-loader in Webpack for syncing css

I'm trying to enable hot style-loader in Webpack but I can not find the right configuration for it. Here's my webpack.config.js:
const webpack = require('webpack');
const path = require('path');
const buildPath = path.resolve(__dirname, 'build');
const nodeModulesPath = path.resolve(__dirname, 'node_modules');
const TransferWebpackPlugin = require('transfer-webpack-plugin');
const config = {
entry: [
'webpack/hot/dev-server',
'webpack/hot/only-dev-server',
path.join(__dirname, '/src/app/app.js'),
],
resolve: {
extensions: ["", ".js"],
},
devServer: {
contentBase: 'src/www',
devtool: 'eval',
hot: true,
inline: true,
port: 3232,
host: '0.0.0.0',
},
devtool: 'eval',
output: {
path: buildPath,
filename: 'app.js',
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new TransferWebpackPlugin(
[{ from: 'www' }],
path.resolve(__dirname, "src")
),
],
module: {
loaders: [
{
test: /\.js$/,
loaders: ['react-hot', 'babel-loader'],
exclude: [nodeModulesPath],
},
{
test: /\.css$/,
loader: "style!css",
},
],
},
eslint: {
configFile: '.eslintrc',
},
};
module.exports = config;
And my package.js file:
{
"name": "test-material-ui",
"version": "0.0.0-beta.1",
"description": "Sample project that uses material-ui",
"scripts": {
"start": "webpack-dev-server --config webpack.config.js --progress --inline --colors",
"build": "webpack -p --define process.env.NODE_ENV='\"production\"' --config webpack-production.config.js --progress --colors",
"lint": "eslint src && echo \"eslint: no lint errors\""
},
"private": true,
"devDependencies": {
"babel-core": "^6.3.26",
"babel-eslint": "^6.0.0",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"copy-webpack-plugin": "^2.1.3",
"css-loader": "^0.23.1",
"eslint": "^2.5.1",
"eslint-plugin-react": "^4.0.0",
"html-webpack-plugin": "^2.7.2",
"react-hot-loader": "^1.3.0",
"style-loader": "^0.13.1",
"transfer-webpack-plugin": "^0.1.4",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"flexboxgrid": "^6.3.0",
"material-ui": "^0.15.0-beta.1",
"react": "^15.0.1",
"react-addons-css-transition-group": "^15.0.1",
"react-dom": "^15.0.1",
"react-redux": "^4.4.5",
"react-tap-event-plugin": "^1.0.0",
"redux": "^3.4.0"
}
}
But no matter how I configure it, I can not get the hot sync working (for .css, for .js files it works just fine)! Can someone please show me the right way to do so?
Make sure that you require the css in Javascript.
Here's a link to the documentation that explains it: https://webpack.github.io/docs/stylesheets.html

Resources