Skip to content

Commit f803b9a

Browse files
committed
Adding ability to pass options to sass-loader
1 parent c5c3d23 commit f803b9a

File tree

6 files changed

+90
-11
lines changed

6 files changed

+90
-11
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# CHANGELOG
22

3+
## 0.10.0
4+
5+
* [BC BREAK] If you're using `enableSassLoader()` AND passing an options
6+
array, the options now need to be moved to the second argument:
7+
8+
```js
9+
// before
10+
.enableSassLoader({ resolve_url_loader: true });
11+
12+
// after
13+
enableSassLoader(function(sassOptions) {}, {
14+
resolve_url_loader: true
15+
})
16+
```
17+
318
## 0.9.1
419

520
* Syntax error fix - #64

index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,18 @@ module.exports = {
282282
/**
283283
* Call this if you plan on loading SASS files.
284284
*
285+
* Encore.enableSassLoader();
286+
*
287+
* Or pass options to node-sass
288+
*
289+
* Encore.enableSassLoader(function(options) {
290+
* // https://github.com/sass/node-sass#options
291+
* // options.includePaths = [...]
292+
* }, {
293+
* // set optional Encore-specific options
294+
* // resolve_url_loader: true
295+
* });
296+
*
285297
* Supported options:
286298
* * {bool} resolve_url_loader (default=true)
287299
* Whether or not to use the resolve-url-loader.
@@ -291,11 +303,12 @@ module.exports = {
291303
* to the original entry file... not whatever file
292304
* the url() appears in.
293305
*
294-
* @param {object} options
306+
* @param {function} sassLoaderOptionsCallback
307+
* @param {object} encoreOptions
295308
* @return {exports}
296309
*/
297-
enableSassLoader(options = {}) {
298-
webpackConfig.enableSassLoader(options);
310+
enableSassLoader(sassLoaderOptionsCallback = () => {}, encoreOptions = {}) {
311+
webpackConfig.enableSassLoader(sassLoaderOptionsCallback, encoreOptions);
299312

300313
return this;
301314
},

lib/WebpackConfig.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class WebpackConfig {
4141
this.useSourceMaps = false;
4242
this.usePostCssLoader = false;
4343
this.useSassLoader = false;
44+
this.sassLoaderOptionsCallback = function() {};
4445
this.sassOptions = {
4546
resolve_url_loader: true
4647
};
@@ -206,9 +207,15 @@ class WebpackConfig {
206207
this.usePostCssLoader = true;
207208
}
208209

209-
enableSassLoader(options = {}) {
210+
enableSassLoader(sassLoaderOptionsCallback = () => {}, options = {}) {
210211
this.useSassLoader = true;
211212

213+
if (typeof sassLoaderOptionsCallback !== 'function') {
214+
throw new Error('Argument 1 to enableSassLoader() must be a callback function.');
215+
}
216+
217+
this.sassLoaderOptionsCallback = sassLoaderOptionsCallback;
218+
212219
for (const optionKey of Object.keys(options)) {
213220
if (!(optionKey in this.sassOptions)) {
214221
throw new Error(`Invalid option "${optionKey}" passed to enableSassLoader(). Valid keys are ${Object.keys(this.sassOptions).join(', ')}`);

lib/loaders/sass.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,21 @@ module.exports = {
3535
});
3636
}
3737

38+
let config = Object.assign({}, sassOptions, {
39+
// needed by the resolve-url-loader
40+
sourceMap: (true === webpackConfig.sassOptions.resolve_url_loader) || webpackConfig.useSourceMaps
41+
});
42+
43+
// allow options to be configured
44+
webpackConfig.sassLoaderOptionsCallback.apply(
45+
// use config as the this variable
46+
config,
47+
[config]
48+
);
49+
3850
sassLoaders.push({
3951
loader: 'sass-loader',
40-
options: Object.assign({}, sassOptions, {
41-
// needed by the resolve-url-loader
42-
sourceMap: (true === webpackConfig.sassOptions.resolve_url_loader) || webpackConfig.useSourceMaps
43-
}),
52+
options: config,
4453
});
4554

4655
return sassLoaders;

test/WebpackConfig.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ describe('WebpackConfig object', () => {
263263

264264
it('Pass valid config', () => {
265265
const config = createConfig();
266-
config.enableSassLoader({ resolve_url_loader: false });
266+
config.enableSassLoader(() => {}, { resolve_url_loader: false });
267267

268268
expect(config.useSassLoader).to.be.true;
269269
expect(config.sassOptions.resolve_url_loader).to.be.false;
@@ -273,9 +273,17 @@ describe('WebpackConfig object', () => {
273273
const config = createConfig();
274274

275275
expect(() => {
276-
config.enableSassLoader({ fake_option: false });
276+
config.enableSassLoader(() => {}, { fake_option: false });
277277
}).to.throw('Invalid option "fake_option" passed to enableSassLoader()');
278278
});
279+
280+
it('Pass options callback', () => {
281+
const config = createConfig();
282+
const callback = (sassOptions) => {};
283+
config.enableSassLoader(callback);
284+
285+
expect(config.sassLoaderOptionsCallback).to.equal(callback);
286+
});
279287
});
280288

281289
describe('enableTypeScriptLoader', () => {

test/loaders/sass.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('loaders/sass', () => {
6666

6767
it('getLoaders() without resolve-url-loader', () => {
6868
const config = createConfig();
69-
config.enableSassLoader({
69+
config.enableSassLoader(() => {}, {
7070
resolve_url_loader: false,
7171
});
7272
config.enableSourceMaps(false);
@@ -100,4 +100,31 @@ describe('loaders/sass', () => {
100100

101101
cssLoader.getLoaders.restore();
102102
});
103+
104+
it('getLoaders() with options callback', () => {
105+
const config = createConfig();
106+
107+
// make the cssLoader return nothing
108+
sinon.stub(cssLoader, 'getLoaders')
109+
.callsFake(() => []);
110+
111+
config.enableSassLoader(function(sassOptions) {
112+
sassOptions.custom_optiona = 'baz';
113+
sassOptions.other_option = true;
114+
});
115+
116+
const actualLoaders = sassLoader.getLoaders(config, {
117+
custom_optiona: 'foo',
118+
custom_optionb: 'bar'
119+
});
120+
121+
expect(actualLoaders[1].options).to.deep.equals({
122+
sourceMap: true,
123+
// callback wins over passed in options
124+
custom_optiona: 'baz',
125+
custom_optionb: 'bar',
126+
other_option: true
127+
});
128+
cssLoader.getLoaders.restore();
129+
});
103130
});

0 commit comments

Comments
 (0)