Skip to content

Commit

Permalink
Add Encore.disableCssExtraction() to the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyrkan committed Mar 17, 2019
1 parent 0facc4d commit 347feed
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 1 deletion.
2 changes: 2 additions & 0 deletions fixtures/js/css_import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

require('./../css/h1_style.css');
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,22 @@ class Encore {
return this;
}

/**
* Call this if you don't want imported CSS to be extracted
* into a .css file. All your styles will then be injected
* into the page by your JS code.
*
* Internally, this disables the mini-css-extract-plugin
* and uses the style-loader instead.
*
* @returns {Encore}
*/
disableCssExtraction() {
webpackConfig.disableCssExtraction();

return this;
}

/**
* Call this to change how the name of each output
* file is generated.
Expand Down
5 changes: 5 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class WebpackConfig {
this.useVersioning = false;
this.useSourceMaps = false;
this.cleanupOutput = false;
this.extractCss = true;
this.useImagesLoader = true;
this.useFontsLoader = true;
this.usePostCssLoader = false;
Expand Down Expand Up @@ -644,6 +645,10 @@ class WebpackConfig {
this.useFontsLoader = false;
}

disableCssExtraction() {
this.extractCss = false;
}

configureFilenames(configuredFilenames = {}) {
if (typeof configuredFilenames !== 'object') {
throw new Error('Argument 1 to configureFilenames() must be an object.');
Expand Down
4 changes: 3 additions & 1 deletion lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ class ConfigGenerator {
buildPluginsConfig() {
const plugins = [];

miniCssExtractPluginUtil(plugins, this.webpackConfig);
if (this.webpackConfig.extractCss) {
miniCssExtractPluginUtil(plugins, this.webpackConfig);
}

// register the pure-style entries that should be deleted
deleteUnusedEntriesPluginUtil(plugins, this.webpackConfig);
Expand Down
11 changes: 11 additions & 0 deletions lib/loaders/css-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ module.exports = {
* @return {Array}
*/
prependLoaders(webpackConfig, loaders) {
if (!webpackConfig.extractCss) {
// If the CSS extraction is disabled, use the
// style-loader instead.
return [{
loader: 'style-loader',
options: {
sourceMap: webpackConfig.useSourceMaps,
}
}, ...loaders];
}

return [MiniCssExtractPlugin.loader, ...loaders];
}
};
15 changes: 15 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,21 @@ describe('WebpackConfig object', () => {
});
});

describe('disableCssExtraction', () => {
it('By default the CSS extraction is enabled', () => {
const config = createConfig();

expect(config.extractCss).to.be.true;
});

it('Calling it disables the CSS extraction', () => {
const config = createConfig();
config.disableCssExtraction();

expect(config.extractCss).to.be.false;
});
});

describe('configureFilenames', () => {
it('Calling method sets it', () => {
const config = createConfig();
Expand Down
50 changes: 50 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -2166,5 +2166,55 @@ module.exports = {
});
});
});

describe('CSS extraction', () => {
it('With CSS extraction enabled', (done) => {
const config = createWebpackConfig('build', 'dev');
config.setPublicPath('/build');
config.disableSingleRuntimeChunk();
config.addEntry('main', './js/css_import');

testSetup.runWebpack(config, (webpackAssert) => {
expect(config.outputPath).to.be.a.directory()
.with.files([
'manifest.json',
'entrypoints.json',
'main.js',
'main.css',
]);

webpackAssert.assertOutputFileContains(
'main.css',
'font-size: 50px;'
);

done();
});
});

it('With CSS extraction disabled', (done) => {
const config = createWebpackConfig('build', 'dev');
config.setPublicPath('/build');
config.disableSingleRuntimeChunk();
config.addEntry('main', './js/css_import');
config.disableCssExtraction();

testSetup.runWebpack(config, (webpackAssert) => {
expect(config.outputPath).to.be.a.directory()
.with.files([
'manifest.json',
'entrypoints.json',
'main.js'
]);

webpackAssert.assertOutputFileContains(
'main.js',
'font-size: 50px;'
);

done();
});
});
});
});
});
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ describe('Public API', () => {

});

describe('disableCssExtraction', () => {

it('must return the API object', () => {
const returnedValue = api.disableCssExtraction();
expect(returnedValue).to.equal(api);
});

});

describe('configureFilenames', () => {

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

0 comments on commit 347feed

Please sign in to comment.