@@ -4,27 +4,66 @@ import {expect} from 'chai';
4
4
import * as webpack from 'webpack' ;
5
5
import * as HtmlWebpackPlugin from 'html-webpack-plugin' ;
6
6
import * as rimraf from 'rimraf' ;
7
- import * as MiniCssExtractPlugin from 'mini-css-extract-plugin' ;
8
7
import { HtmlWebpackLinkTypePlugin } from '../src/plugin' ;
9
8
10
9
const OUTPUT_DIR = join ( __dirname , './test_dist' ) ;
11
10
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
+
12
51
const HtmlWebpackPluginOptions : HtmlWebpackPlugin . Options = {
13
52
filename : 'index.html' ,
14
53
hash : false ,
15
- inject : 'body' ,
54
+ inject : true ,
16
55
minify : {
17
56
collapseWhitespace : true ,
18
57
removeComments : true ,
19
58
removeRedundantAttributes : true ,
20
59
useShortDoctype : true
21
60
} ,
22
- showErrors : false ,
61
+ showErrors : true ,
23
62
template : join ( __dirname , './test_data/index.html' ) ,
24
63
} ;
25
64
65
+
26
66
const webpackDevOptions : webpack . Configuration = {
27
- mode : 'development' ,
28
67
entry : {
29
68
app : join ( __dirname , './test_data/entry.js' ) ,
30
69
styles : join ( __dirname , './test_data/entry.css' ) ,
@@ -36,122 +75,114 @@ const webpackDevOptions: webpack.Configuration = {
36
75
rules : [
37
76
{
38
77
test : / \. c s s $ / ,
39
- use : [
40
- {
41
- loader : MiniCssExtractPlugin . loader
42
- } ,
43
- {
44
- loader : 'css-loader'
45
- } ,
46
- ]
78
+ use : cssRule
47
79
}
48
80
]
49
81
} ,
50
82
} ;
51
83
52
84
const webpackProdOptions : webpack . Configuration = {
53
- ...webpackDevOptions ,
54
- mode : 'production' ,
85
+ ...webpackDevOptions
55
86
} ;
56
87
88
+ if ( addMode ) {
89
+ webpackDevOptions . mode = 'development'
90
+ webpackProdOptions . mode = 'production'
91
+ }
92
+
57
93
function testAutoAssign ( err ) {
94
+ if ( err ) {
95
+ console . error ( err )
96
+ }
58
97
expect ( ! ! err ) . to . be . false ;
59
98
const htmlFile = join ( OUTPUT_DIR , './index.html' ) ;
60
99
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 ;
64
103
}
65
104
66
105
67
106
function testTypeOverride ( err ) {
107
+ if ( err ) {
108
+ console . error ( err )
109
+ }
68
110
expect ( ! ! err ) . to . be . false ;
69
111
const htmlFile = join ( OUTPUT_DIR , './index.html' ) ;
70
112
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 ;
74
116
}
75
117
76
- console . log ( '\nWEBPACK VERSION' , webpack . version , '\n' ) ;
77
- console . log ( '\nHTML-WEBPACK_PLUGIN VERSION' , HtmlWebpackPlugin . version , '\n' ) ;
78
-
79
118
describe ( 'HtmlWebpackLinkTypePlugin Development Mode' , ( ) => {
80
119
81
120
afterEach ( ( done ) => {
82
121
rimraf ( OUTPUT_DIR , done ) ;
83
122
} ) ;
84
123
85
- it ( 'should auto assign types to css and js' , function ( done ) {
124
+ it ( 'should auto assign types to css and js' , ( done ) => {
86
125
webpack ( { ...webpackDevOptions ,
87
126
plugins : [
127
+ new cssPlugin ( cssPluginOpts ) ,
88
128
new HtmlWebpackPlugin ( HtmlWebpackPluginOptions ) ,
89
129
new HtmlWebpackLinkTypePlugin ( ) ,
90
- new MiniCssExtractPlugin ( {
91
- filename : '[name].css'
92
- } ) ,
93
130
]
94
131
} , ( err ) => {
95
132
testAutoAssign ( err ) ;
96
- done ( ) ;
133
+ done ( err ) ;
97
134
} ) ;
98
- } ) ;
135
+ } )
99
136
100
- it ( 'should allow type overrides' , function ( done ) {
137
+ it ( 'should allow type overrides' , ( done ) => {
101
138
webpack ( {
102
139
...webpackDevOptions ,
103
140
plugins : [
104
141
new HtmlWebpackPlugin ( HtmlWebpackPluginOptions ) ,
105
142
new HtmlWebpackLinkTypePlugin ( {
106
143
'*.css' : 'testtype'
107
144
} ) ,
108
- new MiniCssExtractPlugin ( {
109
- filename : '[name].css'
110
- } ) ,
145
+ new cssPlugin ( cssPluginOpts ) ,
111
146
]
112
147
} , ( err ) => {
113
148
testTypeOverride ( err ) ;
114
- done ( ) ;
149
+ done ( err ) ;
115
150
} ) ;
116
151
} ) ;
117
152
} ) ;
118
153
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 ) ;
138
158
} ) ;
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
+ } ) ;
155
186
} ) ;
156
187
} ) ;
157
- } ) ;
188
+ }
0 commit comments