6

Please see https://github.com/gajus/babel-plugin-react-css-modules/issues/162 for full description of issue.

Git repo = https://github.com/basher/react-no-webpack

This is a simple POC / scaffold for a React UI lib without Webpack or Gulp but it must support CSS Modules + Sass.

  • I'm just trying to use Babel + Browserify.
  • And executing NPM scripts directly from "package.json".

I have a sample widget component that imports a CSS file, and another that imports a Sass file. The error happens when parsing the content of both CSS + Sass files - e.g. transpiler does not understand "." in the class selector.

Here's the specific error:

$ npm run watch

> react-no-webpack@1.0.0 watch C:\...path-to-project-folder...\react-no-webpack
> watchify ./src/index.js -o ./build/bundle.js -t babelify -v


C:\...path-to-project-folder...\react-no-webpack\src\lib\components\WidgetCSS\WidgetCSS.css:1
.widget {
^
ParseError: Unexpected token

Edit / Update:

I've done some more investigation, and have asked a question in react-css-modules repo too = https://github.com/gajus/react-css-modules/issues/268

basherkev
  • 83
  • 6

2 Answers2

1

ParseError: Unexpected token mean your babel not understand css syntax

react-css-modules module need transpiler css like webpack, read for more detail.

For your case you can use css-modulesify.

1) Install css modulesify

$ npm install --save css-modulesify

2) Update your package json script

"build": "browserify ./src/index.js -p [ css-modulesify -o ./build/main.css ] -o ./build/bundle.js -t babelify",
"watch": "watchify ./src/index.js -p [ css-modulesify -o ./build/main.css ] -o ./build/bundle.js -t babelify -v"

remember to include ./build/main.css to index.html for css.

Community
  • 1
  • 1
hendrathings
  • 3,720
  • 16
  • 30
  • 1
    Thanks @hendrathings 2 comments about your answer: 1. **babel-plugin-react-css-modules** claims to work without Webpack, and with browserify. This is why I'm seeking help as there is quite clearly an issue with this actually working as expected. 2. **css-modulesify** is highly experimental and has not been updated for 8 months. I've chosen to ignore this package. – basherkev Mar 12 '18 at 08:43
  • @basherkev `react-css-module` need 2 setup either webpack or browserfy. Please check https://github.com/gajus/react-css-modules#browserify for detail. It's says, if use browserfy refer to css-modulesify. – hendrathings Mar 12 '18 at 09:12
  • also, `babel-plugin-react-css-modules claims to work without Webpack` where is you find this? The docs use webpack, check here: https://github.com/gajus/babel-plugin-react-css-modules#installation. If you see the demo, it is using webpack. – hendrathings Mar 12 '18 at 09:22
  • There is no way without `webpack` or `css-modulesify`. It will throw error `ParseError: Unexpected token` because javascript trying parse `css` syntax. – hendrathings Mar 12 '18 at 09:27
1

Thanks @hendrathings - you are correct.

I must have misread the docs, or read about a non-webpack setup somewhere else. I've read so many articles recently that I have probably confused myself!

I have followed your suggestion - I'm now using css-modulesify.

I have a working example POC UI lib with minimal React + CSS Modules config, with both CSS and Sass syntax (using a couple of example PostCSS plugins). This can be found in my updated https://github.com/basher/react-no-webpack repo.

basherkev
  • 83
  • 6