Skip to content

Commit 63e15ce

Browse files
committed
feature #335 Add option to configure the CSS loader (XWB)
This PR was merged into the master branch. Discussion ---------- Add option to configure the CSS loader Disabling the URL handler can be handy for importing legacy CSS. Fixes #334 Commits ------- 64935f1 Add css-loader configuration callback
2 parents 2a1a18a + 64935f1 commit 63e15ce

File tree

5 files changed

+70
-9
lines changed

5 files changed

+70
-9
lines changed

index.js

+19
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,25 @@ class Encore {
581581
return this;
582582
}
583583

584+
/**
585+
* Configure the css-loader.
586+
*
587+
* https://github.com/webpack-contrib/css-loader#options
588+
*
589+
* Encore.configureCssLoader(function(config) {
590+
* // change the config
591+
* // config.minimize = true;
592+
* });
593+
*
594+
* @param {function} callback
595+
* @returns {Encore}
596+
*/
597+
configureCssLoader(callback) {
598+
webpackConfig.configureCssLoader(callback);
599+
600+
return this;
601+
}
602+
584603
/**
585604
* If enabled, the react preset is added to Babel.
586605
*

lib/WebpackConfig.js

+9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class WebpackConfig {
8686
this.lessLoaderOptionsCallback = () => {};
8787
this.stylusLoaderOptionsCallback = () => {};
8888
this.babelConfigurationCallback = () => {};
89+
this.cssLoaderConfigurationCallback = () => {};
8990
this.vueLoaderOptionsCallback = () => {};
9091
this.eslintLoaderOptionsCallback = () => {};
9192
this.tsConfigurationCallback = () => {};
@@ -319,6 +320,14 @@ class WebpackConfig {
319320
this.babelConfigurationCallback = callback;
320321
}
321322

323+
configureCssLoader(callback) {
324+
if (typeof callback !== 'function') {
325+
throw new Error('Argument 1 to configureCssLoader() must be a callback function.');
326+
}
327+
328+
this.cssLoaderConfigurationCallback = callback;
329+
}
330+
322331
createSharedEntry(name, files) {
323332
// don't allow to call this twice
324333
if (this.sharedCommonsEntryName) {

lib/loaders/css.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ module.exports = {
2121
getLoaders(webpackConfig, skipPostCssLoader) {
2222
const usePostCssLoader = webpackConfig.usePostCssLoader && !skipPostCssLoader;
2323

24+
const options = {
25+
minimize: webpackConfig.isProduction(),
26+
sourceMap: webpackConfig.useSourceMaps,
27+
// when using @import, how many loaders *before* css-loader should
28+
// be applied to those imports? This defaults to 0. When postcss-loader
29+
// is used, we set it to 1, so that postcss-loader is applied
30+
// to @import resources.
31+
importLoaders: usePostCssLoader ? 1 : 0
32+
};
33+
2434
const cssLoaders = [
2535
{
2636
loader: 'css-loader',
27-
options: {
28-
minimize: webpackConfig.isProduction(),
29-
sourceMap: webpackConfig.useSourceMaps,
30-
// when using @import, how many loaders *before* css-loader should
31-
// be applied to those imports? This defaults to 0. When postcss-loader
32-
// is used, we set it to 1, so that postcss-loader is applied
33-
// to @import resources.
34-
importLoaders: usePostCssLoader ? 1 : 0
35-
}
37+
options: applyOptionsCallback(webpackConfig.cssLoaderConfigurationCallback, options)
3638
},
3739
];
3840

test/WebpackConfig.js

+17
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,23 @@ describe('WebpackConfig object', () => {
436436
});
437437
});
438438

439+
describe('configureCssLoader', () => {
440+
it('Calling method sets it', () => {
441+
const config = createConfig();
442+
const testCallback = () => {};
443+
config.configureCssLoader(testCallback);
444+
expect(config.cssLoaderConfigurationCallback).to.equal(testCallback);
445+
});
446+
447+
it('Calling with non-callback throws an error', () => {
448+
const config = createConfig();
449+
450+
expect(() => {
451+
config.configureCssLoader('FOO');
452+
}).to.throw('must be a callback function');
453+
});
454+
});
455+
439456
describe('enablePostCssLoader', () => {
440457
it('Call with no config', () => {
441458
const config = createConfig();

test/loaders/css.js

+14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ describe('loaders/css', () => {
4444
expect(actualLoaders[0].options.minimize).to.be.true;
4545
});
4646

47+
it('getLoaders() with options callback', () => {
48+
const config = createConfig();
49+
50+
config.configureCssLoader(function(options) {
51+
options.minimize = true;
52+
options.url = false;
53+
});
54+
55+
const actualLoaders = cssLoader.getLoaders(config);
56+
expect(actualLoaders).to.have.lengthOf(1);
57+
expect(actualLoaders[0].options.minimize).to.be.true;
58+
expect(actualLoaders[0].options.url).to.be.false;
59+
});
60+
4761
describe('getLoaders() with PostCSS', () => {
4862
it('without options callback', () => {
4963
const config = createConfig();

0 commit comments

Comments
 (0)