sass-loader icon indicating copy to clipboard operation
sass-loader copied to clipboard

Pass relative path using `resourcePath` to data option

Open plankguy opened this issue 10 years ago • 2 comments

I'm wondering how I can get the current sass file path (resourcePath in the loader src), to the data option to use in my sass file.

My use case is i'm trying to pass path.relative(...) from the sass file to the images folder, so I won't have to write url(../../../../dist/images/foo.jpg) in scss files. Ideally it might look something like this:

...
sassLoader: {
    includePaths: [
        path.resolve(__dirname, 'src/scss')
        , path.resolve(__dirname, 'craft/templates/components')
    ],
    outputStyle: ISPROD ? 'compressed' : 'expanded'
    , data: `
             $image-path: ${path.relative(resourcePath, 'src/images/')}; // no good :(
             $env: ${process.env.NODE_ENV};
            `
},
...

This way, no matter how deep a sass file might be in the folder structure, it'll have a relative path to the images folder - and hopefully url()'s can then be properly parsed by file/url loaders!

plankguy avatar Apr 27 '16 21:04 plankguy

You can try webpack's resolve.root option. Just set the dist folder as resolve.root and you should be able to just write url(images/foo.jpg).

I won't add any environment variables because this makes everything even a lot more hackier. The data option is already hacky enough.

jhnns avatar Jun 26 '16 10:06 jhnns

Sweet thanks.

plankguy avatar Jun 27 '16 18:06 plankguy