Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit 4ff53e0

Browse files
committed
2 parents e19777f + 8c2b3bf commit 4ff53e0

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,14 @@ will be injected into the document `<head>`:
132132
<link rel="preload" as="script" href="chunk.d15e7fdfc91b34bb78c4.js">
133133
```
134134

135-
You can also configure the plugin to preload all chunks (vendor, async, normal chunks) using
136-
`include`:
135+
You can also configure the plugin to preload all chunks (vendor, async, normal chunks) using `include: 'all'`, or only preload initial chunks with `include: 'initial'`:
137136

138137
```js
139138
plugins: [
140139
new HtmlWebpackPlugin(),
141140
new PreloadWebpackPlugin({
142141
rel: 'preload',
143-
include: 'all'
142+
include: 'all' // or 'initial'
144143
})
145144
]
146145
```

index.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,26 @@ const objectAssign = require('object-assign');
2323

2424
const flatten = arr => arr.reduce((prev, curr) => prev.concat(curr), []);
2525

26-
const isChunkBelongToHtml = (chunk, roots) => {
26+
const doesChunkBelongToHTML = (chunk, roots, visitedChunks) => {
27+
// Prevent circular recursion.
28+
// See https://github.com/GoogleChromeLabs/preload-webpack-plugin/issues/49
29+
if (visitedChunks[chunk.renderedHash]) {
30+
return false;
31+
}
32+
visitedChunks[chunk.renderedHash] = true;
33+
2734
for (const root of roots) {
28-
if (root.hash === chunk.renderedHash) return true;
35+
if (root.hash === chunk.renderedHash) {
36+
return true;
37+
}
2938
}
39+
3040
for (const parent of chunk.parents) {
31-
if (isChunkBelongToHtml(parent, roots)) return true;
41+
if (doesChunkBelongToHTML(parent, roots, visitedChunks)) {
42+
return true;
43+
}
3244
}
45+
3346
return false;
3447
};
3548

@@ -60,6 +73,12 @@ class PreloadPlugin {
6073
} catch (e) {
6174
extractedChunks = compilation.chunks;
6275
}
76+
} else if (options.include === 'initial') {
77+
try {
78+
extractedChunks = compilation.chunks.filter(chunk => chunk.isInitial());
79+
} catch (e) {
80+
extractedChunks = compilation.chunks;
81+
}
6382
} else if (options.include === 'all') {
6483
// Async chunks, vendor chunks, normal chunks.
6584
extractedChunks = compilation.chunks;
@@ -80,7 +99,8 @@ class PreloadPlugin {
8099
const publicPath = compilation.outputOptions.publicPath || '';
81100

82101
// Only handle the chunk import by the htmlWebpackPlugin
83-
extractedChunks = extractedChunks.filter(chunk => isChunkBelongToHtml(chunk, Object.values(htmlPluginData.assets.chunks)));
102+
extractedChunks = extractedChunks.filter(chunk => doesChunkBelongToHTML(
103+
chunk, Object.values(htmlPluginData.assets.chunks), {}));
84104

85105
flatten(extractedChunks.map(chunk => chunk.files)).filter(entry => {
86106
return this.options.fileBlacklist.every(regex => regex.test(entry) === false);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "preload-webpack-plugin",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Enhances html-webpack-plugin with link rel=preload wiring capabilities for scripts",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)