@@ -979,34 +979,172 @@ describe('The config-generator function', () => {
979979 describe ( 'Test configureLoaderRule()' , ( ) => {
980980 let config ;
981981
982- const getLoader = ( loaderName ) => {
983- const webpackConfig = configGenerator ( config ) ;
984- return webpackConfig . module . rules . find ( rule => rule . loader === loaderName ) ;
985- } ;
986-
987982 beforeEach ( ( ) => {
988983 config = createConfig ( ) ;
989984 config . outputPath = '/tmp/public/build' ;
990985 config . setPublicPath ( '/' ) ;
986+ config . enableSingleRuntimeChunk ( ) ;
987+ } ) ;
988+
989+ it ( 'configure rule for "javascript and "js"' , ( ) => {
990+ config . configureLoaderRule ( 'javascript' , ( loaderRule ) => {
991+ loaderRule . test = / \. m ? j s $ / ;
992+ } ) ;
993+ config . configureLoaderRule ( 'js' , ( loaderRule ) => {
994+ loaderRule . use [ 0 ] . options . fooBar = 'fooBar' ;
995+ } ) ;
996+
997+ const webpackConfig = configGenerator ( config ) ;
998+ const rule = findRule ( / \. m ? j s $ / , webpackConfig . module . rules ) ;
999+
1000+ expect ( 'file.js' ) . to . match ( rule . test ) ;
1001+ expect ( 'file.mjs' ) . to . match ( rule . test ) ;
1002+ expect ( rule . use [ 0 ] . options . fooBar ) . to . equal ( 'fooBar' ) ;
1003+ } ) ;
1004+
1005+ it ( 'configure rule for "css"' , ( ) => {
1006+ config . configureLoaderRule ( 'css' , ( loaderRule ) => {
1007+ loaderRule . camelCase = true ;
1008+ } ) ;
1009+
1010+ const webpackConfig = configGenerator ( config ) ;
1011+ const rule = findRule ( / \. c s s $ / , webpackConfig . module . rules ) ;
1012+
1013+ expect ( rule . camelCase ) . to . be . true ;
1014+ } ) ;
1015+
1016+ it ( 'configure rule for "images"' , ( ) => {
1017+ config . configureLoaderRule ( 'images' , ( loaderRule ) => {
1018+ loaderRule . options . name = 'dirname-images/[hash:42].[ext]' ;
1019+ } ) ;
1020+
1021+ const webpackConfig = configGenerator ( config ) ;
1022+ 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 ) ;
1023+
1024+ expect ( rule . options . name ) . to . equal ( 'dirname-images/[hash:42].[ext]' ) ;
1025+ } ) ;
1026+
1027+ it ( 'configure rule for "fonts"' , ( ) => {
1028+ config . configureLoaderRule ( 'fonts' , ( loader ) => {
1029+ loader . options . name = 'dirname-fonts/[hash:42].[ext]' ;
1030+ } ) ;
1031+
1032+ const webpackConfig = configGenerator ( config ) ;
1033+ const rule = findRule ( / \. ( w o f f | w o f f 2 | t t f | e o t | o t f ) $ / , webpackConfig . module . rules ) ;
1034+
1035+ expect ( rule . options . name ) . to . equal ( 'dirname-fonts/[hash:42].[ext]' ) ;
1036+ } ) ;
1037+
1038+ it ( 'configure rule for "sass" and "scss"' , ( ) => {
1039+ config . enableSassLoader ( ) ;
1040+ config . configureLoaderRule ( 'sass' , ( loaderRule ) => {
1041+ loaderRule . use . push ( 'Option pushed when configuring Sass.' ) ;
1042+ } ) ;
1043+ config . configureLoaderRule ( 'scss' , ( loaderRule ) => {
1044+ loaderRule . use . push ( 'Option pushed when configuring SCSS.' ) ;
1045+ } ) ;
1046+
1047+ const webpackConfig = configGenerator ( config ) ;
1048+ const rule = findRule ( / \. s [ a c ] s s $ / , webpackConfig . module . rules ) ;
1049+
1050+ expect ( rule . use . pop ( ) ) . to . equal ( 'Option pushed when configuring SCSS.' ) ;
1051+ expect ( rule . use . pop ( ) ) . to . equal ( 'Option pushed when configuring Sass.' ) ;
1052+ } ) ;
1053+
1054+ it ( 'configure rule for "less"' , ( ) => {
1055+ config . enableLessLoader ( ( options ) => {
1056+ options . optionA = 'optionA' ;
1057+ } ) ;
1058+ config . configureLoaderRule ( 'less' , ( loaderRule ) => {
1059+ loaderRule . use [ 2 ] . options . optionB = 'optionB' ;
1060+ } ) ;
1061+
1062+ const webpackConfig = configGenerator ( config ) ;
1063+ const rule = findRule ( / \. l e s s / , webpackConfig . module . rules ) ;
1064+
1065+ expect ( rule . use [ 2 ] . options . optionA ) . to . equal ( 'optionA' ) ;
1066+ expect ( rule . use [ 2 ] . options . optionB ) . to . equal ( 'optionB' ) ;
1067+ } ) ;
1068+
1069+ it ( 'configure rule for "stylus"' , ( ) => {
1070+ config . enableStylusLoader ( ( options ) => {
1071+ options . optionA = 'optionA' ;
1072+ } ) ;
1073+ config . configureLoaderRule ( 'stylus' , ( loaderRule ) => {
1074+ loaderRule . use [ 2 ] . options . optionB = 'optionB' ;
1075+ } ) ;
1076+
1077+ const webpackConfig = configGenerator ( config ) ;
1078+ const rule = findRule ( / \. s t y l / , webpackConfig . module . rules ) ;
1079+
1080+ expect ( rule . use [ 2 ] . options . optionA ) . to . equal ( 'optionA' ) ;
1081+ expect ( rule . use [ 2 ] . options . optionB ) . to . equal ( 'optionB' ) ;
1082+ } ) ;
1083+
1084+ it ( 'configure rule for "vue"' , ( ) => {
1085+ config . enableVueLoader ( ( options ) => {
1086+ options . shadowMode = true ;
1087+ } ) ;
1088+ config . configureLoaderRule ( 'vue' , ( loaderRule ) => {
1089+ loaderRule . use [ 0 ] . options . prettify = false ;
1090+ } ) ;
1091+
1092+ const webpackConfig = configGenerator ( config ) ;
1093+ const rule = findRule ( / \. v u e $ / , webpackConfig . module . rules ) ;
1094+
1095+ expect ( rule . use [ 0 ] . options . shadowMode ) . to . be . true ;
1096+ expect ( rule . use [ 0 ] . options . prettify ) . to . be . false ;
9911097 } ) ;
9921098
9931099 it ( 'configure rule for "eslint"' , ( ) => {
994- config . enableEslintLoader ( ) ;
995- config . configureLoaderRule ( 'eslint' , ( loader ) => {
996- loader . test = / \. ( j s x ? | v u e ) / ;
1100+ config . enableEslintLoader ( ( options ) => {
1101+ options . extends = 'airbnb' ;
1102+ } ) ;
1103+ config . configureLoaderRule ( 'eslint' , ( loaderRule ) => {
1104+ loaderRule . test = / \. ( j s x ? | v u e ) / ;
9971105 } ) ;
9981106
999- expect ( getLoader ( 'eslint-loader' ) ) . to . deep . equals ( {
1000- test : / \. ( j s x ? | v u e ) / ,
1001- enforce : 'pre' ,
1002- exclude : / n o d e _ m o d u l e s / ,
1003- loader : 'eslint-loader' ,
1004- options : {
1005- cache : true ,
1006- emitWarning : true ,
1007- parser : 'babel-eslint'
1008- }
1107+ const webpackConfig = configGenerator ( config ) ;
1108+ const rule = findRule ( / \. ( j s x ? | v u e ) / , webpackConfig . module . rules ) ;
1109+
1110+ expect ( rule . options . extends ) . to . equal ( 'airbnb' ) ;
1111+ expect ( 'file.js' ) . to . match ( rule . test ) ;
1112+ expect ( 'file.jsx' ) . to . match ( rule . test ) ;
1113+ expect ( 'file.vue' ) . to . match ( rule . test ) ;
1114+ } ) ;
1115+
1116+ it ( 'configure rule for "typescript" and "ts"' , ( ) => {
1117+ config . enableTypeScriptLoader ( ( options ) => {
1118+ options . silent = true ;
1119+ } ) ;
1120+ config . configureLoaderRule ( 'typescript' , ( loaderRule ) => {
1121+ loaderRule . use [ 1 ] . options . happyPackMode = true ;
10091122 } ) ;
1123+ config . configureLoaderRule ( 'ts' , ( loaderRule ) => {
1124+ loaderRule . use [ 1 ] . options . logInfoToStdOut = true ;
1125+ } ) ;
1126+
1127+ const webpackConfig = configGenerator ( config ) ;
1128+ const rule = findRule ( / \. t s x ? $ / , webpackConfig . module . rules ) ;
1129+
1130+ expect ( rule . use [ 1 ] . options . silent ) . to . be . true ;
1131+ expect ( rule . use [ 1 ] . options . happyPackMode ) . to . be . true ;
1132+ expect ( rule . use [ 1 ] . options . logInfoToStdOut ) . to . be . true ;
1133+ } ) ;
1134+
1135+ it ( 'configure rule for "handlebars"' , ( ) => {
1136+ config . enableHandlebarsLoader ( ( options ) => {
1137+ options . debug = true ;
1138+ } ) ;
1139+ config . configureLoaderRule ( 'handlebars' , ( loaderRule ) => {
1140+ loaderRule . use [ 0 ] . options . fooBar = 'fooBar' ;
1141+ } ) ;
1142+
1143+ const webpackConfig = configGenerator ( config ) ;
1144+ const rule = findRule ( / \. ( h a n d l e b a r s | h b s ) $ / , webpackConfig . module . rules ) ;
1145+
1146+ expect ( rule . use [ 0 ] . options . debug ) . to . be . true ;
1147+ expect ( rule . use [ 0 ] . options . fooBar ) . to . be . equal ( 'fooBar' ) ;
10101148 } ) ;
10111149 } ) ;
10121150} ) ;
0 commit comments