Rename the assets template to support old templates files

This commit is contained in:
Jan Nicklas 2015-03-11 11:36:55 +01:00
parent 1debf35bdf
commit 55f316d396
6 changed files with 74 additions and 8 deletions

View File

@ -1,15 +1,15 @@
<!DOCTYPE html>
<html{% if(o.htmlWebpackPlugin.assets.manifest) { %} manifest="{%= o.htmlWebpackPlugin.assets.manifest + '?' + o.hash %}"{% } %}>
<html{% if(o.htmlWebpackPlugin.files.manifest) { %} manifest="{%= o.htmlWebpackPlugin.files.manifest + '?' + o.hash %}"{% } %}>
<head>
<meta charset="UTF-8">
<title>{%=o.htmlWebpackPlugin.options.title || 'Webpack App'%}</title>
{% for (var css in o.htmlWebpackPlugin.assets.css) { %}
<link href="{%=o.htmlWebpackPlugin.assets.css[css] + '?' + o.hash %}" rel="stylesheet">
{% for (var css in o.htmlWebpackPlugin.files.css) { %}
<link href="{%=o.htmlWebpackPlugin.files.css[css] + '?' + o.hash %}" rel="stylesheet">
{% } %}
</head>
<body>
{% for (var chunk in o.htmlWebpackPlugin.assets.chunks) { %}
<script src="{%=o.htmlWebpackPlugin.assets.chunks[chunk].entry + '?' + o.hash %}"></script>
{% for (var chunk in o.htmlWebpackPlugin.files.chunks) { %}
<script src="{%=o.htmlWebpackPlugin.files.chunks[chunk].entry + '?' + o.hash %}"></script>
{% } %}
</body>
</html>

View File

@ -15,7 +15,8 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
templateParams.webpack = webpackStatsJson;
templateParams.hash = webpackStatsJson.hash;
templateParams.htmlWebpackPlugin = {};
templateParams.htmlWebpackPlugin.assets = self.htmlWebpackPluginAssets(compilation, webpackStatsJson);
templateParams.htmlWebpackPlugin.assets = self.htmlWebpackPluginLegacyAssets(compilation, webpackStatsJson);
templateParams.htmlWebpackPlugin.files = self.htmlWebpackPluginAssets(compilation, webpackStatsJson);
templateParams.htmlWebpackPlugin.options = self.options;
var outputFilename = self.options.filename || 'index.html';
@ -66,7 +67,7 @@ HtmlWebpackPlugin.prototype.emitHtml = function(compilation, htmlTemplateContent
HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function(compilation, webpackStatsJson) {
var assets = {
// Will contain all js & css files by chunk
chunks: [],
chunks: {},
// Will contain all js files
js: [],
// Will contain all css files
@ -103,4 +104,18 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function(compilation, webp
return assets;
};
/**
* A helper to support the templates written for html-webpack-plugin <= 1.1.0
*/
HtmlWebpackPlugin.prototype.htmlWebpackPluginLegacyAssets = function(compilation, webpackStatsJson) {
var assets = this.htmlWebpackPluginAssets(compilation, webpackStatsJson);
var legacyAssets = {};
Object.keys(assets.chunks).forEach(function(chunkName){
legacyAssets[chunkName] = assets.chunks[chunkName].entry;
});
return legacyAssets;
};
module.exports = HtmlWebpackPlugin;

View File

@ -83,6 +83,34 @@ describe('HtmlWebpackPlugin', function() {
['<script src="app_bundle.js'], null, done);
});
it('allows you to use the deprecated assets object', function (done) {
testHtmlPlugin({
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({template: path.join(__dirname, 'fixtures/legacy.html')})]
},
['<script src="app_bundle.js', 'Some unique text'], null, done);
});
it('allows you to use the deprecated default_index file', function (done) {
testHtmlPlugin({
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({template: path.join(__dirname, 'fixtures/legacy_default_index.html')})]
},
['<script src="app_bundle.js'], null, done);
});
it('registers a webpack error both template and template content are specified', function(done) {
webpack({
entry: path.join(__dirname, 'fixtures/index.js'),

11
spec/fixtures/legacy.html vendored Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>Test</title>
</head>
<body>
<p>Some unique text</p>
<script src="{%=o.htmlWebpackPlugin.assets.app%}"></script>
</body>
</html>

12
spec/fixtures/legacy_default_index.html vendored Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{%=o.htmlWebpackPlugin.options.title || 'Webpack App'%}</title>
</head>
<body>
{% for (var chunk in o.htmlWebpackPlugin.assets) { %}
<script src="{%=o.htmlWebpackPlugin.assets[chunk]%}"></script>
{% } %}
</body>
</html>

View File

@ -6,6 +6,6 @@
</head>
<body>
<p>Some unique text</p>
<script src="{%=o.htmlWebpackPlugin.assets.chunks.app.entry%}"></script>
<script src="{%=o.htmlWebpackPlugin.files.chunks.app.entry%}"></script>
</body>
</html>