Skip to content

Commit 0168b0b

Browse files
committed
feature #715 Added the possibility to configure the StyleLoader via the method Enc… (Patrick Taddey)
This PR was squashed before being merged into the master branch. Discussion ---------- Added the possibility to configure the StyleLoader via the method Enc… …ore.configureStyleLoader() Commits ------- 55aca97 try to fix the linter errors 287abf8 try to fix the linter errors 8c82bd2 wrote a test for the styleloader-configuration (configureStyleLoader) db6d606 moved function 'configureStyleLoader' close to function 'disableCssExtraction' because they belong together(callback) { 232fe2b comments of the StyleLoader function changed 0d015b2 Removed default options for the StyleLoader 5b585f1 Konflikte die mit dem Master/Upstream bestanden behoben ad78f74 Include sourcemaps in the default options 524af82 Corrected comments 5fcaf86 Added the possibility to configure the StyleLoader via the method Encore.configureStyleLoader()
2 parents e81454e + 55aca97 commit 0168b0b

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,28 @@ class Encore {
12941294
return this;
12951295
}
12961296

1297+
/**
1298+
* Configure the style-loader.
1299+
* The style-loader is used only if you also call Encore. disableCssExtraction().
1300+
*
1301+
* https://github.com/webpack-contrib/style-loader#options
1302+
*
1303+
* ```
1304+
* Encore.configureStyleLoader(function(config) {
1305+
* // change the config
1306+
* // config.injectType = 'singletonStyleTag';
1307+
* });
1308+
* ```
1309+
*
1310+
* @param {function} callback
1311+
* @returns {Encore}
1312+
*/
1313+
configureStyleLoader(callback) {
1314+
webpackConfig.configureStyleLoader(callback);
1315+
1316+
return this;
1317+
}
1318+
12971319
/**
12981320
* Call this to change how the name of each output
12991321
* file is generated.

lib/WebpackConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class WebpackConfig {
146146
this.babelConfigurationCallback = () => {};
147147
this.babelPresetEnvOptionsCallback = () => {};
148148
this.cssLoaderConfigurationCallback = () => {};
149+
this.styleLoaderConfigurationCallback = () => {};
149150
this.splitChunksConfigurationCallback = () => {};
150151
this.watchOptionsConfigurationCallback = () => {};
151152
this.devServerOptionsConfigurationCallback = () => {};
@@ -472,6 +473,14 @@ class WebpackConfig {
472473
this.cssLoaderConfigurationCallback = callback;
473474
}
474475

476+
configureStyleLoader(callback) {
477+
if (typeof callback !== 'function') {
478+
throw new Error('Argument 1 to configureStyleLoader() must be a callback function.');
479+
}
480+
481+
this.styleLoaderConfigurationCallback = callback;
482+
}
483+
475484
enableSingleRuntimeChunk() {
476485
this.shouldUseSingleRuntimeChunk = true;
477486
}

lib/loaders/css-extract.js

Lines changed: 6 additions & 0 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,10 +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',
33+
options: applyOptionsCallback(webpackConfig.styleLoaderConfigurationCallback, options)
34+
2935
}, ...loaders];
3036
}
3137

test/functional.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,6 +2913,33 @@ module.exports = {
29132913
done();
29142914
});
29152915
});
2916+
2917+
it('With CSS extraction disabled and with options callback of the StyleLoader', (done) => {
2918+
const config = createWebpackConfig('build', 'dev');
2919+
config.setPublicPath('/build');
2920+
config.disableSingleRuntimeChunk();
2921+
config.addEntry('main', './js/css_import');
2922+
config.disableCssExtraction();
2923+
config.configureStyleLoader((options) => {
2924+
options.attributes = { id: 'TESTING_ATTRIBUTES' };
2925+
});
2926+
2927+
testSetup.runWebpack(config, (webpackAssert) => {
2928+
expect(config.outputPath).to.be.a.directory()
2929+
.with.files([
2930+
'manifest.json',
2931+
'entrypoints.json',
2932+
'main.js'
2933+
]);
2934+
2935+
webpackAssert.assertOutputFileContains(
2936+
'main.js',
2937+
'TESTING_ATTRIBUTES'
2938+
);
2939+
2940+
done();
2941+
});
2942+
});
29162943
});
29172944

29182945
if (!process.env.DISABLE_UNSTABLE_CHECKS) {

0 commit comments

Comments
 (0)