-
Notifications
You must be signed in to change notification settings - Fork 262
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
Allow for importing styles #83
Comments
I believe that external styles could be enabled with a
export default `
p { color: red; }
` which we transpile to: module.exports = {
css: `p[data-jsx="woot"] { color: red; }`,
id: 'woot'
} Tbd how to handle the |
@giuseppeg stylis does have support for handling sass like |
@thysultan interesting... that works only for plain css though right? Now we support expressions within template literals so external styled jsx files should be javascript modules. |
@giuseppeg Yes a string so you might do... var m = new module.constructor();
m._compile('module.exports = `'+fs.readFileSync(filename, 'utf8')+'`', filename);
var str = m.exports;
return str; or maybe even... try {
new Function("`"+fs.readFileSync(filename, 'utf8')+"`", arguments);
} catch(e) {
} or even parse the file for template literals expressions from the middleware with regex and replace them with their eval'd result. Notes no how middleware work: https://github.com/thysultan/stylis.js#middleware |
@thysultan the problem is that in styled jsx you could do the following import {primary} from '../theme/colors'
export default `
p { color: ${primary}; }
` |
@giuseppeg couldn't you still transpile that with babel and do |
do you mean resolve the template literal to static string? |
Yes, have babel compile. import {primary} from '../theme/colors'
export default `
p { color: ${primary}; }
` Then pass that through var m = new module.constructor();
m._compile('module.exports = `'+transpiledFileContent+'`', filename);
var str = m.exports; Now you have a static string like |
I have used babel inline-import plugin https://www.npmjs.com/package/babel-plugin-inline-import With that babel automatically inlined my css in js. However, inlining file A in file B any changes in file A will not trigger hot-reloading unless you also change B. I believe being able to use external files with hot-reloading is needed because
|
@davibe cool indeed using something like babel-plugin-inline-import gets the job done if you don't need constants in your jsx (string interpolation). Hot-reloading is out of the scope of this package. |
Guys, I hacked for something like this: export default class {
render () {
return (
<div>
<p>test</p>
<style jsx src="./src-style.css" />
</div>
)
}
} See Implementation: https://github.com/arunoda/styled-jsx/blob/style-src/test/fixtures/src.js But with this, we can't get hot reload for CSS changes. Which is not something great. So, I gave up on this idea. |
I believe that if we use js modules like i suggested above we would also get hot reload. I am not a big fan of inlining css because potentially it could lead to duplicate code – probably not a problem with gzipped content but I'd happily avoid that. Also now we support constants so external css file should be js modules imho. |
I'd imagine a nice approach would be to support <div>
<button>Hello world</button>
<style jsx>{`
@import 'bootstrap.css';
button {
composes: btn btn-primary;
font-size: 30px;
}
`}</style>
</div> |
@giuseppeg I've asked babel team for tips how to implement such plugin: babel/babel#5505 |
It seems changing parser per-file is totally possible: https://github.com/lightscript/babel-plugin-lightscript/blob/master/src/index.js#L1001 |
@sheerun thanks that's very interesting and useful information! We are trying to keep CSS (styled-jsx) as standard compliant as possible without introducing custom features. The reason for this choice is that everybody has personal preferences and not necessarily universally wanted sets of custom features that would like to use/have and adding all of them would make the plugin very complex. Those imho belong to user land and can be implemented as an extension of styled-jsx in some cases (see styled-jsx-postcss). In this particular case I would like to avoid to reinvent the CSS Modules composes feature. |
Any updates on this? Not being able to import styles is a big deal breaker for us, apart from that we'd love to use this library. |
Styles can be defined in separate JavaScript modules and exported as strings. Fixes #83
Sometimes styles can be quite long and it makes sense for them to live in a seperate file. For example, if you are using next.js and you want to put your styles in a
./styles.js
file next to a component (minimal styling just for the example):Then import it:
Will give you the following error:
Module build failed: SyntaxError: Expected a template literal or String literal as the child of the JSX Style tag (eg: <style jsx>{
some css}</style>), but got Identifier
.The text was updated successfully, but these errors were encountered: