aphrodite icon indicating copy to clipboard operation
aphrodite copied to clipboard

Specify custom classname generation?

Open kevinSuttle opened this issue 9 years ago • 10 comments
trafficstars

I assume this is using https://github.com/webpack/loader-utils#interpolatename or https://github.com/garycourt/murmurhash-js ? Is there a config object to specify this?

kevinSuttle avatar Oct 12 '16 03:10 kevinSuttle

Hi @kevinSuttle,

There's no way to specify this at the moment, and there aren't plans to add configuration for this.

What's your use case?

jlfwong avatar Oct 12 '16 18:10 jlfwong

In Webpack, you can do this with CSS Loader,

css-loader?localIdentName=[path][name]---[local]---

which eliminates the need for random hashes.

kevinSuttle avatar Oct 12 '16 19:10 kevinSuttle

@kevinSuttle The hashes are used explicitly so that when we rehydrate after server-side rendering we don't generate duplicate styles. They aren't just added for uniqueness.

Is there some problem with the classnames that we generate? Is there a reason you'd like to change how they're generated?

xymostech avatar Oct 12 '16 19:10 xymostech

It's the same issue with the default CSS Module setup: makes it hard to debug. http://kevinsuttle.com/posts/css-modules-a-review#local-by-default-is-a-double-edged-sword

kevinSuttle avatar Oct 12 '16 19:10 kevinSuttle

@kevinSuttle It would be great if you added comment support to your site so we can continue the discussion on your linked post.

ghost avatar Oct 22 '16 08:10 ghost

Feel free https://github.com/kevinsuttle/kevinsuttle.github.io/issues/new

kevinSuttle avatar Oct 22 '16 18:10 kevinSuttle

Side note: not sure if this would be useful or even possible, but I thought I'd bring it up to those with the bigger brains here.

screen shot 2016-10-26 at 1 09 28 pm screen shot 2016-10-26 at 1 10 27 pm

kevinSuttle avatar Oct 26 '16 17:10 kevinSuttle

@kevinSuttle The class names we generate already are a lot friendly than the mangled ones there (i.e. we preserve the key name instead of entirely mangling it is like it is in 1jrfA.

I do think it would be really nice to have the ability to include the file name where the StyleSheet.create call happened, and supporting that wouldn't be too hard.

I am against having an arbitrary classname generation API, since the ways in which classes are generated is integral to making Aphrodite work correctly, but I can imagine something like this:

const styles = StyleSheet.create({
    red: { color: 'red'; }
    blue: { color: 'blue'; }
}, 'my-file-name.jsx');

Then the generated class would do something intelligent where it automatically uses that prefix on the classnames. so Instead of getting css(styles.red) -> "red_219ah1", you get css(styles.red) -> "my-file-name__red_219ah1.

Writing the filename manually everywhere would be crummy, but you can do an AST transform that only happens at dev time to automatically insert file names into any StyleSheet.create call as the last argument.

But stepping back a second, I might suggest that colocating your styles with your components via Aphrodite offers a different approach to debugging entirely. If you have react devtools installed, then you can find the innermost React component containing the class-name, and work from there to pretty quickly find the style definition (it's likely in the same file, barring shared styles).

jlfwong avatar Oct 26 '16 20:10 jlfwong

Would it be any easier to prepend the React classname where the style is in use? That would help debugging when traversing the DOM.

liveresume avatar Nov 08 '16 19:11 liveresume

@liveresume Aphrodite explicitly does not depend on React, so that's not really an option.

jlfwong avatar Nov 09 '16 00:11 jlfwong