Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ function StyleExtHtmlWebpackPlugin () {
StyleExtHtmlWebpackPlugin.prototype.apply = function (compiler) {
var self = this;
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-after-html-processing', self.addInlineCss);
compilation.plugin('html-webpack-plugin-after-html-processing', function (htmlPluginData, callback) {
self.addInlineCss(compilation, htmlPluginData, callback);
});
});
};

StyleExtHtmlWebpackPlugin.prototype.addInlineCss = function (htmlPluginData, callback) {
StyleExtHtmlWebpackPlugin.prototype.addInlineCss = function (compilation, htmlPluginData, callback) {
// compilation passed as this
if (this.inlineCss) {
var splitHtml = htmlPluginData.html.split('</head>');
var head = splitHtml[0];
var body = splitHtml[1];
var scripts = this.inlineCss.map(function (css) {
return '<style>' + css + '</style>';
if (compilation.inlineCss) {
var styles = '<style>' + compilation.inlineCss.join('\n') + '</style>';
htmlPluginData.html = htmlPluginData.html.replace(/(<\/head>)/i, function (match) {
return styles + match;
});
htmlPluginData.html = [head].concat(scripts).concat('</head>').concat(body).join('');
}
callback();
};
Expand All @@ -29,12 +28,8 @@ StyleExtHtmlWebpackPlugin.prototype.addInlineCss = function (htmlPluginData, cal
*/
StyleExtHtmlWebpackPlugin.inline = function (loaders) {
var inlineLoader = require.resolve('./loader.js');
if (loaders) {
loaders = [ inlineLoader ].concat(loaders).join('!');
} else {
loaders = inlineLoader;
}
return loaders;
// add the inline loader before all other loaders
return [ inlineLoader ].concat(loaders || []).join('!');
};

module.exports = StyleExtHtmlWebpackPlugin;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"jasmine": "^2.4.1",
"postcss-loader": "^0.8.2",
"postcss-spiffing": "0.0.6",
"rimraf": "^2.5.2",
"semistandard": "^7.0.5",
"style-loader": "^0.13.0",
"webpack": "^1.12.14"
},
"dependencies": {
}
"dependencies": {}
}
4 changes: 2 additions & 2 deletions spec/StyleExtHtmlWebpackPluginSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('StyleExtHtmlWebpackPlugin', function () {
new StyleExtHtmlWebpackPlugin()
]
},
[/(<style>[\s\S]*<\/style>){1}/],
[fs.readFileSync(path.join(__dirname, 'fixtures', 'exptected_one_stylesheet.html'))],
done);
});

Expand All @@ -93,7 +93,7 @@ describe('StyleExtHtmlWebpackPlugin', function () {
new StyleExtHtmlWebpackPlugin()
]
},
[/(<style>[\s\S]*<\/style>){2}/],
[fs.readFileSync(path.join(__dirname, 'fixtures', 'exptected_two_stylesheets.html'))],
done);
});

Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/exptected_one_stylesheet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<head>
<meta charset="UTF-8">
<title>Webpack App</title>
<style>body {
background: snow;
}</style></head>
10 changes: 10 additions & 0 deletions spec/fixtures/exptected_two_stylesheets.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<head>
<meta charset="UTF-8">
<title>Webpack App</title>
<style>body {
background: snow;
}
body {
color: grey;
}
</style></head>
2 changes: 1 addition & 1 deletion spec/fixtures/stylesheet2.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
body {
colour: grey;
color: grey;
}