forked from numical/style-ext-html-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStyleExtHtmlWebpackPluginSpec.js
More file actions
150 lines (140 loc) · 4.16 KB
/
Copy pathStyleExtHtmlWebpackPluginSpec.js
File metadata and controls
150 lines (140 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* eslint-env jasmine */
'use strict';
// Workaround for css-loader issue
// https://github.com/webpack/css-loader/issues/144
if (!global.Promise) {
require('es6-promise').polyfill();
}
// for debugging
if (typeof v8debug === 'object') {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
}
var path = require('path');
var fs = require('fs');
var webpack = require('webpack');
var rm_rf = require('rimraf');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var StyleExtHtmlWebpackPlugin = require('../index.js');
var OUTPUT_DIR = path.join(__dirname, '../dist');
function testPlugin (webpackConfig, expectedResults, done, showHtml) {
var outputFile = 'index.html';
webpack(webpackConfig, function (err, stats) {
expect(err).toBeFalsy();
var compilationErrors = (stats.compilation.errors || []).join('\n');
expect(compilationErrors).toBe('');
var compilationWarnings = (stats.compilation.warnings || []).join('\n');
expect(compilationWarnings).toBe('');
var outputFileExists = fs.existsSync(path.join(OUTPUT_DIR, outputFile));
expect(outputFileExists).toBe(true);
if (!outputFileExists) {
return done();
}
var htmlContent = fs.readFileSync(path.join(OUTPUT_DIR, outputFile)).toString();
if (showHtml) {
console.log(htmlContent);
}
for (var i = 0; i < expectedResults.length; i++) {
var expectedResult = expectedResults[i];
if (expectedResult instanceof RegExp) {
expect(htmlContent).toMatch(expectedResult);
} else {
expect(htmlContent).toContain(expectedResult);
}
}
done();
});
}
describe('StyleExtHtmlWebpackPlugin', function () {
beforeEach(function (done) {
rm_rf(OUTPUT_DIR, done);
});
it('inlines a single stylesheet', function (done) {
testPlugin(
{ entry: path.join(__dirname, 'fixtures/one_stylesheet.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
module: {
loaders: [
{test: /\.css$/, loader: StyleExtHtmlWebpackPlugin.inline()}
]
},
plugins: [
new HtmlWebpackPlugin(),
new StyleExtHtmlWebpackPlugin()
]
},
[fs.readFileSync(path.join(__dirname, 'fixtures', 'exptected_one_stylesheet.html'))],
done);
});
it('inlines multiple stylesheets', function (done) {
testPlugin(
{ entry: path.join(__dirname, 'fixtures/two_stylesheets.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
module: {
loaders: [
{test: /\.css$/, loader: StyleExtHtmlWebpackPlugin.inline()}
]
},
plugins: [
new HtmlWebpackPlugin(),
new StyleExtHtmlWebpackPlugin()
]
},
[fs.readFileSync(path.join(__dirname, 'fixtures', 'exptected_two_stylesheets.html'))],
done);
});
it('inlining works with postcss-loader', function (done) {
testPlugin(
{ entry: path.join(__dirname, 'fixtures/two_stylesheets.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
module: {
loaders: [
{test: /\.css$/, loader: StyleExtHtmlWebpackPlugin.inline('postcss-loader')}
]
},
postcss: [
require('postcss-spiffing')
],
plugins: [
new HtmlWebpackPlugin(),
new StyleExtHtmlWebpackPlugin()
]
},
[/color: gray/],
done);
});
/* Will not pass until additional event added to HtmlWebpackPlugin
it('inlined stylesheets are minified if minify options are set', function (done) {
testPlugin(
{ entry: path.join(__dirname, 'fixtures/two_stylesheets.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
module: {
loaders: [
{ test: /\.css$/, loader: StyleExtHtmlWebpackPlugin.inline() }
]
},
plugins: [
new HtmlWebpackPlugin({
minify: {
minifyCSS: true
}
}),
new StyleExtHtmlWebpackPlugin()
]
},
[/(<style>.*<\/style>){2}/],
done);
});
*/
});