Rename `append` to `inject` and prevent double injection

This commit is contained in:
Jan Nicklas 2015-03-27 19:37:00 +01:00
parent 2a66d2f545
commit 383dcd480f
3 changed files with 29 additions and 19 deletions

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{%=o.htmlWebpackPlugin.options.title || 'Webpack App'%}</title>
</head>
<body>
</body>
</html>

View File

@ -32,7 +32,8 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
} else {
var templateFile = self.options.template;
if (!templateFile) {
templateFile = path.join(__dirname, 'default_index.html');
// Use a special index file to prevent double script / style injection if the `inject` option is truthy
templateFile = path.join(__dirname, self.options.inject ? 'default_inject_index.html' : 'default_index.html');
}
compilation.fileDependencies.push(templateFile);
@ -55,9 +56,9 @@ HtmlWebpackPlugin.prototype.emitHtml = function(compilation, htmlTemplateContent
} catch(e) {
compilation.errors.push(new Error('HtmlWebpackPlugin: template error ' + e));
}
// Append/Inject link and script elements into an existing html file
if (this.options.append) {
html = this.appendAssetsToHtml(html, templateParams, this.options.append);
// Inject link and script elements into an existing html file
if (this.options.inject) {
html = this.injectAssetsIntoHtml(html, templateParams, this.options.inject);
}
compilation.assets[outputFilename] = {
source: function() {
@ -131,11 +132,11 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function(compilation, webp
};
/**
* Inject the assets into the given html string
* Injects the assets into the given html string
*/
HtmlWebpackPlugin.prototype.appendAssetsToHtml = function(html, templateParams, chunks) {
HtmlWebpackPlugin.prototype.injectAssetsIntoHtml = function(html, templateParams, chunks) {
var assets = templateParams.htmlWebpackPlugin.files;
// If chunks is set to true append all chunks
// If chunks is set to true inject all chunks
if (chunks === true) {
chunks = Object.keys(assets.chunks);
}
@ -154,18 +155,18 @@ HtmlWebpackPlugin.prototype.appendAssetsToHtml = function(html, templateParams,
styles = styles.map(function(stylePath) {
return '<link href="' + stylePath + '?' + templateParams.hash + '" rel="stylesheet">';
});
// Append scripts
html = html.replace(/(<\/body>)/i, function (match, start) {
// Append scripts to body element
html = html.replace(/(<\/body>)/i, function (match) {
return scripts.join('') + match;
});
// Append styles
html = html.replace(/(<\/head>)/i, function (match, start) {
// Append styles to head element
html = html.replace(/(<\/head>)/i, function (match) {
return styles.join('') + match;
});
// Append manifest
// Inject manifest into the opening html tag
if (assets.manifest) {
html = html.replace(/(<html.*)(>)/i, function (match, start, end) {
// Don't append a manifest if a manifest was already specified
// Append the manifest only if no manifest was specified
if (match.test(/\smanifest\s*=/)) {
return match;
}

View File

@ -85,7 +85,7 @@ describe('HtmlWebpackPlugin', function() {
['<script src="app_bundle.js'], null, done);
});
it('allows you to append the assets to a given html file', function (done) {
it('allows you to inject the assets into a given html file', function (done) {
testHtmlPlugin({
entry: {
util: path.join(__dirname, 'fixtures/util.js'),
@ -96,13 +96,13 @@ describe('HtmlWebpackPlugin', function() {
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
append: true,
inject: true,
template: path.join(__dirname, 'fixtures/plain.html')
})]
}, ['<script src="util_bundle.js?%hash%"', '<script src="app_bundle.js?%hash%"'], null, done);
});
it('allows you to append the assets to a html string', function (done) {
it('allows you to inject the assets into a html string', function (done) {
testHtmlPlugin({
entry: {
util: path.join(__dirname, 'fixtures/util.js'),
@ -113,13 +113,13 @@ describe('HtmlWebpackPlugin', function() {
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
append: ['util', 'app'],
inject: ['util', 'app'],
templateContent: fs.readFileSync(path.join(__dirname, 'fixtures/plain.html'), 'utf8')
})]
}, ['<script src="util_bundle.js?%hash%"', '<script src="app_bundle.js?%hash%"'], null, done);
});
it('allows you to append a specified asset to a given html file', function (done) {
it('allows you to inject a specified asset into a given html file', function (done) {
testHtmlPlugin({
entry: {
util: path.join(__dirname, 'fixtures/util.js'),
@ -130,7 +130,7 @@ describe('HtmlWebpackPlugin', function() {
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
append: ['app'],
inject: ['app'],
template: path.join(__dirname, 'fixtures/plain.html')
})]
}, ['<script src="app_bundle.js?%hash%"'], null, done);