Closed punmechanic closed 6 years ago
Right now, this isn't supported. It's come up previously in the context of compile-to-JS languages (https://github.com/sveltejs/svelte/issues/98), and tangentially in the context of creating components from multiple files (i.e. a linked stylesheet/script) in https://github.com/sveltejs/svelte/issues/65. But until then, it would be a case of preprocessing the CSS and creating a single-file component to hand off to Svelte.
FWIW, we made our own svelte-cli analog that runs the stylesheet through the less compiler and then hands the result off to the svelte compiler. I can't open-source it because my employer doesn't understand the benefits, but it wasn't hard. Currently I am trying to modify a fork of the svelte-loader to use our compiler that wraps less and svelte.
@therealnicksaunders What did you use as extension point in svelte-cli? It would make sense to introduce an official compiler-plugin api to svelte-cli which allows to process compilation steps. See also #194
@dschulten we didn't need most of the options in svelte-cli so I wrote our version from scratch. It consumes our xxxxx-svelte library that has a compile method that takes care of less precompilation and then passes the result to svelte's compile method. The API pretty much matches svelte, with the main difference being that it is asynchronous due to the less API being asynchronous.
I guess my point was that it was not terribly difficult as an interim solution, but I agree that this is going to be something a lot of people want out-of-the-box.
Maybe a good first step would be to make svelte's compile method asynchronous. That would have allowed me to fork svelte-cli, svelte-loader, et cetera, and leave them mostly unmodified (just swapping out the svelte dependency in favor of our own version).
I don't think it would take more than an hour or two to create a simple npm package that would pre-parse the Svelte HTML files for <style>
enclosures, pass that through the compiler of your choice, then pass the entire file on to Svelte. It would be hacky, but it'd work for now.
I may work on this this evening.
Has this been implemented yet?
Not yet, no
The only thing keeping Svelte from being approved for a big project right now is the lack of official support for SASS/SCSS by the framework (which is a must for all company projects where I work).
Has anyone managed to "hack" SASS support into Svelte? If yes, do you guys have any tips or perhaps a code sample to share?
The main issue is that Svelte can't just add support for SASS, it has to add full support for any CSS preprocessors. Svelte also modifies the generated CSS, so the build process has to actually be handled by Svelte as well.
I'm currently just using the scss
through webpack build process. Having to use the hash
based css names through sass, ahh - someone might need to have a go at that or CSS modules thing. I think, due to compilation everything is very limited to change, like supporting Sass and TypeScript. I would love to learn more about the patterns that could be invented for it to become approved for bigger projects.
My current CSS build process consists in creating a single-file component as suggested by @Rich-Harris, importing it in my global style file which is then processed by postcss-nextcss.
However, I'm experiencing the following issues:
components.css
file is updated (in my case by Rollup), I need to rerun the global style build script in order to include the updated components. In this way I'm able to reuse global custom propertiesAt the end the build process works but I can't use many useful nextcss features in scoped style, and the overall build process is quite slow causing unwanted Browsersync reloads.
Do you have any suggestion to improve it?
@Rich-Harris
Instead of implementing features like transitions (https://github.com/sveltejs/svelte/issues/525) & easing functions (https://github.com/sveltejs/svelte/issues/549) in Svelte, doesn't it make more sense to add support for CSS preprocessors like Less & Sass and use preprocessors to add custom transition behavior to components?
It seems to me that high level features like transitions & easing don't really belong in a framework like Svelte, whereas preprocessor support is pretty much essential for many companies to even consider using a framework like Svelte.
Preprocessors can't help you with transitions (which are already implemented, but need a section in the docs) — that needs to be a framework-level feature. No amount of CSS could do this, for example:
https://svelte.technology/repl?version=1.29.0&gist=f94338ee682bddccb8596874cad5b68d
For one thing, you can't use an easing function like that in a CSS animation. For another, if Svelte were to remove the <span>
from the DOM immediately (because it didn't know about the out
transition), it would be impossible to smoothly fade it out. Svelte transitions let you build fairly complex (and data-driven, via parameters) intro/outro effects that would be impossible with CSS itself, but that use CSS animations under the hood (i.e. off the main thread).
Realistically, we have two choices:
svelte.compile
async — without that, it's impossible to plug in support for some preprocessorsIn the meantime, it's totally possible to use preprocessors with something like this pseudo-code:
code = code
.replace(/<style>(.+)<\/style>/, (m, styles) => `<style>${preprocess(styles)}</style>`)
No amount of CSS could do this. For one thing, you can't use an easing function like that in a CSS animation.
Really? Doesn't the transition-timing-function
property in combination with the cubic-bezier
keyword allow you do achieve pretty much the same result, but with better performance?
Pretty much every modern browser supports this, including IE > 9.
Or is there something I'm missing here?
if Svelte were to remove the
<span>
from the DOM immediately (because it didn't know about the out transition), it would be impossible to smoothly fade it out.
Sure, it does require some trickery to do transitions in CSS right, especially if you're hiding & showing things. But again, this is nothing that can't be done with just SCSS.
Svelte transitions let you build fairly complex (and data-driven, via parameters) intro/outro effects that would be impossible with CSS itself, but that use CSS animations under the hood (i.e. off the main thread).
Again, that's precisely what languages like SCSS are for. Sure, many people use them only for parameterization, but the language is far more powerful than that.
Wait until v2, so we can make
svelte.compile
async — without that, it's impossible to plug in support for some preprocessors
Works for me, I guess. When is v2 expected to be released?
In the meantime, it's totally possible to use preprocessors with something like this pseudo-code:
code = code .replace(/<style>(.+)<\/style>/, (m, styles) => `<style>${preprocess(styles)}</style>`)
No thanks!
Having to use dirty hacks like this as workarounds to implement a missing feature as essential as SCSS support is a good reason not to use this framework for the time being. I think I'll just wait ;-)
@jslegers Pretty much what @Rich-Harris said. On our project, we built a tiny CSS preprocessing Webpack loader that is applied to *.html files before they are handed off to the svelte-loader. The loader looks for inline LESS stylesheets via a regex similar to Rich's, runs their contents through the LESS compiler, and then replaces the original stylesheet with the processed stylesheet. We used the less-loader's getOptions service which even made it easy to tie into Webpack's module loading system for LESS imports and such.
This has worked flawlessly for us, so maybe your team can do something similar?
This has worked flawlessly for us, so maybe your team can do something similar?
I'm looking at Svelte first and foremost as a foundation upon which to build version 2 of Cascade Framework, which is a one-man project I do in my spare time. The project being first and foremost a CSS framework makes native SCSS support pretty essential.
At my employer's, the current approach for the frontend of our products is to use TypeScript + React + Webpack. I'd really love to recommend Svelte + Rollup or Svelte + Webpack as an alternative for React + Webpack, but here as well I consider native SCSS support a rather essential feature. TypeScript support (https://github.com/sveltejs/svelte/issues/98) would also be pretty essential. And even that wouldn't be enough, I'm afraid, to convince my colleagues to make that kind of investment. I'll need a much stronger case to convince anyone to replace React + Webpack with an alternative that sure looks promising but doesn't seem mature enough just yet.
There is already an issue for that - #65 - and it's on the roadmap (#622) under "Medium term".
Or is there something I'm missing here?
Yep. cubic-bezier is very limited. You can't do the elastic easing function I showed above, for example.
Sure, it does require some trickery to do transitions in CSS right, especially if you're hiding & showing things. But again, this is nothing that can't be done with just SCSS.
There's no way to style an element that's no longer in the DOM. You have to delay the removal of the node from the DOM. In other words, the framework needs to understand whether or not a given element has an outro transition — i.e., a framework-level feature.
Again, that's precisely what languages like SCSS are for.
SCSS can't do things like vary the duration of a transition based on some value that isn't known until runtime.
Works for me, I guess. When is v2 expected to be released?
When it's done 😉
Just as an aside, saying things like 'that wouldn't be enough, I'm afraid, to convince my colleagues to make that kind of investment' doesn't motivate the maintainers of OSS projects to drop everything to support your use case — it just creates antagonism. This is a project that the other maintainers and I work on in our spare time. If you want a particular feature to land sooner, here's the link you're looking for!
Yep. cubic-bezier is very limited. You can't do the elastic easing function I showed above, for example.
* oops *
What about a sequence of key-frames
?
There's no way to style an element that's no longer in the DOM. You have to delay the removal of the node from the DOM.
In such a case, can't you just add/remove a class with a CSS transition, timeout to allow the transition to render and then remove the element from the DOM?
SCSS can't do things like vary the duration of a transition based on some value that isn't known until runtime.
Fair enough. How often do you need that, though? The number of use cases that requires the duration of a transition to be determined at runtime seems pretty small to me.
Just as an aside, saying things like 'that wouldn't be enough, I'm afraid, to convince my colleagues to make that kind of investment' doesn't motivate the maintainers of OSS projects to drop everything to support your use case — it just creates antagonism.
I'm sorry if I sound antagonistic. That's 100% unintended. In fact, I haven't been as exited about any open source project as much I am about Svelte in years.
And Rollup looks just as promising by what I've seen so far, so kudos for that as well! 👍
Anyway, I absolutely love where you're going with Svelte & I'm seriously considering it as a foundation for my own open source projects once it has features like native SCSS support & named scoping (https://github.com/sveltejs/svelte/issues/570).
I guess I'll have to work some more on my social skills if I hadn't made that clear enough ;-)
Thanks — it's all cool, it's just very hard to balance all the competing feature requests while keeping the project focused and maintainable. Svelte will never 'natively' support SCSS, insofar as that means including node-sass
and using it without some form of configuration, because that kind of coupling is dangerous. It's more likely to look something like this:
import svelte from 'svelte';
import scss from 'svelte-scss';
const code = `
<div><p>styled with SCSS</p></div>
<style type='scss'>
div {
p {
color: red;
}
}
</style>
`;
svelte.compile(code, { preprocess: { scss } })
.then(result => ...);
That way, we just need to define a preprocessor interface and it can be reused with LESS, postcss, etc.
it's just very hard to balance all the competing feature requests while keeping the project focused and maintainable.
I know. I work in R&D professionally & have my own open source projects on the side. I know where you're coming from. I feel your pain ;-)
I guess I'm just trying to throw in my 5 cents as I really really love where the Svelte core framework is heading in general. It's just missing a few features that are pretty essential to me, and I guess I'm just frustrated that features like transitions are getting so much attention that could have been used on improving the core, the build process or the docs...
But hey... it's not my project and I don't even have enough time for my own projects at the moment, so sorry again if I come off as ungrateful or greedy :-)
It's more likely to look something like this
What would work for me, I guess, would be an officially supported way to plug node-sass
into the standard build process.
As an "end user", I expect to be able to write my components like this...
<div><p>styled with SCSS</p></div>
<style>
div {
p {
color: red;
}
}
</style>
... and just run npm run build
, the exact same way I'd do it if it my styles were CSS instead of SCSS.
Am I OK with having an extra build step in the build process? Sure! Am I OK with having to install a separate plugin to add this feature? Sure!
I see why you'd want to separate preprocessor support from the core library, but adding SCSS or Less support shouldn't be any more complicated than adding a plugin... and I don't want to have to write my own plugins or look for third party plugins for something as elementary as SCSS support!
"End users" shouldn't have to know anything about the build process other than how to run it. They should be able to treat it like a black box. It shouldn't matter to an "end user" of Svelte whether it's Rollup or Webpack that's under the hood and how many build steps are used to get from src
to dist
. And they shouldn't have to write their own build processes for adding preprocessor support.
Also, I don't want to risk losing my preprocessor support every time I upgrade the Svelte build process. Without at least an officially supported plugin for preprocessor support, the chances of that occuring are pretty high.
What would work for me, I guess, would be an officially supported way to plug node-sass into the standard build process.
That would be the point of this issue then!
I'm going to add some fuel to the fire here and state my current position that CSS preprocessing is a separate concern and should not be handled by Svelte at all. svelte-cli might give the impression that Svelte is the build process, but, frankly, for any non-trivial project (like the kind that would require CSS preprocessing), Svelte should be merely one step in the build process. Let the Svelte compiler itself focus on the native languages of the Web (CSS, HTML, JavaScript) and keep unnecessary complexity out!
Instead of lobbying for plugins or CSS preprocessing in the Svelte core, perhaps our little Svelte community should tackle the problem of real build tools like Webpack, Browserify, Rollup, et cetra, lacking the necessary plugins/loaders to process HTML modules. (For example, as I previously explained, Webpack has less-loader, but I couldn't find a loader that could be applied to an HTML file with inlined LESS). This is a relatively easy problem for just about anybody to solve, as it only requires knowledge of the build tool and some string manipulation. It is way easier than working on the source of the Svelte compiler.
I think it's up to @Rich-Harris to guide the direction of this "anti-framework". But if I were him, I would strongly prefer to outsource the CSS preprocessing problem instead of wasting my time trying to build and maintain a plugin API that is perhaps unnecessary.
Yeah I think I largely agree with @therealnicksaunders . Perhaps the only "plugin API" that Svelte needs to support here is that it can generate sourcemaps leading back to the original sources if its input included sourcemaps.
Yep, the sourcemap stuff is crucial, and also incredibly finicky to try and stitch back together later unless it's part of the compiler. The other place where Svelte might need to have some awareness of its surroundings is in dealing with @import
— if two of my components have SCSS in <style>
tags that both @import
the same file, then the only way we can avoid duplicating that code is if Svelte understands that concept.
But I want to keep that stuff out of the core to the greatest degree possible while still supporting these use cases. For one thing, supporting compile-to-X languages is a source of fragmentation — if your component uses SCSS, then my app can't import it unless I configure my compiler instance to deal with your authoring decisions.
Ultimately, I want preprocessors to die. I used to use them all the time, but eventually I came to the realisation that 90% of what I was using them for was nesting, which is kind of unnecessary when you have styles that are scoped to the current component. 9% was variables, which is a problem that can be solved in other ways, I think. The last 1% was stuff like mixins, which are theoretically useful but which I found myself having to consult documentation for every single time. The thing that finally convinced me to stop using preprocessors was the realisation that I was wasting so much time tweaking styles in devtools then manually converting the resulting styles back into SCSS (replacing colours with variables, etc etc) in order to paste them back into my source code. When you just use CSS, there's so much less friction.
But there are lots of people for whom preprocessors clearly are useful, and I think Svelte should make it possible to support those workflows, while not explicitly encouraging them.
Ultimately, I want preprocessors to die. I used to use them all the time, but eventually I came to the realisation that 90% of what I was using them for was nesting, which is kind of unnecessary when you have styles that are scoped to the current component. 9% was variables, which is a problem that can be solved in other ways, I think. The last 1% was stuff like mixins, which are theoretically useful but which I found myself having to consult documentation for every single time.
That's like arguing that JavaScript sucks, basing it only on your experiences with jQuery.
When you just use CSS, there's so much less friction.
That's like arguing vanilla JS is so much easier than React or Svelte because all you ever use JavaScript for is a bunch of Ajax calls and click events.
That's like arguing that JavaScript sucks, basing it only on your experiences with jQuery.
I've used SCSS, LESS and postcss extensively.
I've used SCSS, LESS and postcss extensively.
You said yourself that 90% of what you used it for was nesting and 9% was variables.
That's the CSS preprocessor equivalent of only writing basic Ajax calls & click handlers with jQuery.
a) I mean that's what they turned out to be good for, when I reviewed my usage of them, and b) don't take the numbers super literally. I was just making a point. The upshot here is that having used preprocessors a lot over the years, I'm much happier writing plain CSS.
I'd implore you to think a little bit before making some of these comments, they're coming off as very patronising.
I'm just saying you can't judge a language or framework if you've been using <5% of its functionality >95% of the time.
SCSS is a pretty powerful language that's unfortunately totally underused beyond the most basic features because most people using it are designers who barely know how to program and most JavaScript programmers don't bother to learn most of the language's features because they're too busy writing JavaScript code.
Now, if you were talking about JS preprocessors, I'd agree. I never quite understood the popularity of TypeScript or Babel, really. ES5 + a few polyfilled ES6 features here and there can do the job quite fine, and all TypeScript and Babel do, IMO, is add a bunch of unnecessary abstractions between the code you write and the code that's run in the browser that does more to cause headaches than to prevent them.
I guess it all depends on your very personal experience and taste. And again, sorry if my comments come off more antagonizing than intended. Having discussions without getting under people's skin isn't one on my talents... especially online.
@jslegers mind sharing some of the usage that can demonstrate that it is powerful, but underused or gets too little attention?
For example, I'm thinking about writing stuff like this...
.s1 {
@include rules((
float : right,
width : 30%,
background : #fff,
));
}
.s2 {
@include rules((
float : right,
width : 100%,
background : #000,
));
}
.s3 {
@include rules((
float : left,
width : 30%,
background : #fff,
));
}
.s4 {
@include rules((
float : left,
width : 40%,
background : 'url(image/cool-background.jpg)',
));
}
... to produce output like this :
.s1, .s2 {
float: right; }
.s1, .s3 {
width: 30%; }
.s1, .s3 {
background: #fff; }
.s2 {
width: 100%; }
.s2 {
background: #000; }
.s3, .s4 {
float: left; }
.s4 {
width: 40%; }
.s4 {
background: "url(image/cool-background.jpg)"; }
See https://gist.github.com/jslegers/9805919 for the complete demo.
These are my reasons and preferences for use case and feedback purposes.
I actually agree that a great deal of what preprocessors were both useful for and perceived as useful for in the past is either no longer relevant, or in my opinion, turned out to be not quite worth the trouble.
&:hover
, because nesting crossed the line into bad for style architecture real quick in global CSS. I find that after creating UI component systems that integrate and isolate the behavior and appearance of a GUI pattern that you have to approach things a bit differently than you did a CSS Framework. But the previous approach is of course the source of many preprocessor features.
Personally I still want to use a preprocessor along the lines of PostCSS and a plugin like postcss-cssnext. Svelte is already taking an analogous approach to that setup with the JavaScript in its modules by using import
and export,
features not natively supported by all browsers and which are being compiled. Granted, there are critical distinctions between transpiling "future CSS" and transpiling "future JS" due to differences in how the standards for each evolve as well as in CSS being a language whose future features are more often not unable to be reproduced using current CSS alone unless browsers have implemented with vendor prefixes.
There are few more reasons that matter to me:
Initially I thought I could use the CLI options to output a stylesheet and run PostCSS on it, which I thought was good enough for now. But now I hear there will be errors and realize it will change the experience for component users, so maybe not.
Of the ideas proposed,
...perhaps our little Svelte community should tackle the problem of real build tools like Webpack, Browserify, Rollup, et cetra, lacking the necessary plugins/loaders to process HTML modules. (For example, as I previously explained, Webpack has less-loader, but I couldn't find a loader that could be applied to an HTML file with inlined LESS). This is a relatively easy problem for just about anybody to solve, as it only requires knowledge of the build tool and some string manipulation. It is way easier than working on the source of the Svelte compiler. - @therealnicksaunders
That makes sense. I've come across plugins that do similar things in the past. But it seems to me from the outside that the most appropriate time to preprocess the style is during the build runtime after Svelte has extracted the style from the input HTML source file, but before Svelte places the stringified style in the output JS distribution file. The other options of doing something either before or after Svelte's process looks less than ideal right now...
It would make sense to introduce an official compiler-plugin api to svelte-cli which allows to process compilation steps. - @dschulten
I like when tools are extensible and flexible, so I like this idea. But I also note that there sure is a proliferation of small build plugins that exist only to glue another tool into Gulp/Webpack/Rollup/etc. It'd sure be nice if it could re-use the plugins from one or more existing ecosystems rather than having a unique plugin API--or didn't necessarily need a plugin to use modules like node-sass or postcss.
While you're considering this type of thing in terms of CSS preprocessing and alternative JavaScript syntaxes, I'd like to mention PostHTML. Svelte templates have their own syntax and outside of templating purposes, processing HTML is not as popular, but I hope there could be symmetry that will enable HTML pre/post-processing in addition to CSS and JS. It could allow some potential feature requests (e.g. minification, global custom attributes) to be served by third parties rather than Svelte itself.
- I only cared for nesting selectors like &:hover, because nesting crossed the line into bad for style architecture real quick in global CSS.
@at-root
to the rescue!
If you prefer BEM-like syntax to nested classes, you could use the @at-root
directive to nest your SCSS without nesting the CSS output it produces.
So, you could do shit like this :
$theme : (
simple : simple,
xmas : theme-xmas
);
.form {
@at-root .form__input {
border: black;
}
@at-root .form__submit {
background: black;
@at-root .form__submit--disabled {
background: grey;
}
}
$simple : map-get($theme, simple);
@at-root .form--#{$simple} {
color: black;
}
$xmas : map-get($theme, xmas);
@at-root .form--#{$xmas} {
color: red;
@at-root .form__input--#{$xmas} {
border: red;
}
@at-root .form__submit--#{$xmas} {
background: green;
@at-root .form__submit--disabled--#{$xmas} {
background: lightgreen;
}
}
}
}
That would produce this :
.form__input {
border: black; }
.form__submit {
background: black; }
.form__submit--disabled {
background: grey; }
.form--simple {
color: black; }
.form--theme-xmas {
color: red; }
.form__input--theme-xmas {
border: red; }
.form__submit--theme-xmas {
background: green; }
.form__submit--disabled--theme-xmas {
background: lightgreen; }
Personally, I prefer a maximum of 3 levels of nesting, using simple, semantically named class based selectors + a select few ID based selectors when I want to get really specific. BEM syntax is way too ugly & convoluted for my taste, but some big names in the CSS world seem to love it for some reason...
- "Native" CSS variables can be a feasible choice now.
Not in the real world, where having to support IE is still a real thing.
Also, variables in CSS behave very differently from variables in LESS or Sass and IMO pretty counter-intuitively. Personally, I'm not inclined to use them any time soon!
- The perceived modularity of a Sass partial is more often than not an illusion (meh to "components"-via-information-architecture).
I'd agree that SCSS is missing a few features to make it truly dynamic, like conditional @import
s within control directives and/or mixins? I've been waiting for this feature to be implemented for 3 years now!
To make things worse, the core devs don't seem to care about making SCSS more dynamic in spite of many, many people requesting such features. I feel like they're holding their own language back...
While you're considering this type of thing in terms of CSS preprocessing and alternative JavaScript syntaxes, I'd like to mention PostHTML. Svelte templates have their own syntax and outside of templating purposes, processing HTML is not as popular, but I hope there could be symmetry that will enable HTML pre/post-processing in addition to CSS and JS. It could allow some potential feature requests (e.g. minification, global custom attributes) to be served by third parties rather than Svelte itself.
Being able to choose whether or how to pre-process JS, CSS & HTML by just dropping in your preferred plugins (with minimal configuration) sure does sound like a killer feature to me!
What about playing with the type
attribute of <style />
tag somehow? This can lead to an elegant API for developers IMHO. For example for SCSS:
<!-- defaulted to text/css -->
<style type="text/x-scss">
header {
h1 {
color: #BADA55;
}
}
</style>
Then, either natively embedded preprocessors or a manually added ones via plugins could transpile it at compile time.
This could be also used for <script />
tags actually. For example for TypeScript:
<!-- defaulted to application/javascript -->
<script type="application/x-typescript">
function greeter(person: string) {
return "Hello, " + person;
}
</script>
Basically, one media type for one preprocessor.
I haven't yet thought of all the technical involvements that would be needed (nor checked what's currently being done to handle these transpilers TBH), and I'm very new to Svelte, but I just wanted to share my thoughts ;)
What about playing with the type attribute of tag somehow? This can lead to an elegant API for developers IMHO.
Agreed, if it's useful for whatever gets done. It's an interesting idea that feels very "native." The type
attribute for both script
and style
has been primarily useless redundancy since there's been one ubiquitous language for each within the browser. Intriguing to think of it as a compiler instruction instead.
@Rich-Harris, any thoughts concerning my previous comment? I may try to PoC it on my fork, but only if nothing else is in progress already concerning these preprocessors ;)
As of 1.44, Svelte comes with a preprocess
function, which provides a standard way to transform components before they get compiled:
const processed = await svelte.preprocess(source, {
markup: ({ content }) => {
// `content` is the entire component string
return { code: '...', map: {...} };
},
style: ({ content, attributes }) => {
// `content` is what's inside the <style> element, if present
// `attributes` is a map of attributes on the element
if (attributes.type !== 'text/scss') return;
return { code: '...', map: {...} };
},
script: ({ content, attributes }) => {
// `content` is what's inside the <script> element, if present
// `attributes` is a map of attributes on the element
if (attributes.type !== 'text/coffeescript') return;
return { code: '...', map: {...} };
}
});
The style
and script
preprocessors will run after the markup
preprocessor. Each preprocessor can return a) nothing (in which case no transformation takes place), b) a { code, map }
object, or c) a Promise that resolves to a) or b). Note that sourcemaps are currently discarded, but will be used in future versions of Svelte.
This is separated from the compiler by design — it allows (for example) component authors to write components using SASS or whatever, but still ship uncompiled 'vanilla' components (so that consumers of those components don't need to know what languages were used). It also means that svelte.compile
retains its synchronous API.
I don't envisage that most people will use svelte.preprocess
directly, but rather something like this (note: svelte-preprocess-postcss doesn't exist yet!). Substitute [your-preferred-tool] for Rollup:
// rollup.config.js
import svelte from 'rollup-plugin-svelte';
import postcss from 'svelte-preprocess-postcss';
export default {
// ...
plugins: [
svelte({
preprocess: {
style: postcss({ plugins: [...] })
}
})
]
};
If anyone is interested in helping out with official svelte-preprocess-[thing]
modules, shout!
Update: rollup-plugin-svelte now supports the preprocess option.
That's awesome, great work @Rich-Harris, thanks for working on this!
Because a simple reaction just isn't enough for this one:
😀
At @lukeed's request, I put together a small demo:
I feel special 😚 Thanks for the quick demo
Awesome, nice work!
Great work @esarbanis and @Rich-Harris!
P.S. this is a slight adaptation of the rollup.config.js contained in the demo using PostCSS instead of Sass: https://gist.github.com/marianoviola/c9bbf5e80359df7d109bf73476475c2e
Anybody has a sample webpack.config for the above plugin?
I totally forgot to do this... almost a year and a half ago now...
https://github.com/sveltejs/svelte/issues/181#issuecomment-274366135
Yeah I was planning to do that but wanted to check if somebody has. Also I tried to do the below only to realize the extracted file is css and not .scss or .less A.
Or
B.
For anyone lurking around this comment:
Check this out: https://github.com/kaisermann/svelte-preprocess
Here's an interesting problem:
I've written a custom transition that's using mask-image
. Is there any way to get style preprocessor to understand css created by the transition compiler? 'cos right now I need to add some browser-prefixed variants manually..
e.g. I have a transition which must return:
return `mask-image: linear-gradient(${stops.join(",")});-webkit-mask-image: linear-gradient(${stops.join(",")});`
...rather than:
return `mask-image: linear-gradient(${stops.join(",")});`
Would be nice if svelte-preprocess-postcss and the like could find this css and process it.
Just wondering how Svelte ties in to existing CSS preprocessors? I'm not seeing any documentation around this, and I'm not expecting it to just magically work if I add less or scss content to my style tags.
This would mainly be useful when using existing frameworks like Bootstrap.