Skip to content

Commit f73204b

Browse files
committed
attempt to fix tests for webpack3
mini-css-extract-plugin does not work AT ALL with webpack3. The old configuration for extract-text-plugin should work, but I'm missing something, because the webpack build never completes, and just hangs. I've tested in an actual webpack3 project and verified it works, so there's something in my test setup, not the project code itself. Will eventually fix, but I don't want to hold up an actual fix to fix a test...
1 parent 1fe41c4 commit f73204b

File tree

3 files changed

+105
-72
lines changed

3 files changed

+105
-72
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
strategy:
77
matrix:
88
webpack: [4]
9-
html-webpack: [4, 3]
9+
html-webpack: [3, 4]
1010
include:
1111
- webpack: 5
1212
html-webpack: 5

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-webpack-link-type-plugin",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "a plugin for html-webpack-plugin to allow for mimetype tagging of injected links",
55
"main": "dist/plugin.js",
66
"scripts": {
@@ -23,10 +23,12 @@
2323
"@types/node": "12.19.15",
2424
"chai": "4.2.0",
2525
"css-loader": "5.0.1",
26+
"extract-text-webpack-plugin": "3.0.2",
2627
"html-webpack-plugin": ">=3.2.0",
2728
"mini-css-extract-plugin": "1.3.5",
2829
"mocha": "8.2.1",
2930
"rimraf": "3.0.2",
31+
"style-loader": "3.3.0",
3032
"ts-node": "9.1.1",
3133
"typescript": "4.1.3",
3234
"webpack": ">=3.0.0"

spec/index.spec.ts

Lines changed: 101 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,66 @@ import {expect} from 'chai';
44
import * as webpack from 'webpack';
55
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
66
import * as rimraf from 'rimraf';
7-
import * as MiniCssExtractPlugin from 'mini-css-extract-plugin';
87
import {HtmlWebpackLinkTypePlugin} from '../src/plugin';
98

109
const 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+
1251
const 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+
2666
const 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: /\.css$/,
39-
use: [
40-
{
41-
loader: MiniCssExtractPlugin.loader
42-
},
43-
{
44-
loader: 'css-loader'
45-
},
46-
]
78+
use: cssRule
4779
}
4880
]
4981
},
5082
};
5183

5284
const 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+
5793
function 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(/href="styles\.css"[^>]*?type="text\/css"/i.test(htmlContents)).to.be.true;
63-
expect(/src="app\.js"/i.test(htmlContents)).to.be.true;
100+
expect(!!htmlContents, 'Missing HTML contents').to.be.true;
101+
expect(/href="styles\.css"[^>]*?type="text\/css"/i.test(htmlContents), 'Missing labeled styles output').to.be.true;
102+
expect(/src="app\.js"/i.test(htmlContents), 'No app.js file appended to html').to.be.true;
64103
}
65104

66105

67106
function 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(/href="styles\.css"[^>]*?type="testtype"/i.test(htmlContents)).to.be.true;
73-
expect(/src="app\.js"/i.test(htmlContents)).to.be.true;
113+
expect(!!htmlContents, 'Missing HTML contents!').to.be.true;
114+
expect(/href="styles\.css"[^>]*?type="testtype"/i.test(htmlContents), 'Incorrect type applied or type not found').to.be.true;
115+
expect(/src="app\.js"/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-
79118
describe('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

Comments
 (0)