Using .scss files as modules without naming them with .modules create-react-app - css

I created a new app using create-react-app, and added scss to the project (simply ran npm install node-sass)
Now, when I want to use a .scss as a module, I have to name the file x.module.scss.
How can make every .scss file to automatically considered a module?
desired result example:
import styles from './test.scss'
.
.
.
currently I have to do this:
import styles from './test.module.scss'
.
.
.
Thanks!

run -> react-scripts eject
GOTO -> /config/webpack.config.dev.js

I just created a create-react-app application and added node-sass. You can see the versions below:
package.json
{
"name": "test-sass",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"node-sass": "^4.13.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I changed the .css files to .scss files without the module keyword, and it works fine like a charm.
I guess maybe you have check the versions or other configuration. The eject is not necessary here. it is a build-in feature.

Related

How to set dev and prod targets in Parcel

Please can anyone shed some light on how "Targets" work in Parcel, or at least how to separate unminified dev and minified prod files. The sparse documentation just raises further questions and no amount of Googling will find a solution. I am new to Parcel and to the modular nature of newer Javascript syntax.
This all started when I noticed that parcel watch does not minify/uglify the JS, and I don't want that to end up on the prod server. But parcel build WILL minify/uglify the JS. So it would be really swell if my start script could bundle files into build/wp-content/themes/yourproject/assets--dev and the build script could bundle files into build/wp-content/themes/yourproject/assets. That way I can put assets-dev in .gitignore and use those while building the site. Or is that even the proper way to acheive this? In Gulp you can just define different tasks or create one task to do both of these outputs, but I cannot find a way to do this in Parcel.
Description of the project - It's a WordPress site so I can't necessarily feed Parcel source html files to scan. Parcel is really just bundling JS and SCSS files, we are replacing our old Gulp setup. So in package.json, I have this (I'll explain what I have tried in "scripts" and in "targets" further down):
{
"name": "localtesting2",
"version": "1.0.0",
"description": "## Overview",
"browserslist": "> 0.5%, last 2 versions, not dead",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "parcel watch develop/js/index.js develop/styles/style--critical.scss develop/styles/style--noncritical.scss --dist-dir ./build/wp-content/themes/yourproject/assets--dev",
"build": "parcel build develop/js/index.js develop/styles/style--critical.scss develop/styles/style--noncritical.scss --dist-dir ./build/wp-content/themes/yourproject/assets"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"smoothscroll-polyfill": "^0.4.4"
},
"devDependencies": {
"#parcel/transformer-sass": "^2.3.2",
"autoprefixer": "^10.4.2",
"parcel": "latest",
"parcel-bundler": "^1.12.5",
"parcel-resolver-ignore": "^2.0.0",
"postcss": "^8.4.6",
"postcss-modules": "^4.3.0",
"sass": "^1.49.8",
"webfontloader": "^1.6.28"
},
"targets": {
"default": {
"distDir": "build/wp-content/themes/yourproject/assets--dev"
},
"build": {
"distDir": "build/wp-content/themes/yourproject/assets"
}
},
"parcelIgnore": [
"images/*.*",
"webfonts/*.*"
]
}
The root of the project is like this:
build/
-- build/wp-admin
-- build/wp-content
-- etc all the WordPress core/theme files that will ultimately end up on the server
The dev directory looks like this, basically just holding the source code for JS and CSS that get compiled into the theme:
In "scripts" I have tried setting "start" and "build" both with and without the --dist-dir flag. And I remove the "targets" object from package.json when I try --dist-dir.
For "targets", I can't find clear documentation/examples on what exactly goes here so I just tried anything I could think of. I have tried all of the following but nothing works the way I am intending it to. Parcel picks either assets-dev or assets and compiles everything there, it won't separate the code. Sometimes it will just compile files into a dist folder, which I am not using.
"targets": {
"start": {
"distDir": "build/wp-content/themes/yourproject/assets--dev"
},
"build": {
"distDir": "build/wp-content/themes/yourproject/assets"
}
},
"targets": {
"watch": {
"distDir": "build/wp-content/themes/yourproject/assets--dev"
},
"build": {
"distDir": "build/wp-content/themes/yourproject/assets"
}
},
"targets": {
"default": {
"distDir": "build/wp-content/themes/yourproject/assets--dev"
},
"build": {
"distDir": "build/wp-content/themes/yourproject/assets"
}
},
"targets": {
"dev": {
"distDir": "build/wp-content/themes/yourproject/assets--dev"
},
"prod": {
"distDir": "build/wp-content/themes/yourproject/assets"
}
},
Ultimately I am looking for how to get unminified assets into one folder that I can conditionally load, and minified assets into another folder that will get loaded on the prod server.
I figured it out. The CLI commands in the "scripts" portion of package.json can include the --target flag. This way you can name a target and then define it in the "targets" property. This is the working package.json:
{
"name": "localtesting2",
"version": "1.0.0",
"description": "## Overview",
"browserslist": "> 0.5%, last 2 versions, not dead",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "parcel watch develop/js/index.js develop/styles/style--critical.scss develop/styles/style--noncritical.scss develop/styles/editor.scss --target default",
"build": "parcel build develop/js/index.js develop/styles/style--critical.scss develop/styles/style--noncritical.scss develop/styles/editor.scss --target build"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"smoothscroll-polyfill": "^0.4.4"
},
"devDependencies": {
"#parcel/transformer-sass": "^2.3.2",
"autoprefixer": "^10.4.2",
"parcel": "latest",
"parcel-bundler": "^1.12.5",
"parcel-resolver-ignore": "^2.0.0",
"postcss": "^8.4.6",
"postcss-modules": "^4.3.0",
"sass": "^1.49.8",
"webfontloader": "^1.6.28"
},
"targets": {
"default": {
"distDir": "build/wp-content/themes/yourproject/assets--dev"
},
"build": {
"distDir": "build/wp-content/themes/yourproject/assets"
}
},
"parcelIgnore": [
"images/*.*",
"webfonts/*.*"
]
}
Still though there must be a better workflow for this, or maybe a way to get the watch command to minify everything like build does.

npm run build fails with: Syntax Error: TypeError: Cannot read property 'parseComponent' of undefined

I´m currently working on a website using vue/cli 4.5.13 and firebase.
After trying to get a simple authentification site working, my npm run build fails with this message:
ERROR Failed to compile with 1 error
Syntax Error: TypeError: Cannot read property 'parseComponent' of undefined
You may use special comments to disable some warnings. Use //
eslint-disable-next-line to ignore the next line. Use /*
eslint-disable */ to ignore all warnings in a file. ERROR Build
failed with errors.
I know this problem was asked here frequently but all the given solutions didn´t work for me.
So far i tried: npm install typescript#latest, npm uninstall #vue/component-compiler-utils,
npm install --dev #vue/component-compiler-utils#3.1.2, npm update vue-template-compiler and npm audit fix (--force).
my package.json looks like this:
"name": "my-website",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"#vue/component-compiler-utils": "^3.1.2",
"core-js": "^3.6.5",
"typescript": "^4.3.2",
"vue": "^3.0.2"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-eslint": "^3.1.1",
"#vue/cli-service": "^3.5.3",
"#vue/compiler-sfc": "^3.0.2",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0",
"vue-template-compiler": "^2.6.11"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
Try to delete node modules directory and then run
npm install

Create-react-app css modules with typescript: Cant resolve .module.css

I am trying to use css modules with typescript and create-react-app react app.
I did import './App.modules.css'; in my App.tsx but I get the error:
Module not found: Can't resolve './App.modules.css' in 'C:\...\testapp\src'
however, it works with const styles = require('./App.module.css');
then I apparently have to use classes (classNames) for my Styles...
If I only use normal tagnames e.g h1{ background-color: green; }
those tag styles are not scoped and are overridden by later styles...
are scoped css modules only limited to css classes and not to any other things like e.g tags, attributes etc. or is there a workaround? and why do I have to require them and cannot import them?
Typescript also sometimes complains that no types for .modules.css have been found..?
{
"name": "testapp",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"#types/jest": "^26.0.23",
"#types/node": "^15.0.3",
"#types/react-dom": "^17.0.5",
"bootstrap": "^4.6.0",
"css-modules-typescript-loader": "^4.0.1",
"react": "^17.0.2",
"react-bootstrap": "^1.6.0",
"react-dom": "^17.0.2",
"react-icons": "^4.2.0",
"react-scripts": "4.0.3",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^2.0.3",
"serve": "^11.3.2",
"typescript": "^4.2.4",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"serve": "serve -s build",
"db": "json-server --watch db.json"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/react": "^17.0.6",
"json-server": "^0.16.3"
}
}
Change your filename to lowercase.
import './app.modules.css'

React-App crashed due to node-sass error | TypeError: fs.existsSync is not a function

I have been working on my application for the past few months and it was working fine with node-sass installed until I came up with the following error with node-sass module itself today.
I tried debugging with upgrading node-sass to version-5, installed sass-loader tried everything to no avail so far. The error still persists.
My Package.json file:
{
"name": "giftshop2",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"firebase": "^7.23.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.0",
"sass-loader": "^10.0.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Any help would be highly appreciated.
I have just figured out that the problem is not with node-sass, I have accidentally imported render from node-sass in one of my components and this was breaking my whole application. And when I commented it my app started to work again. After 4 hours I found that how silly mistake it was :)
I am keeping my question open and not delete it, in case someone goes through the same problem, this will be helpful.
Thanks

Use latest #material-ui/icons in react-admin without conflicts

Are there any best practices on how to use the latest version of #material-ui/icons in react-admin? I'd want to use a newer icon that is not included in the 1.0.0 version, bundled with ra-ui-materialui.
When I try to use a newer version (3.0.1) of #material-ui/icons in my package.json the build sometimes errors with something like this:
./node_modules/ra-ui-materialui/node_modules/#material-ui/icons/FilterNone.js
Cannot find module: '#babel/runtime/helpers/builtin/interopRequireDefault'. Make sure this package is installed.
When using the same as the shipped version (^1.0.0) it builds fine consistently, however, I cannot use icons that were added only recently.
Is there a solution to this dilemma?
you need to use same the meterial UI version what is used in react-admin then you can use latest #material-ui/icons version .
Sample package.Json
{
"name": "sample-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^1.4.0",
"#material-ui/icons": "^3.0.2",
"apollo-cache-inmemory": "^1.4.2",
"apollo-client": "^2.4.12",
"apollo-link": "^1.2.8",
"apollo-link-http": "^1.5.11",
"autosuggest-highlight": "^3.1.1",
"firebase": "^5.8.4",
"graphql": "^14.1.1",
"graphql-tag": "^2.10.1",
"inflection": "^1.12.0",
"moment": "^2.24.0",
"prop-types": "^15.6.2",
"ra-core": "^2.6.4",
"ra-data-hasura": "0.0.3",
"react": "^16.8.3",
"react-admin": "^2.6.2",
"react-admin-color-input": "^1.0.8",
"react-autosuggest": "^9.4.3",
"react-dom": "^16.8.3",
"react-redux": "~5.0.7",
"react-router-dom": "~4.2.2",
"react-router-redux": "^4.0.8",
"react-scripts": "^2.1.5",
"recompose": "^0.30.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}

Resources