Skip to content

Commit 725833d

Browse files
committed
Add Encore.disableCssExtraction() to the public API
1 parent 0facc4d commit 725833d

File tree

8 files changed

+103
-2
lines changed

8 files changed

+103
-2
lines changed

fixtures/js/css_import.js

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

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;
@@ -644,6 +645,10 @@ class WebpackConfig {
644645
this.useFontsLoader = false;
645646
}
646647

648+
disableCssExtraction() {
649+
this.extractCss = false;
650+
}
651+
647652
configureFilenames(configuredFilenames = {}) {
648653
if (typeof configuredFilenames !== 'object') {
649654
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
@@ -391,7 +391,9 @@ class ConfigGenerator {
391391
buildPluginsConfig() {
392392
const plugins = [];
393393

394-
miniCssExtractPluginUtil(plugins, this.webpackConfig);
394+
if (this.webpackConfig.extractCss) {
395+
miniCssExtractPluginUtil(plugins, this.webpackConfig);
396+
}
395397

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

lib/loaders/css-extract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ module.exports = {
2020
* @return {Array}
2121
*/
2222
prependLoaders(webpackConfig, loaders) {
23-
return [MiniCssExtractPlugin.loader, ...loaders];
23+
return [webpackConfig.extractCss ? MiniCssExtractPlugin.loader : 'style-loader', ...loaders];
2424
}
2525
};

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
@@ -2166,5 +2166,55 @@ module.exports = {
21662166
});
21672167
});
21682168
});
2169+
2170+
describe('CSS extraction', () => {
2171+
it('With CSS extraction enabled', (done) => {
2172+
const config = createWebpackConfig('build', 'dev');
2173+
config.setPublicPath('/build');
2174+
config.disableSingleRuntimeChunk();
2175+
config.addEntry('main', './js/css_import');
2176+
2177+
testSetup.runWebpack(config, (webpackAssert) => {
2178+
expect(config.outputPath).to.be.a.directory()
2179+
.with.files([
2180+
'manifest.json',
2181+
'entrypoints.json',
2182+
'main.js',
2183+
'main.css',
2184+
]);
2185+
2186+
webpackAssert.assertOutputFileContains(
2187+
'main.css',
2188+
'font-size: 50px;'
2189+
);
2190+
2191+
done();
2192+
});
2193+
});
2194+
2195+
it('With CSS extraction disabled', (done) => {
2196+
const config = createWebpackConfig('build', 'dev');
2197+
config.setPublicPath('/build');
2198+
config.disableSingleRuntimeChunk();
2199+
config.addEntry('main', './js/css_import');
2200+
config.disableCssExtraction();
2201+
2202+
testSetup.runWebpack(config, (webpackAssert) => {
2203+
expect(config.outputPath).to.be.a.directory()
2204+
.with.files([
2205+
'manifest.json',
2206+
'entrypoints.json',
2207+
'main.js'
2208+
]);
2209+
2210+
webpackAssert.assertOutputFileContains(
2211+
'main.js',
2212+
'font-size: 50px;'
2213+
);
2214+
2215+
done();
2216+
});
2217+
});
2218+
});
21692219
});
21702220
});

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)