Custom import resolution
bep opened this issue · 3 comments
First, thanks for this library, it works great.
I'm the lead maintainer of Hugo (https://github.com/gohugoio/hugo) and I'm about to add SASS support to Hugo via go-libsass/libsass. I hope we can switch out that with a native Go library eventually, but after thinking hard, I concluded that I/we really need this now.
I have a very convincing demo of this working in Hugo (https://twitter.com/GoHugoIO/status/1007361748201037824) -- but I have one problem that I would really like to solve to get this where I want it.
In Hugo we have a layered and virtual filesystem with a well defined order (project, themes). Setting IncludePaths
with a list of these directories works mostly as I want, but LibSass seems to always prefer the local relative import if present.
I know which import should be picked, so if I could hook in a "import resolver" func given the file path and import path, I could provide the correct import filename.
Looking at the LibSass API, it looks like this should be possible, but I would appreciate any help/hint in this area.
I went down the native Go route, but Sass is not a compiler friendly language. That project consumed all my time for modest gains in language support. You can see where that ended here: https://github.com/wellington/sass
Having said that, I've done a fair bit to speed up the C compiling. You can obviously just install the pkg and forget about it. However, you may be interested in the -dev
buildtag if you need to tinker in the code. It will use a system installed libsass lib instead of compiling it. Try to match the included one as closely as possible.
There are go hooks to the libsass importer here: https://github.com/wellington/go-libsass/blob/master/importer.go#L109-L129 or the low level call: https://github.com/wellington/go-libsass/blob/master/libs/importer.go#L38 I use this for various things, adding magic sass code (blah), and implementing custom functions to support spritewell.
OK, thanks -- I will have a look at it.
I'll reopen this issue to track a forthcoming PR to add a callback interface for custom import resolution. I have tested it on a simple and hardcoded setup, and it works. We can discuss further in the PR.