File tree Expand file tree Collapse file tree 8 files changed +111
-1
lines changed Expand file tree Collapse file tree 8 files changed +111
-1
lines changed Original file line number Diff line number Diff line change 1+
2+ require ( './../css/h1_style.css' ) ;
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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.' ) ;
Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff 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} ;
Original file line number Diff line number Diff 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 ( ) ;
Original file line number Diff line number Diff 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} ) ;
Original file line number Diff line number Diff 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' , ( ) => {
You can’t perform that action at this time.
0 commit comments