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 ;
@@ -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.' ) ;
Original file line number Diff line number Diff 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 ) ;
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 @@ -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} ) ;
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