Skip to content
Closed
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
40 changes: 38 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ const updateSrcElement = (options, tag) => {
return tag;
};

const addCustomResourceHints = (options, scripts) => {
let hints = [];
scripts.forEach(script => {
if (options.dynamicChunks.preload) {
hints.push(createResourceHint('preload', null, script));
} else if (options.dynamicChunks.prefetch) {
hints.push(createResourceHint('prefetch', null, script));
}
});
return hints;
};

const addResourceHints = (options, tags) => {
return tags
.filter(hasScriptName)
Expand All @@ -133,13 +145,13 @@ const addResourceHints = (options, tags) => {
);
};

const createResourceHint = (rel, tag) => {
const createResourceHint = (rel, tag, customPath) => {
return {
tagName: 'link',
closeTag: true,
attributes: {
rel: rel,
href: getRawScriptName(tag),
href: customPath || getRawScriptName(tag),
as: 'script'
}
};
Expand Down Expand Up @@ -176,6 +188,30 @@ class ScriptExtHtmlWebpackPlugin {
addResourceHints(options, pluginArgs.body)
]);
}
if (shouldUpdateElements(options)) {
if (options.dynamicChunks) {
const chunkRegEx = /^chunk[.]/;
// Get the paths to dynamic chunks
const extractedChunks = compilation
.chunks
.reduce((chunks, chunk) => chunks.concat(chunk.files), [])
.filter(chunk => chunkRegEx.test(chunk));
// Default to 'head' as the position
if (options.dynamicChunks.position === 'body') {
debug(`${EVENT}: injecting <body> resource hints`);
pluginArgs.body = concat([
pluginArgs.body,
addCustomResourceHints(options, extractedChunks)
]);
} else {
debug(`${EVENT}: injecting <head> resource hints`);
pluginArgs.head = concat([
pluginArgs.head,
addCustomResourceHints(options, extractedChunks)
]);
}
}
}
debug(`${EVENT}: completed`);
callback(null, pluginArgs);
} catch (err) {
Expand Down