Skip to content

Commit bb31a34

Browse files
carlosruanaGatsbyJS Bot
authored and
GatsbyJS Bot
committed
feat(gatsby-plugin-sass): add option to enable resolve-url-loader (#14272)
* Using resolve-url-loader if configured in the options * Documenting how to use resolve-url-plugin plugin * Fixing small typo * Fixing small typo * Fixing prettier checks * Using backtick quotes for strings * resolve-url-loader needs sass-loader to have sourceMap activated * Updating documentation * Updating documentation about sourceMap * chore: format * Update README.md * Update README.md Running prettier on README * chore: format
1 parent d652496 commit bb31a34

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

packages/gatsby-plugin-sass/README.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,62 @@ in the plugin options.
116116

117117
This plugin resolves `url()` paths relative to the entry SCSS/Sass file not – as might be expected – the location relative to the declaration. Under the hood, it makes use of [sass-loader](https://github.com/webpack-contrib/sass-loader/blob/master/README.md#problems-with-url) and this is documented in the [readme](https://github.com/webpack-contrib/sass-loader/blob/master/README.md#problems-with-url).
118118

119-
Using [resolve-url-loader](https://github.com/bholloway/resolve-url-loader) may provide a workaround, but at present this is not in the build and implementation would demand customization.
119+
Using [resolve-url-loader](https://github.com/bholloway/resolve-url-loader) provides a workaround, if you want to use relative url just install the plugin and then add it to your sass plugin options configuration.
120120

121-
<!-- TODO link to a plugin that adds resolve-url-loader -->
121+
First:
122+
123+
```javascript
124+
npm install resolve-url-loader --save-dev
125+
or
126+
yarn add resolve-url-loader --dev
127+
```
128+
129+
And then:
130+
131+
```javascript
132+
plugins: [
133+
{
134+
resolve: "gatsby-plugin-sass",
135+
options: {
136+
useResolveUrlLoader: true,
137+
},
138+
},
139+
]
140+
```
141+
142+
You can also configure resolve-url-plugin providing some options (see plugin documentation for all options https://github.com/bholloway/resolve-url-loader):
143+
144+
```javascript
145+
plugins: [
146+
{
147+
resolve: "gatsby-plugin-sass",
148+
options: {
149+
useResolveUrlLoader: {
150+
options: {
151+
debug: true,
152+
},
153+
},
154+
},
155+
},
156+
]
157+
```
158+
159+
NOTE that adding resolve-url-loader will use `sourceMap: true` on sass-loader (as it is required for the plugin to work), you can then activate/deactivate source-map for sass files in the plugin:
160+
161+
```javascript
162+
plugins: [
163+
{
164+
resolve: "gatsby-plugin-sass",
165+
options: {
166+
useResolveUrlLoader: {
167+
options: {
168+
sourceMap: true, //default is false
169+
},
170+
},
171+
},
172+
},
173+
]
174+
```
122175

123176
## Breaking changes history
124177

packages/gatsby-plugin-sass/src/gatsby-node.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import resolve from "./resolve"
22

33
exports.onCreateWebpackConfig = (
44
{ actions, stage, rules, plugins, loaders },
5-
{ cssLoaderOptions = {}, postCssPlugins, ...sassOptions }
5+
{ cssLoaderOptions = {}, postCssPlugins, useResolveUrlLoader, ...sassOptions }
66
) => {
77
const { setWebpackConfig } = actions
88
const PRODUCTION = stage !== `develop`
@@ -11,7 +11,7 @@ exports.onCreateWebpackConfig = (
1111
const sassLoader = {
1212
loader: resolve(`sass-loader`),
1313
options: {
14-
sourceMap: !PRODUCTION,
14+
sourceMap: useResolveUrlLoader ? true : !PRODUCTION,
1515
...sassOptions,
1616
},
1717
}
@@ -27,6 +27,12 @@ exports.onCreateWebpackConfig = (
2727
sassLoader,
2828
],
2929
}
30+
if (useResolveUrlLoader && !isSSR) {
31+
sassRule.use.splice(-1, 0, {
32+
loader: `resolve-url-loader`,
33+
options: useResolveUrlLoader.options ? useResolveUrlLoader.options : {},
34+
})
35+
}
3036
const sassRuleModules = {
3137
test: /\.module\.s(a|c)ss$/,
3238
use: [

0 commit comments

Comments
 (0)