From 0cf7f7ac783c4c584f0d944cc59412ad6ae27cb8 Mon Sep 17 00:00:00 2001 From: Ran Ding Date: Sat, 26 Jan 2019 02:39:07 -0800 Subject: [PATCH] Support inserting tags to arbitrary places --- README.md | 18 ++++++++++++++++++ index.js | 7 ++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c9831cc..db364f7 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,24 @@ plugins: [ }) ``` +## Control where the generated tags are inserted in the result HTML + +By default `PreloadWebpackPlugin` will insert the preload/prefetch tags in ``. +You can control the inserting point by overriding the default option. + +```javascript +plugins: [ + new HtmlWebpackPlugin({ + filename: 'index.html', + template: 'src/index.html', + chunks: ['main'] + }), + // I want to insert preload tags before this comment + new PreloadWebpackPlugin({ + insertTagsBefore: '', + }) +``` + Resource Hints --------------------- diff --git a/index.js b/index.js index fbdd58e..79a2d6b 100644 --- a/index.js +++ b/index.js @@ -79,6 +79,7 @@ const defaultOptions = { include: 'asyncChunks', fileBlacklist: [/\.map/], excludeHtmlNames: [], + insertTagsBefore: '' }; class PreloadPlugin { @@ -281,9 +282,9 @@ class PreloadPlugin { filesToInclude+= `\n`; } }); - if (htmlPluginData.html.indexOf('') !== -1) { - // If a valid closing is found, update it to include preload/prefetch tags - htmlPluginData.html = htmlPluginData.html.replace('', filesToInclude + ''); + if (htmlPluginData.html.indexOf(this.options.insertTagsBefore) !== -1) { + // If a valid inserting point is found (default to ), update it to include preload/prefetch tags + htmlPluginData.html = htmlPluginData.html.replace(this.options.insertTagsBefore, filesToInclude + this.options.insertTagsBefore); } else { // Otherwise assume at least a is present and update it to include a new htmlPluginData.html = htmlPluginData.html.replace('', '' + filesToInclude + '');