Skip to content
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

Add a hook function to the exports generation #511

Closed
eduardoboucas opened this issue Apr 23, 2017 · 1 comment
Closed

Add a hook function to the exports generation #511

eduardoboucas opened this issue Apr 23, 2017 · 1 comment
Assignees
Projects

Comments

@eduardoboucas
Copy link

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

What is the current behavior?
A exports.locals object is automatically created, resolving human-friendly class names to their generated names.

What is the expected behavior?
It would be nice to be able to intercept and modify the code that is exported using a hook. This would be done using a exportsHook option in the loader and would run the exported code by this function, if it exists, before the loader finishes.

If this is a feature request, what is motivation or use case for changing the behavior?
This would allow people to create custom behaviour for how their styles are imported into their components. Although the use cases are endless, I have a very specific one in mind.

Instead of importing an object with the class names, I want to import a small function that would be aware of the classes available and do some smart management/concatenation with them, accepting as first argument the component to be rendered as well as a list of conditions/classes to be added to it. Ultimately, this is what I want to achieve:

import style from './Header.css'

export default class Header extends Component {
  render() {
    const StyledHeader = style('div',
      'header',
      this.props.large && 'header-large',
      this.state.disabled && 'header-disabled'
    )

    return (
      <StyledHeader someProp={true}>
        <p>Some children</p>
      </StyledHeader>
    )
  }
}

The implications in terms of file size and performance would be negligible, as we're talking about an if statement that checks for the function and calls it if it exists. It wouldn't introduce any breaking changes either.

I'm more than happy to put together a PR and tests for this, just wanted to check whether this is something that you'd like to see happening.

Thanks in advance!

@alexander-akait
Copy link
Member

@eduardoboucas Thanks for issue, it is out of scope css-loader, you can create own function in your app to solve this problem.

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

No branches or pull requests

4 participants