@@ -1018,34 +1018,172 @@ describe('The config-generator function', () => {
10181018 describe ( 'Test configureLoaderRule()' , ( ) => {
10191019 let config ;
10201020
1021- const getLoader = ( loaderName ) => {
1022- const webpackConfig = configGenerator ( config ) ;
1023- return webpackConfig . module . rules . find ( rule => rule . loader === loaderName ) ;
1024- } ;
1025-
10261021 beforeEach ( ( ) => {
10271022 config = createConfig ( ) ;
10281023 config . outputPath = '/tmp/public/build' ;
10291024 config . setPublicPath ( '/' ) ;
1025+ config . enableSingleRuntimeChunk ( ) ;
1026+ } ) ;
1027+
1028+ it ( 'configure rule for "javascript and "js"' , ( ) => {
1029+ config . configureLoaderRule ( 'javascript' , ( loaderRule ) => {
1030+ loaderRule . test = / \. m ? j s $ / ;
1031+ } ) ;
1032+ config . configureLoaderRule ( 'js' , ( loaderRule ) => {
1033+ loaderRule . use [ 0 ] . options . fooBar = 'fooBar' ;
1034+ } ) ;
1035+
1036+ const webpackConfig = configGenerator ( config ) ;
1037+ const rule = findRule ( / \. m ? j s $ / , webpackConfig . module . rules ) ;
1038+
1039+ expect ( 'file.js' ) . to . match ( rule . test ) ;
1040+ expect ( 'file.mjs' ) . to . match ( rule . test ) ;
1041+ expect ( rule . use [ 0 ] . options . fooBar ) . to . equal ( 'fooBar' ) ;
1042+ } ) ;
1043+
1044+ it ( 'configure rule for "css"' , ( ) => {
1045+ config . configureLoaderRule ( 'css' , ( loaderRule ) => {
1046+ loaderRule . camelCase = true ;
1047+ } ) ;
1048+
1049+ const webpackConfig = configGenerator ( config ) ;
1050+ const rule = findRule ( / \. c s s $ / , webpackConfig . module . rules ) ;
1051+
1052+ expect ( rule . camelCase ) . to . be . true ;
1053+ } ) ;
1054+
1055+ it ( 'configure rule for "images"' , ( ) => {
1056+ config . configureLoaderRule ( 'images' , ( loaderRule ) => {
1057+ loaderRule . options . name = 'dirname-images/[hash:42].[ext]' ;
1058+ } ) ;
1059+
1060+ const webpackConfig = configGenerator ( config ) ;
1061+ const rule = findRule ( / \. ( p n g | j p g | j p e g | g i f | i c o | s v g | w e b p ) $ / , webpackConfig . module . rules ) ;
1062+
1063+ expect ( rule . options . name ) . to . equal ( 'dirname-images/[hash:42].[ext]' ) ;
1064+ } ) ;
1065+
1066+ it ( 'configure rule for "fonts"' , ( ) => {
1067+ config . configureLoaderRule ( 'fonts' , ( loader ) => {
1068+ loader . options . name = 'dirname-fonts/[hash:42].[ext]' ;
1069+ } ) ;
1070+
1071+ const webpackConfig = configGenerator ( config ) ;
1072+ const rule = findRule ( / \. ( w o f f | w o f f 2 | t t f | e o t | o t f ) $ / , webpackConfig . module . rules ) ;
1073+
1074+ expect ( rule . options . name ) . to . equal ( 'dirname-fonts/[hash:42].[ext]' ) ;
1075+ } ) ;
1076+
1077+ it ( 'configure rule for "sass" and "scss"' , ( ) => {
1078+ config . enableSassLoader ( ) ;
1079+ config . configureLoaderRule ( 'sass' , ( loaderRule ) => {
1080+ loaderRule . use . push ( 'Option pushed when configuring Sass.' ) ;
1081+ } ) ;
1082+ config . configureLoaderRule ( 'scss' , ( loaderRule ) => {
1083+ loaderRule . use . push ( 'Option pushed when configuring SCSS.' ) ;
1084+ } ) ;
1085+
1086+ const webpackConfig = configGenerator ( config ) ;
1087+ const rule = findRule ( / \. s [ a c ] s s $ / , webpackConfig . module . rules ) ;
1088+
1089+ expect ( rule . use . pop ( ) ) . to . equal ( 'Option pushed when configuring SCSS.' ) ;
1090+ expect ( rule . use . pop ( ) ) . to . equal ( 'Option pushed when configuring Sass.' ) ;
1091+ } ) ;
1092+
1093+ it ( 'configure rule for "less"' , ( ) => {
1094+ config . enableLessLoader ( ( options ) => {
1095+ options . optionA = 'optionA' ;
1096+ } ) ;
1097+ config . configureLoaderRule ( 'less' , ( loaderRule ) => {
1098+ loaderRule . use [ 2 ] . options . optionB = 'optionB' ;
1099+ } ) ;
1100+
1101+ const webpackConfig = configGenerator ( config ) ;
1102+ const rule = findRule ( / \. l e s s / , webpackConfig . module . rules ) ;
1103+
1104+ expect ( rule . use [ 2 ] . options . optionA ) . to . equal ( 'optionA' ) ;
1105+ expect ( rule . use [ 2 ] . options . optionB ) . to . equal ( 'optionB' ) ;
1106+ } ) ;
1107+
1108+ it ( 'configure rule for "stylus"' , ( ) => {
1109+ config . enableStylusLoader ( ( options ) => {
1110+ options . optionA = 'optionA' ;
1111+ } ) ;
1112+ config . configureLoaderRule ( 'stylus' , ( loaderRule ) => {
1113+ loaderRule . use [ 2 ] . options . optionB = 'optionB' ;
1114+ } ) ;
1115+
1116+ const webpackConfig = configGenerator ( config ) ;
1117+ const rule = findRule ( / \. s t y l / , webpackConfig . module . rules ) ;
1118+
1119+ expect ( rule . use [ 2 ] . options . optionA ) . to . equal ( 'optionA' ) ;
1120+ expect ( rule . use [ 2 ] . options . optionB ) . to . equal ( 'optionB' ) ;
1121+ } ) ;
1122+
1123+ it ( 'configure rule for "vue"' , ( ) => {
1124+ config . enableVueLoader ( ( options ) => {
1125+ options . shadowMode = true ;
1126+ } ) ;
1127+ config . configureLoaderRule ( 'vue' , ( loaderRule ) => {
1128+ loaderRule . use [ 0 ] . options . prettify = false ;
1129+ } ) ;
1130+
1131+ const webpackConfig = configGenerator ( config ) ;
1132+ const rule = findRule ( / \. v u e $ / , webpackConfig . module . rules ) ;
1133+
1134+ expect ( rule . use [ 0 ] . options . shadowMode ) . to . be . true ;
1135+ expect ( rule . use [ 0 ] . options . prettify ) . to . be . false ;
10301136 } ) ;
10311137
10321138 it ( 'configure rule for "eslint"' , ( ) => {
1033- config . enableEslintLoader ( ) ;
1034- config . configureLoaderRule ( 'eslint' , ( loader ) => {
1035- loader . test = / \. ( j s x ? | v u e ) / ;
1139+ config . enableEslintLoader ( ( options ) => {
1140+ options . extends = 'airbnb' ;
1141+ } ) ;
1142+ config . configureLoaderRule ( 'eslint' , ( loaderRule ) => {
1143+ loaderRule . test = / \. ( j s x ? | v u e ) / ;
10361144 } ) ;
10371145
1038- expect ( getLoader ( 'eslint-loader' ) ) . to . deep . equals ( {
1039- test : / \. ( j s x ? | v u e ) / ,
1040- enforce : 'pre' ,
1041- exclude : / n o d e _ m o d u l e s / ,
1042- loader : 'eslint-loader' ,
1043- options : {
1044- cache : true ,
1045- emitWarning : true ,
1046- parser : 'babel-eslint'
1047- }
1146+ const webpackConfig = configGenerator ( config ) ;
1147+ const rule = findRule ( / \. ( j s x ? | v u e ) / , webpackConfig . module . rules ) ;
1148+
1149+ expect ( rule . options . extends ) . to . equal ( 'airbnb' ) ;
1150+ expect ( 'file.js' ) . to . match ( rule . test ) ;
1151+ expect ( 'file.jsx' ) . to . match ( rule . test ) ;
1152+ expect ( 'file.vue' ) . to . match ( rule . test ) ;
1153+ } ) ;
1154+
1155+ it ( 'configure rule for "typescript" and "ts"' , ( ) => {
1156+ config . enableTypeScriptLoader ( ( options ) => {
1157+ options . silent = true ;
1158+ } ) ;
1159+ config . configureLoaderRule ( 'typescript' , ( loaderRule ) => {
1160+ loaderRule . use [ 1 ] . options . happyPackMode = true ;
10481161 } ) ;
1162+ config . configureLoaderRule ( 'ts' , ( loaderRule ) => {
1163+ loaderRule . use [ 1 ] . options . logInfoToStdOut = true ;
1164+ } ) ;
1165+
1166+ const webpackConfig = configGenerator ( config ) ;
1167+ const rule = findRule ( / \. t s x ? $ / , webpackConfig . module . rules ) ;
1168+
1169+ expect ( rule . use [ 1 ] . options . silent ) . to . be . true ;
1170+ expect ( rule . use [ 1 ] . options . happyPackMode ) . to . be . true ;
1171+ expect ( rule . use [ 1 ] . options . logInfoToStdOut ) . to . be . true ;
1172+ } ) ;
1173+
1174+ it ( 'configure rule for "handlebars"' , ( ) => {
1175+ config . enableHandlebarsLoader ( ( options ) => {
1176+ options . debug = true ;
1177+ } ) ;
1178+ config . configureLoaderRule ( 'handlebars' , ( loaderRule ) => {
1179+ loaderRule . use [ 0 ] . options . fooBar = 'fooBar' ;
1180+ } ) ;
1181+
1182+ const webpackConfig = configGenerator ( config ) ;
1183+ const rule = findRule ( / \. ( h a n d l e b a r s | h b s ) $ / , webpackConfig . module . rules ) ;
1184+
1185+ expect ( rule . use [ 0 ] . options . debug ) . to . be . true ;
1186+ expect ( rule . use [ 0 ] . options . fooBar ) . to . be . equal ( 'fooBar' ) ;
10491187 } ) ;
10501188 } ) ;
10511189} ) ;
0 commit comments