Add support for specifying the title of the generated HTML

This commit is contained in:
Charles Blaxland 2014-08-13 09:30:27 +10:00
parent 42e8dfa651
commit 67ae8e0746
3 changed files with 18 additions and 11 deletions

View File

@ -2,7 +2,7 @@
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>Webpack App</title>
<title>{%=o.htmlWebpackPlugin.options.title || 'Webpack App'%}</title>
</head>
<body>
{% for (var chunk in o.htmlWebpackPlugin.assets) { %}

View File

@ -12,7 +12,9 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
var webpackStatsJson = stats.toJson();
var templateParams = {};
templateParams.webpack = webpackStatsJson;
templateParams.htmlWebpackPlugin = self.htmlWebpackPluginJson(compiler, webpackStatsJson);
templateParams.htmlWebpackPlugin = {};
templateParams.htmlWebpackPlugin.assets = self.htmlWebpackPluginAssets(compiler, webpackStatsJson);
templateParams.htmlWebpackPlugin.options = self.options;
var templateFile = self.options.template;
if (!templateFile) {
@ -23,9 +25,8 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
});
};
HtmlWebpackPlugin.prototype.htmlWebpackPluginJson = function(compiler, webpackStatsJson) {
var json = {};
json.assets = {};
HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function(compiler, webpackStatsJson) {
var assets = {};
for (var chunk in webpackStatsJson.assetsByChunkName) {
var chunkValue = webpackStatsJson.assetsByChunkName[chunk];
@ -38,10 +39,10 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginJson = function(compiler, webpackSt
if (compiler.options.output.publicPath) {
chunkValue = compiler.options.output.publicPath + chunkValue;
}
json.assets[chunk] = chunkValue;
assets[chunk] = chunkValue;
}
return json;
return assets;
};
module.exports = HtmlWebpackPlugin;

View File

@ -83,7 +83,6 @@ describe('HtmlWebpackPlugin', function() {
it('handles hashes in bundle filenames', function(done) {
testHtmlPlugin({
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
@ -95,7 +94,6 @@ describe('HtmlWebpackPlugin', function() {
it('prepends the webpack public path to script src', function(done) {
testHtmlPlugin({
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
@ -108,7 +106,6 @@ describe('HtmlWebpackPlugin', function() {
it('handles subdirectories in the webpack output bundles', function(done) {
testHtmlPlugin({
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
@ -118,7 +115,6 @@ describe('HtmlWebpackPlugin', function() {
}, ['<script src="assets/index_bundle.js"'], done);
testHtmlPlugin({
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
@ -129,5 +125,15 @@ describe('HtmlWebpackPlugin', function() {
}, ['<script src="http://cdn.example.com/assets/index_bundle.js"'], done);
});
it('allows you to configure the title of the generated HTML page', function(done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin({title: 'My Cool App'})]
}, ['<title>My Cool App</title>'], done);
});
});