Skip to content

Commit 5fcaf86

Browse files
author
Patrick Taddey
committed
Added the possibility to configure the StyleLoader via the method Encore.configureStyleLoader()
1 parent bdf8ba4 commit 5fcaf86

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,27 @@ class Encore {
965965
return this;
966966
}
967967

968+
/**
969+
* Configure the css-loader.
970+
*
971+
* https://github.com/webpack-contrib/style-loader#Options
972+
*
973+
* ```
974+
* Encore.configureStyleLoader(function(config) {
975+
* // change the config
976+
* // config.injectType: 'styleTag'
977+
* });
978+
* ```
979+
*
980+
* @param {function} callback
981+
* @returns {Encore}
982+
*/
983+
configureStyleLoader(callback) {
984+
webpackConfig.configureStyleLoader(callback);
985+
986+
return this;
987+
}
988+
968989
/**
969990
* If enabled, the react preset is added to Babel.
970991
*

lib/WebpackConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class WebpackConfig {
142142
this.babelConfigurationCallback = () => {};
143143
this.babelPresetEnvOptionsCallback = () => {};
144144
this.cssLoaderConfigurationCallback = () => {};
145+
this.styleLoaderConfigurationCallback = () => {};
145146
this.splitChunksConfigurationCallback = () => {};
146147
this.watchOptionsConfigurationCallback = () => {};
147148
this.devServerOptionsConfigurationCallback = () => {};
@@ -468,6 +469,14 @@ class WebpackConfig {
468469
this.cssLoaderConfigurationCallback = callback;
469470
}
470471

472+
configureStyleLoader(callback) {
473+
if (typeof callback !== 'function') {
474+
throw new Error('Argument 1 to configureStyleLoader() must be a callback function.');
475+
}
476+
477+
this.styleLoaderConfigurationCallback = callback;
478+
}
479+
471480
enableSingleRuntimeChunk() {
472481
this.shouldUseSingleRuntimeChunk = true;
473482
}

lib/loaders/css-extract.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const WebpackConfig = require('../WebpackConfig'); //eslint-disable-line no-unused-vars
1313
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
14+
const applyOptionsCallback = require('../utils/apply-options-callback');
1415

1516
module.exports = {
1617
/**
@@ -22,13 +23,15 @@ module.exports = {
2223
*/
2324
prependLoaders(webpackConfig, loaders) {
2425
if (!webpackConfig.extractCss) {
26+
27+
const options = {};
28+
2529
// If the CSS extraction is disabled, use the
2630
// style-loader instead.
2731
return [{
2832
loader: 'style-loader',
29-
options: {
30-
sourceMap: webpackConfig.useSourceMaps,
31-
}
33+
options: applyOptionsCallback(webpackConfig.styleLoaderConfigurationCallback, options)
34+
3235
}, ...loaders];
3336
}
3437

0 commit comments

Comments
 (0)