Skip to content

Document SASS & CSS Modules #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LukasBombach opened this issue Mar 5, 2018 · 10 comments
Closed

Document SASS & CSS Modules #522

LukasBombach opened this issue Mar 5, 2018 · 10 comments

Comments

@LukasBombach
Copy link

The Readme claims that react cli has support for SASS and CSS Modules.

I read in another issue how to enable support for SASS (yarn add -D node-sass sass-loader) but I don't know how to enable CSS Modules. But this is now what this this issue is for, I made a Stackoverflow post for this. With this issue I want to feature-request documentation on this. I'd PR this, but I don't know how to CSS Module :(.

Do you want to request a feature or report a bug?

Feature

What is the current behaviour?

No documentation on this

What is the expected behaviour?

I'd like to know how it works

If this is a feature request, what is motivation or use case for changing the behaviour?

Developers starting with preact cli know what to do

@ForsakenHarmony
Copy link
Member

Installing that is all you need to do

@fokusferit
Copy link

CSS Module support is already there you can see the configuration for user styles here https://github.com/developit/preact-cli/blob/master/src/lib/webpack/webpack-base-config.js#L164 so the question is, are you seeing any issue when you run it locally ?

@LukasBombach
Copy link
Author

LukasBombach commented Mar 6, 2018

Yes, locally. Check out my project: https://github.com/LukasBombach/poc-preact-cli-css-modules

Check out how I use it: I import the styles: https://github.com/LukasBombach/poc-preact-cli-css-modules/blob/master/src/layout/index.js#L5

import { h, Component } from 'preact';
import styles from './index.module.css';

export default class Layout extends Component {
  render() {
    return (
      <div className={styles.container}>
        {this.props.children}
      </div>
    );
  }
}

from this file

.container {
  margin: 50px auto;
  max-width: 960px;
  width: 100%;
}

It all happens in this folder: https://github.com/LukasBombach/poc-preact-cli-css-modules/tree/master/src/layout

(index.js and index.module.css)

@LukasBombach
Copy link
Author

The result is am empty class attribute and if I log styles it's an empty object.

@LukasBombach
Copy link
Author

LukasBombach commented Mar 6, 2018

I think I found the issue. preact cli forces you to put your components in the src/components folder or css modules won't work. Now it does.

@reznord
Copy link
Member

reznord commented Mar 6, 2018

You can watch on other folders for CSS by extending the webpack config using preact.config.js:

config.module.loaders[4].include = [
  path.resolve(__dirname, 'src', 'routes'),
  path.resolve(__dirname, 'src', 'components'),
  path.resolve(__dirname, 'src', 'sections')
];

config.module.loaders[5].exclude = [
  path.resolve(__dirname, 'src', 'routes'),
  path.resolve(__dirname, 'src', 'components'),
  path.resolve(__dirname, 'src', 'sections')
];

@LukasBombach
Copy link
Author

Thank you @reznord

@tzvc
Copy link

tzvc commented May 7, 2018

Thanks for the tips @reznord !
I have a question thought, what exactly represent config.module.loaders[4] and config.module.loaders[5] ? Why do we new to include them in one and exclude them from the other ?

@webdevotion
Copy link

@theochampion this is the output of config.module.loaders:

[ { enforce: 'pre',
    test: /\.jsx?$/,
    loader: 'babel-loader',
    options: { babelrc: false, presets: [Array], plugins: [Array] } },
  { enforce: 'pre', test: /\.less$/, use: [ [Object] ] },
  { enforce: 'pre', test: /\.s[ac]ss$/, use: [ [Object] ] },
  { enforce: 'pre', test: /\.styl$/, use: [ [Object] ] },
  { test: /\.(css|less|s[ac]ss|styl)$/,
    include:
     [ '<...__dir__...>/src/components',
       '<...__dir__...>/src/routes' ],
    loader: [ [Object], [Object], [Object], [Object] ] },
  { test: /\.(css|less|s[ac]ss|styl)$/,
    exclude:
     [ '<...__dir__...>/src/components',
       '<...__dir__...>/src/routes' ],
    loader: [ [Object], [Object], [Object], [Object] ] },
  { test: /\.json$/, loader: 'json-loader' },
  { test: /\.(xml|html|txt|md)$/, loader: 'raw-loader' },
  { test: /\.(svg|woff2?|ttf|eot|jpe?g|png|gif|mp4|mov|ogg|webm)(\?.*)?$/i,
    loader: 'url-loader' },
  { test: /\.jsx?$/,
    include: [ [Function], [Function] ],
    loader: '<...__dir__...>/node_modules/preact-cli/lib/lib/webpack/async-component-loader',
    options: { name: [Function: name], formatName: [Function: formatName] } } ]

As you can see index 4 and 5 take care of stylesheet file extensions. At index 4 folders are included and at index 5 folders are excluded. What @reznord's snippet basically does is providing a way to manually override them in your personal project's preact.config.js file.

Custom configuration of your project is described in the README.

@schwiet
Copy link

schwiet commented Sep 29, 2020

For anyone running into this issue now, as you might expect this workaround is relatively fragile. loaders is replaced by rules (ref: https://webpack.js.org/configuration/module/#modulerules):

config.module.rules[4].include = [
  path.resolve(__dirname, 'src', 'routes'),
  path.resolve(__dirname, 'src', 'components'),
  path.resolve(__dirname, 'src', 'sections')
];

config.module.rules[5].exclude = [
  path.resolve(__dirname, 'src', 'routes'),
  path.resolve(__dirname, 'src', 'components'),
  path.resolve(__dirname, 'src', 'sections')
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants