File tree 8 files changed +103
-2
lines changed
8 files changed +103
-2
lines changed Original file line number Diff line number Diff line change
1
+
2
+ require ( './../css/h1_style.css' ) ;
3
+
4
+ console . log ( 'foo' ) ;
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 ;
@@ -644,6 +645,10 @@ class WebpackConfig {
644
645
this . useFontsLoader = false ;
645
646
}
646
647
648
+ disableCssExtraction ( ) {
649
+ this . extractCss = false ;
650
+ }
651
+
647
652
configureFilenames ( configuredFilenames = { } ) {
648
653
if ( typeof configuredFilenames !== 'object' ) {
649
654
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 {
391
391
buildPluginsConfig ( ) {
392
392
const plugins = [ ] ;
393
393
394
- miniCssExtractPluginUtil ( plugins , this . webpackConfig ) ;
394
+ if ( this . webpackConfig . extractCss ) {
395
+ miniCssExtractPluginUtil ( plugins , this . webpackConfig ) ;
396
+ }
395
397
396
398
// register the pure-style entries that should be deleted
397
399
deleteUnusedEntriesPluginUtil ( plugins , this . webpackConfig ) ;
Original file line number Diff line number Diff line change @@ -20,6 +20,6 @@ module.exports = {
20
20
* @return {Array }
21
21
*/
22
22
prependLoaders ( webpackConfig , loaders ) {
23
- return [ MiniCssExtractPlugin . loader , ...loaders ] ;
23
+ return [ webpackConfig . extractCss ? MiniCssExtractPlugin . loader : 'style-loader' , ...loaders ] ;
24
24
}
25
25
} ;
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 @@ -2166,5 +2166,55 @@ module.exports = {
2166
2166
} ) ;
2167
2167
} ) ;
2168
2168
} ) ;
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
+ } ) ;
2169
2219
} ) ;
2170
2220
} ) ;
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