Skip to content

Commit b91f16a

Browse files
committed
feature #539 Add Encore.disableCssExtraction() to the public API (Lyrkan)
This PR was merged into the master branch. Discussion ---------- Add Encore.disableCssExtraction() to the public API This PR adds an `Encore.disableCssExtraction()` method that allows to disable the `mini-css-extract-plugin` and use the `style-loader` instead. It can be used to solve various problems that, until now, required a really ugly workaround that relied on our internal implementation (for instance the following commit probably broke some builds that used previous versions of it: 6867443#diff-8beacd21a12ca072bafa4e8e3f1aae6b). Related issues: #3, #256, #348, #527 Commits ------- 347feed Add Encore.disableCssExtraction() to the public API
2 parents 7bec574 + 347feed commit b91f16a

File tree

8 files changed

+111
-1
lines changed

8 files changed

+111
-1
lines changed

fixtures/js/css_import.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
require('./../css/h1_style.css');

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,22 @@ class Encore {
10461046
return this;
10471047
}
10481048

1049+
/**
1050+
* Call this if you don't want imported CSS to be extracted
1051+
* into a .css file. All your styles will then be injected
1052+
* into the page by your JS code.
1053+
*
1054+
* Internally, this disables the mini-css-extract-plugin
1055+
* and uses the style-loader instead.
1056+
*
1057+
* @returns {Encore}
1058+
*/
1059+
disableCssExtraction() {
1060+
webpackConfig.disableCssExtraction();
1061+
1062+
return this;
1063+
}
1064+
10491065
/**
10501066
* Call this to change how the name of each output
10511067
* file is generated.

lib/WebpackConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class WebpackConfig {
5353
this.useVersioning = false;
5454
this.useSourceMaps = false;
5555
this.cleanupOutput = false;
56+
this.extractCss = true;
5657
this.useImagesLoader = true;
5758
this.useFontsLoader = true;
5859
this.usePostCssLoader = false;
@@ -657,6 +658,10 @@ class WebpackConfig {
657658
this.useFontsLoader = false;
658659
}
659660

661+
disableCssExtraction() {
662+
this.extractCss = false;
663+
}
664+
660665
configureFilenames(configuredFilenames = {}) {
661666
if (typeof configuredFilenames !== 'object') {
662667
throw new Error('Argument 1 to configureFilenames() must be an object.');

lib/config-generator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ class ConfigGenerator {
397397
buildPluginsConfig() {
398398
const plugins = [];
399399

400-
miniCssExtractPluginUtil(plugins, this.webpackConfig);
400+
if (this.webpackConfig.extractCss) {
401+
miniCssExtractPluginUtil(plugins, this.webpackConfig);
402+
}
401403

402404
// register the pure-style entries that should be deleted
403405
deleteUnusedEntriesPluginUtil(plugins, this.webpackConfig);

lib/loaders/css-extract.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ module.exports = {
2020
* @return {Array}
2121
*/
2222
prependLoaders(webpackConfig, loaders) {
23+
if (!webpackConfig.extractCss) {
24+
// If the CSS extraction is disabled, use the
25+
// style-loader instead.
26+
return [{
27+
loader: 'style-loader',
28+
options: {
29+
sourceMap: webpackConfig.useSourceMaps,
30+
}
31+
}, ...loaders];
32+
}
33+
2334
return [MiniCssExtractPlugin.loader, ...loaders];
2435
}
2536
};

test/WebpackConfig.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,21 @@ describe('WebpackConfig object', () => {
10331033
});
10341034
});
10351035

1036+
describe('disableCssExtraction', () => {
1037+
it('By default the CSS extraction is enabled', () => {
1038+
const config = createConfig();
1039+
1040+
expect(config.extractCss).to.be.true;
1041+
});
1042+
1043+
it('Calling it disables the CSS extraction', () => {
1044+
const config = createConfig();
1045+
config.disableCssExtraction();
1046+
1047+
expect(config.extractCss).to.be.false;
1048+
});
1049+
});
1050+
10361051
describe('configureFilenames', () => {
10371052
it('Calling method sets it', () => {
10381053
const config = createConfig();

test/functional.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,5 +2204,55 @@ module.exports = {
22042204
});
22052205
});
22062206
});
2207+
2208+
describe('CSS extraction', () => {
2209+
it('With CSS extraction enabled', (done) => {
2210+
const config = createWebpackConfig('build', 'dev');
2211+
config.setPublicPath('/build');
2212+
config.disableSingleRuntimeChunk();
2213+
config.addEntry('main', './js/css_import');
2214+
2215+
testSetup.runWebpack(config, (webpackAssert) => {
2216+
expect(config.outputPath).to.be.a.directory()
2217+
.with.files([
2218+
'manifest.json',
2219+
'entrypoints.json',
2220+
'main.js',
2221+
'main.css',
2222+
]);
2223+
2224+
webpackAssert.assertOutputFileContains(
2225+
'main.css',
2226+
'font-size: 50px;'
2227+
);
2228+
2229+
done();
2230+
});
2231+
});
2232+
2233+
it('With CSS extraction disabled', (done) => {
2234+
const config = createWebpackConfig('build', 'dev');
2235+
config.setPublicPath('/build');
2236+
config.disableSingleRuntimeChunk();
2237+
config.addEntry('main', './js/css_import');
2238+
config.disableCssExtraction();
2239+
2240+
testSetup.runWebpack(config, (webpackAssert) => {
2241+
expect(config.outputPath).to.be.a.directory()
2242+
.with.files([
2243+
'manifest.json',
2244+
'entrypoints.json',
2245+
'main.js'
2246+
]);
2247+
2248+
webpackAssert.assertOutputFileContains(
2249+
'main.js',
2250+
'font-size: 50px;'
2251+
);
2252+
2253+
done();
2254+
});
2255+
});
2256+
});
22072257
});
22082258
});

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ describe('Public API', () => {
324324

325325
});
326326

327+
describe('disableCssExtraction', () => {
328+
329+
it('must return the API object', () => {
330+
const returnedValue = api.disableCssExtraction();
331+
expect(returnedValue).to.equal(api);
332+
});
333+
334+
});
335+
327336
describe('configureFilenames', () => {
328337

329338
it('must return the API object', () => {

0 commit comments

Comments
 (0)