We are building a React component for embedding code snippets onto a Docusaurus2 page. This component uses a local CSS file, which is a modified version of Prism CSS, to highlight syntax on the code snippets. This component is then published as a NPM package.
When importing our package to our Docusaurus2 project, we get the following error on server build : ReferenceError: document is not defined
After investigation, it seems like Docusaurus is not working with the local CSS file or any local CSS file.
If we import the Prism CSS directly into our component using import "prismjs/themes/prism.css"; instead of importing a local CSS file with import "./prism.css", there is no build issue.
What we tried :
We tried using the MiniCssExtractPlugin, but ended up with no more
highlight in the code snippets.
The temporary solution we have is to created a npm package that only
contains the css file and to import it into our React
component. There is no more build error in the project by doing this.
Here are examples of the files in our component :
webpack.config.js:
const path = require('path');
const nodeExternals = require("webpack-node-externals");
module.exports = {
mode: 'production',
entry: './src/index.tsx',
output: {
path: path.resolve('lib'),
filename: 'index.js',
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.(t|j)sx?$/,
exclude: /(node_modules)/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
]
},
externals: [nodeExternals()],
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx']
},
}
editor.tsx (prism.css is the css file discuted above):
import React from 'react';
import Prism from 'prismjs';
import { ReactCodeJar } from "react-codejar";
import "./prism.css";
import axios from 'axios';
import YAML from 'yaml';
let darkTheme = {
editorStyle: {
borderRadius: "25px 25px 25px 0",
boxShadow: "0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2)",
fontFamily: "'Source Code Pro', monospace",
fontSize: "14px",
fontWeight: "400",
height: "350px",
letterSpacing: "normal",
lineHeight: "20px",
padding: "20px",
tabSize: "4",
...
package.json :
{
"name": "ligosnippet",
"version": "1.0.0",
"main": "./lib/index.js",
"scripts": {
"build": "webpack"
},
"files": [
"lib"
],
"license": "ISC",
"peerDependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"devDependencies": {
"#babel/cli": "^7.8.4",
"#babel/core": "^7.8.7",
"#babel/plugin-transform-react-jsx": "^7.8.3",
"#babel/preset-env": "^7.8.7",
"#babel/preset-react": "^7.8.3",
"#babel/preset-typescript": "^7.8.3",
"#types/node": "^13.9.5",
"#types/react": "^16.9.23",
"#types/react-dom": "^16.9.5",
"#types/styled-components": "^5.0.1",
"babel-loader": "8.0.6",
"css-loader": "^3.4.2",
"parcel-bundler": "^1.12.4",
"path": "^0.12.7",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"style-loader": "^1.1.3",
"typescript": "^3.8.3",
"webpack": "4.41.5",
"webpack-cli": "^3.3.11",
"webpack-node-externals": "^1.7.2"
},
"dependencies": {
"axios": "^0.19.2",
"prismjs": "^1.20.0",
"react-codejar": "^1.0.1",
"yaml": "^1.9.2"
}
}
.babelrc:
{
"presets": [
"#babel/preset-react",
"#babel/preset-env",
"#babel/preset-typescript"
],
"plugins": ["#babel/plugin-transform-react-jsx"]
}
Related
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;
}
so I started my app and Im trying to use CSS for my React Components, I included my css rules in the webpack file:
This is the file structure:
The error that I get is :
ERROR in ./client/src/related_items/styles/Card.css 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> .Card-Component {
| border: 1px solid black;
| }
# ./client/src/related_items/Card.jsx 2:0-27
# ./client/src/related_items/App.jsx 2:0-26 8:163-167
# ./client/src/App.jsx 26:0-51 78:43-55
# ./client/src/index.js 3:0-28 4:50-53
webpack 5.52.0 compiled with 1 error in 13 ms
webpack.config.js
const path = require('path');
const SRC_DIR = path.join(__dirname, 'client', 'src');
const OUT_DIR = path.join(__dirname, 'client', 'dist');
module.exports = {
mode: 'development',
entry: path.join(SRC_DIR, 'index.js'),
output: {
path: OUT_DIR,
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
]
},
resolve: {
extensions: ['.js', '.jsx']
},
};
Im simply trying to use CSS in a component:
Card Component
import React from 'react';
import './styles/Card.css';
const Card = () => {
return (
<div className="Card-Component">
<h1>This is a Card</h1>
</div>
)
}
export default Card;
This is my CSS:
.Card-Component {
border: 1px solid black;
}
This is my package.json
{
"name": "front-end-capstone",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"react-dev": "webpack -w",
"server-dev": "nodemon server/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.15.5",
"#babel/preset-env": "^7.15.4",
"#babel/preset-react": "^7.14.5",
"axios": "^0.21.4",
"babel-loader": "^8.2.2",
"babel-preset-es2015": "^6.24.1",
"chai": "^4.3.4",
"css-loader": "^6.2.0",
"express": "^4.17.1",
"http-proxy-middleware": "^2.0.1",
"jest": "^27.1.1",
"mocha": "^9.1.1",
"nodemon": "^2.0.12",
"path": "^0.12.7",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"sass": "^1.39.0",
"sass-loader": "^12.1.0",
"style-loader": "^3.2.1",
"webpack": "^5.52.0",
"webpack-cli": "^4.8.0"
}
}
Just killed webpack and re run it again, thought that having in in watch mode would do it
I'm creating an app in react. Having webpack installed I'm trying to add some styling but it doesn't seem to work.
My app.js:
import React from 'react'
import ReactDOM from 'react-dom'
import IndecisionApp from './components/IndecisionApp'
import './styles/styles.css'
ReactDOM.render(<IndecisionApp />, document.getElementById('app'))
My webpack.config
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
style.css file
* {
color: red;
}
app.js and styles folder are in the same folder. Shouldn't this work?
Btw, I'm not getting any errors or anything.
EDIT
Error in terminal (new edit: fixed this error, but initial problem still not solved)
ERROR in ./src/styles/styles.css
Module parse failed: C:\Users\Jossif\Desktop\Projects\react-course-projects\indecision-app\r-08-04-refactor\src\styles\styles.css Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| * {
| color: red;
| }
# ./src/app.js 15:0-30
# multi (webpack)-dev-server/client?http://localhost:8081 ./src/app.js
My webpack.config file
const path = require('path')
module.exports = {
entry: './src/app.js',
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
module: {
rules: [
{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
devtool: 'cheap-module-eval-source-map',
devServer: {
contentBase: path.join(__dirname, 'public')
}
}
My package.json file
{
"name": "indecision-app",
"version": "1.0.0",
"main": "index.js",
"author": "Andrew Mead",
"license": "MIT",
"scripts": {
"serve": "live-server public/",
"build": "webpack",
"dev-server": "webpack-dev-server"
},
"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-preset-env": "1.5.2",
"babel-preset-react": "6.24.1",
"css-loader": "0.28.4",
"live-server": "^1.2.0",
"react": "15.6.1",
"react-dom": "15.6.1",
"react-modal": "2.2.2",
"style-loader": "0.18.2",
"validator": "8.0.0",
"webpack": "3.1.0",
"webpack-dev-server": "2.5.1"
}
}
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!
I'm helping create a webapp and we've recently switched to stylus and react. I've rewritten all the stylus lang files and written some react but the react is not being styled at all. Here is the component below.
import React, { Component } from 'react'
import Icon from 'react-component-bytesize-icons'
class CoursesCard extends Component {
render() {
const divStyle = {
backgroundImage : '/assets/img/animatingCourse.jpg'
}
return (
<div className='CoursesCard' style={divStyle}>
<div className='CardOverlay'>
<h3>What the courses is called?</h3>
<p>Last Checed</p>
<p>Users online</p>
</div>
</div>
)
}
}
export default CoursesCard
Here is the stylus lang code:
.CoursesCard {
width 32%
margin 10px 1%
float left
padding-top 20%
display inline-block
background white
box-shadow 0px 3px 20px #555
background-repeat no-repeat
background-size cover
cursor pointer
&.hover{
box-shadow 0px 3px 22px #333
}
.CardOverlay {
width 100%
height auto
padding 10px 0
background rgba(33,51,66,0.8)
color #fff
background-repeat no-repeat
background-size cover
cursor pointer
&.hover {
box-shadow 0px 3px 22px #333
}
h3{
font-size 18px
font-weight 500
margin-bottom 5px
margin-left 10px
}
p.lastChecked{
font-size 13px
font-weight 100
margin-bottom 5px
margin-left 10px
}
p.onlineUsers{
font-size 14px
font-weight 400
margin-left 10px
display block
}
}
}
Using Stylus in React requires a bundler like Webpack. I use Stylus + React a lot, so I can help but your question doesn't tell me if you have Webpack and npm or yarn set up.
Since I don't see your full build, I can share my build and maybe that will help. This is the closest I can get to helping you step-by-step without knowing more about your current build. Hope this helps!
Assuming you have Webpack and npm set up and your project has a package.json file already...
Run these commands in terminal...
npm install yarn
yarn init
yarn add poststylus autoprefixer-stylus autoprefixer
css-loader poststylus sass-loader style-loader stylus-loader
Make your webpack.config.js similar to this...
const path = require('path');
const postStylus = require('poststylus');
const webpack = require('webpack');
module.exports = {
context: __dirname,
entry: [
'./source/ClientApp.jsx',
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:8080/',
'webpack/hot/only-dev-server',
],
devtool: 'cheap-eval-source-map',
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js',
publicPath: '/public/',
hotUpdateChunkFilename: 'hot/hot-update.js',
hotUpdateMainFilename: 'hot/hot-update.json'
},
devServer: {
hot: true,
publicPath: '/public/',
historyApiFallback: true,
},
watch: true,
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
// new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.LoaderOptionsPlugin({
options: {
stylus: {
preferPathResolver: 'webpack',
},
}
}),
],
resolve: {
extensions: ['.js', '.jsx', '.json', '.styl'],
modules: [
path.resolve('./source'),
path.resolve('./node_modules'),
],
},
stats: {
colors: true,
reasons: true,
chunks: true,
},
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
loader: 'eslint-loader',
exclude: /node_modules/,
},
{
test: /\.jsx?$/,
loader: 'babel-loader',
},
{
test: /\.styl$/,
use: [
'style-loader',
'css-loader',
{
loader: 'stylus-loader',
options: {
use: [
postStylus(['autoprefixer']),
],
modules: true,
},
},
],
exclude: /node_modules/,
},
],
},
};
And here is my entire package.json if you need it for reference...
{
"scripts": {
"lint": "eslint **/*.{js,jsx} --quiet",
"clean": "rm -r public/bundle.js",
"build": "rm -r public/bundle.js && webpack",
"dev": "webpack-dev-server --watch --progress --colors",
"watch": "webpack --watch"
},
"dependencies": {
"autoprefixer": "^7.1.2",
"autoprefixer-stylus": "^0.14.0",
"axios": "^0.16.2",
"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-dynamic-import-webpack": "^1.0.1",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
"babel-preset-airbnb": "^2.4.0",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.24.1",
"concurrently": "^3.5.0",
"css-loader": "^0.28.4",
"d3": "^4.10.0",
"enzyme": "^2.9.1",
"eslint": "3.19.0",
"eslint-config-airbnb": "^15.0.2",
"eslint-config-airbnb-base": "^11.2.0",
"eslint-config-react": "^1.1.7",
"eslint-loader": "^1.9.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^5.0.3",
"eslint-plugin-react": "^7.1.0",
"express": "^4.15.3",
"extract-text-webpack-plugin": "^3.0.0",
"firebase": "^4.2.0",
"firebase-tools": "^3.9.2",
"json-loader": "^0.5.7",
"node-sass": "^4.5.3",
"nodemon": "^1.11.0",
"poststylus": "^0.2.3",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-date-range": "^0.9.4",
"react-dom": "^15.6.1",
"react-hot-loader": "3.0.0-beta.6",
"react-router-dom": "^4.1.2",
"react-test-renderer": "^15.6.1",
"resolve-url-loader": "^2.1.0",
"sass-loader": "^6.0.6",
"sort-package-json": "^1.7.0",
"style-loader": "^0.18.2",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.1",
"webpack": "2.6.1",
"webpack-combine-loaders": "^2.0.3",
"webpack-dev-middleware": "^1.11.0",
"webpack-dev-server": "^2.6.1",
"webpack-hot-middleware": "^2.18.2"
},
}
React has nothing to do with Stylus at all.
I suppose you are using Webpack as bundler of choice. If so, you have to configure Webpack so that it understands what to do with .styl files (or whatever file extension is used).
Webpack has concept of loaders - loader loads file, transforms it in some way and pushes output down the chain.
You need to add stylus loader. To do so, please take a look at this plugin's GH page