Skip to content

Add better examples #23

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
pixelass opened this issue Sep 16, 2016 · 14 comments
Closed

Add better examples #23

pixelass opened this issue Sep 16, 2016 · 14 comments

Comments

@pixelass
Copy link

pixelass commented Sep 16, 2016

IMHO it is very hard to understand how this works.

I think some real world examples with different combinations would help.

I looked into the tests but could not find any tests which combine certain properties (e.g. preprocessCss + generateScopedName)

As-is, I cannot use this plugin due to fear of misconfiguration.

Happy to contribute if I figure out how it works.

@pixelass
Copy link
Author

pixelass commented Sep 17, 2016

https://github.com/tone-studio/ui-patterns

Here's an example how I am using it to generate standalone modules. I'm pretty sure there's an easier way to do this.

What I'm doing:

  1. `preprocessCss``
  2. generateScopedName

This is the order, per definition of this plugin

"plugins": [
        ["css-modules-transform", {
          "generateScopedName": "./scripts/generate-scoped-name.js",
          "preprocessCss": "./scripts/preprocess-css.js",
          "extensions": [".css",".scss"],
          "extractCss": {
            "dir": "./lib/",
            "relativeRoot": "./src/",
            "filename": "[path]"
          }
        }]
      ]
    }

All files are then generated in the lib folder. (I adjusted the filename: See #24)

Since this plugin does not resolve dependencies I had to manually add @import statements to the css files. This also makes sure 'generateScopedName' doesn't run for every file, preventing wrong names.

After the above steps are done I run postcss-import to inline the @import statements.

I want to try to work without any extra build tool and your plugin is really helpful so far but I'm not sure if I'm over-engineering the transpile step.

@michalkvasnicak
Copy link
Owner

As I wrote in comment for #24 there is a bug in extractCss.filename configuration where [path] contains filename so [name] is useless. This will be fixed by #25 but I need to find a time for this.

What do you mean that this plugin does not resolve dependencies? Can you write a simple example please?

@pixelass
Copy link
Author

pixelass commented Sep 17, 2016

Thx for the quick response.
What I mean is:
It does not include dependencies (by design) and it should not. This is obviously what webpack or browserify are for. But due to that, the dependencies in css files are also not included, so I had to manually add them in files.

I guess this is only an issue because of the structure I chose for that project.

It works well as it is. I was just wondering if there is a better way to make sure css files are fully resolved (include all dependencies) while the js files are simply babelified

@michalkvasnicak
Copy link
Owner

extractCss.filename is fixed now in 1.0.1. [path]/[name].css should work and [path] should not.

@michalkvasnicak
Copy link
Owner

By dependencies you mean images, etc?

@pixelass
Copy link
Author

I mean required files:

Basically what extractCss does if it is a string 'combined.css' but per folder (with object notation)

@michalkvasnicak
Copy link
Owner

Ok I think that I understand now. Basically you want something like combined.css but for directory / entrypoint.

So for example for your molecules/poti/index.js you want index.css to contain css from poti-knob/index.css and poti-marker/index.css. Sou you have everything you need in single css file for given molecule.

Maybe we can do something like that but using some special option for extractCss so we can have 3 modes:

  1. combined (global)
  2. combined (for each parent their children) - NEW - this should work like 1. but will create a combined files for every child. So you will have tree where each parent node contains everything from children.
  3. own css file for each javascript file importing css

@michalkvasnicak
Copy link
Owner

But it is tricky because babel does not follow import/require statements, it just loads all files and processes them. Webpack on the other hand follows import/require statements so it is aware about context.

@pixelass
Copy link
Author

pixelass commented Sep 17, 2016

If I use postcss-import in processCss the generated names get messed up. I forgot to mention that.
So I am running that after everything is transpiled because I have to inline the imports after the names have been generated.
(I am well aware that this might not be an intended feature and fully understand if it will be rejected.)
The "2nd" (new) mode sounds perfect but it would have to support this requirement: (without the @import statements )

atoms/poti-knob/index.css

.UI\:atoms--poti-knob__knob {
  content: 'poti knob'; }

.UI\:atoms--poti-knob__indicator {
  content: 'poti knob indicator'; }

atoms/poti-marker/index.css

.UI\:atoms--poti-marker__marker {
  content: 'poti marker'; }

.UI\:atoms--poti-marker__label {
  content: 'poti marker label'; }

.UI\:atoms--poti-marker__selected {
  content: 'poti marker selected'; }

molecules/poti/index.css

@import '../../atoms/poti-marker/index.css';
@import '../../atoms/poti-knob/index.css';

.poti {/* shortened*/}

.focused {
  content: 'poti focused';
}

.knobWrapper {
  content: 'poti knobWrapper';
}

.markerIndicator {
  content: 'poti markerIndicator';
}
.UI\:atoms--poti-marker__marker {
  content: 'poti marker'; }

.UI\:atoms--poti-marker__label {
  content: 'poti marker label'; }

.UI\:atoms--poti-marker__selected {
  content: 'poti marker selected'; }

.UI\:atoms--poti-knob__knob {
  content: 'poti knob'; }

.UI\:atoms--poti-knob__indicator {
  content: 'poti knob indicator'; }

/* 
 * names above are correct (atoms--poti-knob & atoms--poti-marker)
 * names below have a different prefix (molecules--poti)
 */

.UI\:molecules--poti__poti {/* shortened*/}

.UI\:molecules--poti__focused {
  content: 'poti focused'; }

.UI\:molecules--poti__knobWrapper {
  content: 'poti knobWrapper'; }

.UI\:molecules--poti__markerIndicator {
  content: 'poti markerIndicator'; }

@michalkvasnicak
Copy link
Owner

Have you tried to use postcss-import as part of a prepend?

css-modules/css-modules-require-hook#58 (comment)

There is also mention about sync mode of postcss-import and it's supported by postcss-import v7.

@michalkvasnicak
Copy link
Owner

michalkvasnicak commented Sep 17, 2016

Because you need to resolve imports before css AST is processed.

@pixelass
Copy link
Author

I've played around with various combinations. The current solution seems to work well. The only downside is having to import css files. prepend did not work for me. I might have to look into it again.

@michalkvasnicak
Copy link
Owner

I think we can close this issue. I don't see any simple solution for this problem and as I wrote, babel don't know about context because it does not follow imports/requires so we can't construct tree of css files.

@pixelass
Copy link
Author

👍

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

2 participants