Skip to content

Commit c7dd577

Browse files
authored
Merge pull request #63 from weaverryan/allow-disable-resolve-url-loader
Allowing the resolve_url_loader to be disabled (for performance reasons)
2 parents 1353fcb + df83f98 commit c7dd577

File tree

6 files changed

+85
-18
lines changed

6 files changed

+85
-18
lines changed

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,20 @@ module.exports = {
232232
/**
233233
* Call this if you plan on loading SASS files.
234234
*
235+
* Supported options:
236+
* * {bool} resolve_url_loader (default=true)
237+
* Whether or not to use the resolve-url-loader.
238+
* Setting to false can increase performance in some
239+
* cases, especially when using bootstrap_sass. But,
240+
* when disabled, all url()'s are resolved relative
241+
* to the original entry file... not whatever file
242+
* the url() appears in.
243+
*
244+
* @param {object} options
235245
* @return {exports}
236246
*/
237-
enableSassLoader() {
238-
webpackConfig.enableSassLoader();
247+
enableSassLoader(options = {}) {
248+
webpackConfig.enableSassLoader(options);
239249

240250
return this;
241251
},

lib/WebpackConfig.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class WebpackConfig {
4040
this.useSourceMaps = false;
4141
this.usePostCssLoader = false;
4242
this.useSassLoader = false;
43+
this.sassOptions = {
44+
resolve_url_loader: true
45+
};
4346
this.useLessLoader = false;
4447
this.cleanupOutput = false;
4548
this.sharedCommonsEntryName = null;
@@ -189,8 +192,16 @@ class WebpackConfig {
189192
this.usePostCssLoader = true;
190193
}
191194

192-
enableSassLoader() {
195+
enableSassLoader(options = {}) {
193196
this.useSassLoader = true;
197+
198+
for (const optionKey of Object.keys(options)) {
199+
if (!(optionKey in this.sassOptions)) {
200+
throw new Error(`Invalid option "${optionKey}" passed to enableSassLoader(). Valid keys are ${Object.keys(this.sassOptions).join(', ')}`);
201+
}
202+
203+
this.sassOptions[optionKey] = options[optionKey];
204+
}
194205
}
195206

196207
enableLessLoader() {

lib/config-generator.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,24 +191,29 @@ class ConfigGenerator {
191191
if (this.webpackConfig.useSassLoader) {
192192
loaderFeatures.ensureLoaderPackagesExist('sass');
193193

194+
const sassLoaders = [...cssLoaders];
195+
if (true === this.webpackConfig.sassOptions.resolve_url_loader) {
196+
// responsible for resolving SASS url() paths
197+
// without this, all url() paths must be relative to the
198+
// entry file, not the file that contains the url()
199+
sassLoaders.push({
200+
loader: 'resolve-url-loader' + this.getSourceMapOption(),
201+
});
202+
}
203+
204+
sassLoaders.push({
205+
loader: 'sass-loader',
206+
options: {
207+
// needed by the resolve-url-loader
208+
sourceMap: (true === this.webpackConfig.sassOptions.resolve_url_loader) || this.webpackConfig.useSourceMaps
209+
}
210+
});
211+
194212
rules.push({
195213
test: /\.s[ac]ss$/,
196214
use: ExtractTextPlugin.extract({
197215
fallback: 'style-loader' + this.getSourceMapOption(),
198-
use: [
199-
...cssLoaders,
200-
{
201-
// responsible for resolving SASS import paths
202-
loader: 'resolve-url-loader' + this.getSourceMapOption(),
203-
},
204-
{
205-
loader: 'sass-loader',
206-
options: {
207-
// always enabled, needed by resolve-url-loader
208-
sourceMap: true
209-
}
210-
},
211-
]
216+
use: sassLoaders
212217
})
213218
});
214219
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"less": "^2.7.2",
6060
"less-loader": "^4.0.2",
6161
"mocha": "^3.2.0",
62-
"node-sass": "^4.5.0",
62+
"node-sass": "^4.5.3",
6363
"nsp": "^2.6.3",
6464
"postcss-loader": "^1.3.3",
6565
"sass-loader": "^6.0.3",

test/WebpackConfig.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,29 @@ describe('WebpackConfig object', () => {
251251
}).to.throw('configureBabel() cannot be called because your app has a .babelrc file');
252252
});
253253
});
254+
255+
describe('enableSassLoader', () => {
256+
it('Call with no config', () => {
257+
const config = createConfig();
258+
config.enableSassLoader();
259+
260+
expect(config.useSassLoader).to.be.true;
261+
});
262+
263+
it('Pass valid config', () => {
264+
const config = createConfig();
265+
config.enableSassLoader({ resolve_url_loader: false });
266+
267+
expect(config.useSassLoader).to.be.true;
268+
expect(config.sassOptions.resolve_url_loader).to.be.false;
269+
});
270+
271+
it('Pass invalid config', () => {
272+
const config = createConfig();
273+
274+
expect(() => {
275+
config.enableSassLoader({ fake_option: false });
276+
}).to.throw('Invalid option "fake_option" passed to enableSassLoader()');
277+
});
278+
});
254279
});

test/config-generator.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,22 @@ describe('The config-generator function', () => {
305305
const actualConfig = configGenerator(config);
306306

307307
expect(JSON.stringify(actualConfig.module.rules)).to.contain('sass-loader');
308+
expect(JSON.stringify(actualConfig.module.rules)).to.contain('resolve-url-loader');
309+
// sourceMap option is needed for resolve-url-loader
310+
expect(JSON.stringify(actualConfig.module.rules)).to.contain('"sourceMap":true');
311+
});
312+
313+
it('enableSassLoader() without resolve_url_loader', () => {
314+
const config = createConfig();
315+
config.outputPath = '/tmp/output/public-path';
316+
config.publicPath = '/public-path';
317+
config.addEntry('main', './main');
318+
config.enableSassLoader({ resolve_url_loader: false });
319+
320+
const actualConfig = configGenerator(config);
321+
322+
expect(JSON.stringify(actualConfig.module.rules)).to.not.contain('resolve-url-loader');
323+
expect(JSON.stringify(actualConfig.module.rules)).to.contain('"sourceMap":false');
308324
});
309325
});
310326

0 commit comments

Comments
 (0)