-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Can I provide an 'import hook' to load files in a custom way? #470
Comments
This would require an unwelcome amount of juggling in node-sass, because the Sass compilation is done with https://github.com/sass/libsass, so the import hooks would need to be implemented in libsass and made available via the interface. The functionality you're looking for might be a better fit for rework or postcss (run before scss compilation), and a stream-based workflow (via gulp, perhaps) would do away with temporary files. |
I don't think you follow. Doing preprocessing (eg with rework) before SCSS compilation is exactly why I'm requesting this feature, because it's currently not possible to do it, except with 'entry' SCSS files. For imported partials, there's no way to preprocess them because libsass wants to read them directly from disk. There's no way to feed it the partials dynamically from my own stream. Or am I missing something? How would rework help me? If node-sass lets me feed in an entry file like I'm happy to request this on the libsass project and see where it gets me, but it sounds like you don't agree it's a good idea... if it got implemented in libsass would you be willing to expose it through node-sass? |
I did miss your point regarding how rework might help, but I think it makes sense to think of this as a libsass (rather than node-sass) feature request. Perhaps libsass could take a map with include paths as keys and the file contents as data? |
cool I agree it's a libsass request (now that I know more about it). just wanted to check you were potentially on board, if I ever manage to get the libsass authors to do this :) regarding passing in a map... I would rather be able to provide my own dynamic 'importer' so that I can decide when/whether to load (and prepreprocess) partials on-demand, rather than have to prepreprocess the whole project and pass the whole lot to libsass in one go. If I could provide my own import hook, this would allow me to put something very efficient together I think Ruby Sass has a feature like this (ie you can bring your own Importer class), so I'll read up on that and then pitch it to libsass... thanks |
I came across this posting because I would need this too... I want to be able to create the following behavior:
Is this somehow possible? |
@adick no, not with node-sass/libsass. Ruby Sass lets you provide your own import hook... it has a default Importer class that loads files from the filesystem according to the usual rules, but you can provide your own Importer by extending this Base class. Then Sass would never touch the filesystem itself (except maybe its own .sass-cache files), and instead would always go through your Importer. So you can do whatever you want – eg look for non-standard file extensions, maybe preprocess things first to inject some config from environment variables, whatever.. This is probably how the Rails asset pipeline lets you do things like |
Hi @callumlocke - thanks for the heads-up! I already managed to get it work by overriding the default
I had to override the default |
Ah cool, mine was just theory till now, hadn't seen a working example yet.
|
this is now being picked up in #530 |
s/Nathan/Natalie in Readme
node-sass already allows me to provide an SCSS string (as
data
) instead of a file path. But if there are@import
statements in that string, these get loaded from disk (using whateverloadPaths
I provide).I want to be able to provide some kind of 'importer' hook in the options - a custom function for loading file contents. This function would be called whenever libsass encounters an
@import
statement (so node-sass/libsass never actually touches the disk directly), and its job is to call back with the contents of the requested file.This would make it much more efficient to use node-sass in a multi-step preprocessing sequence - eg with
.scss.hbs
files, where the first step renders Handlebars templates to Sass, and then the second step renders the in-memory Sass to CSS. At the moment the only way I can do this is using a.tmp
directory.Is this possible?
The text was updated successfully, but these errors were encountered: