@@ -4,27 +4,66 @@ import {expect} from 'chai';
44import * as webpack from 'webpack' ;
55import * as HtmlWebpackPlugin from 'html-webpack-plugin' ;
66import * as rimraf from 'rimraf' ;
7- import * as MiniCssExtractPlugin from 'mini-css-extract-plugin' ;
87import { HtmlWebpackLinkTypePlugin } from '../src/plugin' ;
98
109const OUTPUT_DIR = join ( __dirname , './test_dist' ) ;
1110
11+ const webpackPackageVersion = process . env . npm_package_devDependencies_webpack . replace ( / [ ^ 0 - 9 . ] / g, '' )
12+ const htmlPluginPackageVersion = process . env . npm_package_devDependencies_html_webpack_plugin . replace ( / [ ^ 0 - 9 . ] / g, '' )
13+
14+ const webpackVersion = webpack . version ?? webpackPackageVersion
15+ const htmlPluginVersion = HtmlWebpackPlugin . version ?? htmlPluginPackageVersion
16+
17+ console . log ( '\nWEBPACK VERSION' , webpackVersion , '\n' ) ;
18+ console . log ( '\nHTML-WEBPACK_PLUGIN VERSION' , htmlPluginVersion , '\n' ) ;
19+
20+ let cssRule ;
21+ let cssPlugin ;
22+ let cssPluginOpts ;
23+ let addMode = true ;
24+
25+ if ( / ^ \s * [ 3 ] / . test ( webpackVersion ) ) {
26+ // use extractTextWebpackPlugin
27+ const ExtractTextWebpackPlugin = require ( 'extract-text-webpack-plugin' ) ;
28+ cssRule = ExtractTextWebpackPlugin . extract ( {
29+ fallback : 'style-loader' ,
30+ use : 'css-loader'
31+ } ) ;
32+ cssPlugin = ExtractTextWebpackPlugin ;
33+ cssPluginOpts = '[name].css'
34+ addMode = false ;
35+ } else {
36+ const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
37+ cssRule = [
38+ {
39+ loader : MiniCssExtractPlugin . loader
40+ } ,
41+ {
42+ loader : 'css-loader'
43+ } ,
44+ ] ;
45+ cssPlugin = MiniCssExtractPlugin ;
46+ cssPluginOpts = {
47+ filename : '[name].css'
48+ } ;
49+ }
50+
1251const HtmlWebpackPluginOptions : HtmlWebpackPlugin . Options = {
1352 filename : 'index.html' ,
1453 hash : false ,
15- inject : 'body' ,
54+ inject : true ,
1655 minify : {
1756 collapseWhitespace : true ,
1857 removeComments : true ,
1958 removeRedundantAttributes : true ,
2059 useShortDoctype : true
2160 } ,
22- showErrors : false ,
61+ showErrors : true ,
2362 template : join ( __dirname , './test_data/index.html' ) ,
2463} ;
2564
65+
2666const webpackDevOptions : webpack . Configuration = {
27- mode : 'development' ,
2867 entry : {
2968 app : join ( __dirname , './test_data/entry.js' ) ,
3069 styles : join ( __dirname , './test_data/entry.css' ) ,
@@ -36,122 +75,114 @@ const webpackDevOptions: webpack.Configuration = {
3675 rules : [
3776 {
3877 test : / \. c s s $ / ,
39- use : [
40- {
41- loader : MiniCssExtractPlugin . loader
42- } ,
43- {
44- loader : 'css-loader'
45- } ,
46- ]
78+ use : cssRule
4779 }
4880 ]
4981 } ,
5082} ;
5183
5284const webpackProdOptions : webpack . Configuration = {
53- ...webpackDevOptions ,
54- mode : 'production' ,
85+ ...webpackDevOptions
5586} ;
5687
88+ if ( addMode ) {
89+ webpackDevOptions . mode = 'development'
90+ webpackProdOptions . mode = 'production'
91+ }
92+
5793function testAutoAssign ( err ) {
94+ if ( err ) {
95+ console . error ( err )
96+ }
5897 expect ( ! ! err ) . to . be . false ;
5998 const htmlFile = join ( OUTPUT_DIR , './index.html' ) ;
6099 const htmlContents = readFileSync ( htmlFile ) . toString ( 'utf8' ) ;
61- expect ( ! ! htmlContents ) . to . be . true ;
62- expect ( / h r e f = " s t y l e s \. c s s " [ ^ > ] * ?t y p e = " t e x t \/ c s s " / i. test ( htmlContents ) ) . to . be . true ;
63- expect ( / s r c = " a p p \. j s " / i. test ( htmlContents ) ) . to . be . true ;
100+ expect ( ! ! htmlContents , 'Missing HTML contents' ) . to . be . true ;
101+ expect ( / h r e f = " s t y l e s \. c s s " [ ^ > ] * ?t y p e = " t e x t \/ c s s " / i. test ( htmlContents ) , 'Missing labeled styles output' ) . to . be . true ;
102+ expect ( / s r c = " a p p \. j s " / i. test ( htmlContents ) , 'No app.js file appended to html' ) . to . be . true ;
64103}
65104
66105
67106function testTypeOverride ( err ) {
107+ if ( err ) {
108+ console . error ( err )
109+ }
68110 expect ( ! ! err ) . to . be . false ;
69111 const htmlFile = join ( OUTPUT_DIR , './index.html' ) ;
70112 const htmlContents = readFileSync ( htmlFile ) . toString ( ) ;
71- expect ( ! ! htmlContents ) . to . be . true ;
72- expect ( / h r e f = " s t y l e s \. c s s " [ ^ > ] * ?t y p e = " t e s t t y p e " / i. test ( htmlContents ) ) . to . be . true ;
73- expect ( / s r c = " a p p \. j s " / i. test ( htmlContents ) ) . to . be . true ;
113+ expect ( ! ! htmlContents , 'Missing HTML contents!' ) . to . be . true ;
114+ expect ( / h r e f = " s t y l e s \. c s s " [ ^ > ] * ?t y p e = " t e s t t y p e " / i. test ( htmlContents ) , 'Incorrect type applied or type not found' ) . to . be . true ;
115+ expect ( / s r c = " a p p \. j s " / i. test ( htmlContents ) , 'No app.js file appended to html' ) . to . be . true ;
74116}
75117
76- console . log ( '\nWEBPACK VERSION' , webpack . version , '\n' ) ;
77- console . log ( '\nHTML-WEBPACK_PLUGIN VERSION' , HtmlWebpackPlugin . version , '\n' ) ;
78-
79118describe ( 'HtmlWebpackLinkTypePlugin Development Mode' , ( ) => {
80119
81120 afterEach ( ( done ) => {
82121 rimraf ( OUTPUT_DIR , done ) ;
83122 } ) ;
84123
85- it ( 'should auto assign types to css and js' , function ( done ) {
124+ it ( 'should auto assign types to css and js' , ( done ) => {
86125 webpack ( { ...webpackDevOptions ,
87126 plugins : [
127+ new cssPlugin ( cssPluginOpts ) ,
88128 new HtmlWebpackPlugin ( HtmlWebpackPluginOptions ) ,
89129 new HtmlWebpackLinkTypePlugin ( ) ,
90- new MiniCssExtractPlugin ( {
91- filename : '[name].css'
92- } ) ,
93130 ]
94131 } , ( err ) => {
95132 testAutoAssign ( err ) ;
96- done ( ) ;
133+ done ( err ) ;
97134 } ) ;
98- } ) ;
135+ } )
99136
100- it ( 'should allow type overrides' , function ( done ) {
137+ it ( 'should allow type overrides' , ( done ) => {
101138 webpack ( {
102139 ...webpackDevOptions ,
103140 plugins : [
104141 new HtmlWebpackPlugin ( HtmlWebpackPluginOptions ) ,
105142 new HtmlWebpackLinkTypePlugin ( {
106143 '*.css' : 'testtype'
107144 } ) ,
108- new MiniCssExtractPlugin ( {
109- filename : '[name].css'
110- } ) ,
145+ new cssPlugin ( cssPluginOpts ) ,
111146 ]
112147 } , ( err ) => {
113148 testTypeOverride ( err ) ;
114- done ( ) ;
149+ done ( err ) ;
115150 } ) ;
116151 } ) ;
117152} ) ;
118153
119-
120- describe ( 'HtmlWebpackLinkTypePlugin Production Mode' , ( ) => {
121-
122- afterEach ( ( done ) => {
123- rimraf ( OUTPUT_DIR , done ) ;
124- } ) ;
125-
126- it ( 'should auto assign types to css and js' , function ( done ) {
127- webpack ( { ...webpackProdOptions ,
128- plugins : [
129- new HtmlWebpackPlugin ( HtmlWebpackPluginOptions ) ,
130- new HtmlWebpackLinkTypePlugin ( ) ,
131- new MiniCssExtractPlugin ( {
132- filename : '[name].css'
133- } ) ,
134- ]
135- } , ( err ) => {
136- testAutoAssign ( err ) ;
137- done ( ) ;
154+ if ( addMode ) {
155+ describe ( 'HtmlWebpackLinkTypePlugin Production Mode' , ( ) => {
156+ afterEach ( ( done ) => {
157+ rimraf ( OUTPUT_DIR , done ) ;
138158 } ) ;
139- } ) ;
140-
141- it ( 'should allow type overrides' , function ( done ) {
142- webpack ( { ...webpackProdOptions ,
143- plugins : [
144- new HtmlWebpackPlugin ( HtmlWebpackPluginOptions ) ,
145- new HtmlWebpackLinkTypePlugin ( {
146- '*.css' : 'testtype'
147- } ) ,
148- new MiniCssExtractPlugin ( {
149- filename : '[name].css'
150- } ) ,
151- ]
152- } , ( err ) => {
153- testTypeOverride ( err ) ;
154- done ( ) ;
159+
160+ it ( 'should auto assign types to css and js' , ( done ) => {
161+ webpack ( { ...webpackProdOptions ,
162+ plugins : [
163+ new HtmlWebpackPlugin ( HtmlWebpackPluginOptions ) ,
164+ new HtmlWebpackLinkTypePlugin ( ) ,
165+ new cssPlugin ( cssPluginOpts ) ,
166+ ]
167+ } , ( err ) => {
168+ testAutoAssign ( err ) ;
169+ done ( err ) ;
170+ } ) ;
171+ } ) ;
172+
173+ it ( 'should allow type overrides' , ( done ) => {
174+ webpack ( { ...webpackProdOptions ,
175+ plugins : [
176+ new HtmlWebpackPlugin ( HtmlWebpackPluginOptions ) ,
177+ new HtmlWebpackLinkTypePlugin ( {
178+ '*.css' : 'testtype'
179+ } ) ,
180+ new cssPlugin ( cssPluginOpts ) ,
181+ ]
182+ } , ( err ) => {
183+ testTypeOverride ( err ) ;
184+ done ( err ) ;
185+ } ) ;
155186 } ) ;
156187 } ) ;
157- } ) ;
188+ }
0 commit comments