Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the possibility to configure the StyleLoader via the method Enc… #715

Merged
merged 10 commits into from
Apr 17, 2020
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,27 @@ class Encore {
return this;
}

/**
* Configure the style-loader.
*
* https://github.com/webpack-contrib/style-loader#Options
*
tooltonix marked this conversation as resolved.
Show resolved Hide resolved
* ```
* Encore.configureStyleLoader(function(config) {
* // change the config
* // config.injectType = 'styleTag';
tooltonix marked this conversation as resolved.
Show resolved Hide resolved
* });
* ```
*
* @param {function} callback
* @returns {Encore}
*/
configureStyleLoader(callback) {
tooltonix marked this conversation as resolved.
Show resolved Hide resolved
webpackConfig.configureStyleLoader(callback);

return this;
}

/**
* If enabled, the react preset is added to Babel.
*
Expand Down
9 changes: 9 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class WebpackConfig {
this.babelConfigurationCallback = () => {};
this.babelPresetEnvOptionsCallback = () => {};
this.cssLoaderConfigurationCallback = () => {};
this.styleLoaderConfigurationCallback = () => {};
this.splitChunksConfigurationCallback = () => {};
this.watchOptionsConfigurationCallback = () => {};
this.devServerOptionsConfigurationCallback = () => {};
Expand Down Expand Up @@ -468,6 +469,14 @@ class WebpackConfig {
this.cssLoaderConfigurationCallback = callback;
}

configureStyleLoader(callback) {
if (typeof callback !== 'function') {
throw new Error('Argument 1 to configureStyleLoader() must be a callback function.');
}

this.styleLoaderConfigurationCallback = callback;
}

enableSingleRuntimeChunk() {
this.shouldUseSingleRuntimeChunk = true;
}
Expand Down
11 changes: 8 additions & 3 deletions lib/loaders/css-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

module.exports = {
/**
Expand All @@ -22,13 +23,17 @@ module.exports = {
*/
prependLoaders(webpackConfig, loaders) {
if (!webpackConfig.extractCss) {

const options = {
sourceMap: webpackConfig.useSourceMaps,
tooltonix marked this conversation as resolved.
Show resolved Hide resolved
};

tooltonix marked this conversation as resolved.
Show resolved Hide resolved
// If the CSS extraction is disabled, use the
// style-loader instead.
return [{
loader: 'style-loader',
options: {
sourceMap: webpackConfig.useSourceMaps,
tooltonix marked this conversation as resolved.
Show resolved Hide resolved
}
options: applyOptionsCallback(webpackConfig.styleLoaderConfigurationCallback, options)

}, ...loaders];
}

Expand Down