Setting a sass includePath to a sub-directory #297
Replies: 2 comments 2 replies
-
(oops, one improvement already, realized there was no purpose at all to the ruby |
Beta Was this translation helpful? Give feedback.
-
Nothing wrong with configuring A generic approach to use "absolute" paths, is to leverage import aliases as mentioned in the docs. For example, if you add: export default defineConfig({
resolve: {
alias: {
'@stylesheets': resolve(__dirname, 'app/stylesheets'),
},
},
}) You should then be able to use: @import '@stylesheets/example' The advantage is that this works for both CSS and JS files, regardless of which pre-processor is used. When importing files from outside the |
Beta Was this translation helpful? Give feedback.
-
It took me a while to figure this out, so I figured I'd document it for others who might be interested, and I'm also interested in feedback:
Context
I have a Rails app, where I am working on using vite for both JS and CSS, including sass/SCSS. My source files end up set up like this:
In
application.scss
, I wants to do an SCSS@import
of_example.scss
. (along with many more similar)This does work:
But I was not really loving the need to put that relative
../
in there every time. And no kind of absolute path at all seemed to work.A Solution
If we can set SASS load paths to a directory we wanted, can we get just
@import 'example'
to work? That's basically what was going on in the webpacker setup I am porting from, can we match it?It turns out, yes, like this, with the somewhat obscure/under-documented vite config setting, and some js for calculating relative paths to config file.
Bingo, it seems to work!
./app/frontend/entrypoints/application.scss
can just@import 'example'
, and it now successfully imports./app/frontend/stylesheets/_example.scss
This is similar/analogous to how things were working under webpacker for me.
Feedback?
I am new to vite and not an expert in javascript build processes generally!
Is this a bad idea? Should i just resign myself to only doing SCSS imports with relative paths like
@import '../stylesheets/example'
?Or is there a better way to set something like this up? Appreciate any feedback!
Beta Was this translation helpful? Give feedback.
All reactions