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

Update themr.js #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Update themr.js #68

wants to merge 2 commits into from

Conversation

idangozlan
Copy link

when the source style is empty / contain only :global rules, extract-text-webpack-plugin put string instead of json, which break the merge.
this if prevent this specific case
#66

when the source style is empty / contain only :global rules, `extract-text-webpack-plugin` put string instead of json, which break the merge.
this if prevent this specific case
javivelasco#66
@raveclassic
Copy link
Contributor

raveclassic commented Jun 8, 2017

Thanks for PR!

Still I don't think that it is a solution because then themr starts to depend on some magic string from the other library. When it changes and no doubt one day it changes, we'll get the same error so I think we need to find more generic solution.

On the other hand, doesn't extract-plugin violate the principe of a css-module? Shouldn't an import of a module always be an object? Shouldn't it be an empty object with no keys when there are no styles? Could you please check what does an import contain without extract plugin for a file with only global styles?

UPDATE:
Moreover why would you apply an empty theme with no keys anyway?

@idangozlan
Copy link
Author

idangozlan commented Jun 8, 2017 via email

@raveclassic
Copy link
Contributor

So as far as I can see, extract plugin changes the type of imported module from object to string when all styles are global, am I right?
Then can we check module's type when passing it to decorator and throw the error earlier? Or even do not merge anything and take theme object from props as is?

@idangozlan
Copy link
Author

idangozlan commented Jun 8, 2017 via email

@raveclassic
Copy link
Contributor

raveclassic commented Jun 8, 2017

But it's incorrect to try to merge a string with an object and suppressing the error or replacing it with a warning is more like an in-place hack.

Maybe the best solution is not to apply an empty theme at all? Could you provide more concrete example of your component? Do you specify a shape of the theme either via propTypes or ts/flow type/interface?
If you do then then the problem is that you pass incorrect shape - a string. And this string is produced by third-party library (extract-plugin) so why should themr adjust its behavior to cover such edge-case which is absolutely out of scope of applying a theme to a component? Furthermore I think that we should also throw an error when passing incorrect theme to @themr decorator as default theme to fail earlier.

Here's an example of type-related solution:

import * as theme from './only-globals.css'; //theme === //removed by extract-plugin
import { themr } from 'react-css-themr';

type TProps = {
  theme: { //you specify a strict API which should be respected
    container: string
  }
}

//do not apply default theme because it's actually empty and is not assignable to defined type
//we should also throw here in runtime
@themr(Symbol()/*, theme*/) 
export class Foo extends React.Component<TProps, never> {
}

@idangozlan
Copy link
Author

idangozlan commented Jun 8, 2017 via email

@raveclassic
Copy link
Contributor

I still don't think that hardcoding a string // removed by... is a good idea.
Adding a check for default theme type in the beginning of @themr decorator would emit an error in design-time instead of run-time and I think it would be enough for a developer to understand that a theme passed to decorator is invalid. What do you think?

@idangozlan
Copy link
Author

idangozlan commented Jun 8, 2017 via email

@raveclassic
Copy link
Contributor

Well, errors in themeable function imo should be related only to type incompatibilities while I agree that @themr decorator could throw something more informative.

Would you mind updating the PR?

@idangozlan
Copy link
Author

idangozlan commented Jun 8, 2017 via email

@idangozlan
Copy link
Author

Done.

@raveclassic
Copy link
Contributor

Great, thanks! Now we need @javivelasco to merge it

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

Successfully merging this pull request may close these issues.

2 participants