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 {
1046
1046
return this ;
1047
1047
}
1048
1048
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
+
1049
1065
/**
1050
1066
* Call this to change how the name of each output
1051
1067
* file is generated.
Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ class WebpackConfig {
53
53
this . useVersioning = false ;
54
54
this . useSourceMaps = false ;
55
55
this . cleanupOutput = false ;
56
+ this . extractCss = true ;
56
57
this . useImagesLoader = true ;
57
58
this . useFontsLoader = true ;
58
59
this . usePostCssLoader = false ;
@@ -657,6 +658,10 @@ class WebpackConfig {
657
658
this . useFontsLoader = false ;
658
659
}
659
660
661
+ disableCssExtraction ( ) {
662
+ this . extractCss = false ;
663
+ }
664
+
660
665
configureFilenames ( configuredFilenames = { } ) {
661
666
if ( typeof configuredFilenames !== 'object' ) {
662
667
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 {
397
397
buildPluginsConfig ( ) {
398
398
const plugins = [ ] ;
399
399
400
- miniCssExtractPluginUtil ( plugins , this . webpackConfig ) ;
400
+ if ( this . webpackConfig . extractCss ) {
401
+ miniCssExtractPluginUtil ( plugins , this . webpackConfig ) ;
402
+ }
401
403
402
404
// register the pure-style entries that should be deleted
403
405
deleteUnusedEntriesPluginUtil ( plugins , this . webpackConfig ) ;
Original file line number Diff line number Diff line change @@ -20,6 +20,17 @@ module.exports = {
20
20
* @return {Array }
21
21
*/
22
22
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
+
23
34
return [ MiniCssExtractPlugin . loader , ...loaders ] ;
24
35
}
25
36
} ;
Original file line number Diff line number Diff line change @@ -1033,6 +1033,21 @@ describe('WebpackConfig object', () => {
1033
1033
} ) ;
1034
1034
} ) ;
1035
1035
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
+
1036
1051
describe ( 'configureFilenames' , ( ) => {
1037
1052
it ( 'Calling method sets it' , ( ) => {
1038
1053
const config = createConfig ( ) ;
Original file line number Diff line number Diff line change @@ -2204,5 +2204,55 @@ module.exports = {
2204
2204
} ) ;
2205
2205
} ) ;
2206
2206
} ) ;
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
+ } ) ;
2207
2257
} ) ;
2208
2258
} ) ;
Original file line number Diff line number Diff line change @@ -324,6 +324,15 @@ describe('Public API', () => {
324
324
325
325
} ) ;
326
326
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
+
327
336
describe ( 'configureFilenames' , ( ) => {
328
337
329
338
it ( 'must return the API object' , ( ) => {
You can’t perform that action at this time.
0 commit comments