Skip to content

Commit

Permalink
feat: link preload support (webpack-contrib#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii.Sas authored and Andrii.Sas committed Dec 30, 2019
1 parent 1ffc393 commit 116392a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class MiniCssExtractPlugin {
filename: DEFAULT_FILENAME,
moduleFilename: () => this.options.filename || DEFAULT_FILENAME,
ignoreOrder: false,
cssPreload: true,
},
options
);
Expand Down Expand Up @@ -338,7 +339,7 @@ class MiniCssExtractPlugin {
Template.indent([
'var tag = existingLinkTags[i];',
'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");',
'if(tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return resolve();',
'if((tag.rel === "stylesheet" || tag.rel === "preload") && (dataHref === href || dataHref === fullhref)) return resolve();',
]),
'}',
'var existingStyleTags = document.getElementsByTagName("style");',
Expand All @@ -350,9 +351,46 @@ class MiniCssExtractPlugin {
]),
'}',
'var linkTag = document.createElement("link");',
this.options.cssPreload
? Template.asString([
'var isPreloadSupported = (function() {',
Template.indent([
'try { return linkTag.relList.supports("preload"); }',
'catch(e) { return false; }',
]),
'}());',
'if (isPreloadSupported) {',
Template.indent([
'linkTag.rel = "preload";',
'linkTag.as = "style";',
'linkTag.onload = function() {',
Template.indent([
'linkTag.rel = "stylesheet";',
'linkTag.onload = null;',
'resolve();',
]),
'};',
]),
'} else {',
Template.indent([
'linkTag.rel = "stylesheet";',
'linkTag.type = "text/css";',
'linkTag.media = "print";',
'linkTag.onload = function() {',
Template.indent([
'linkTag.media = "all";',
'linkTag.onload = null;',
'resolve();',
]),
'};',
]),
'}',
])
: Template.asString([
'linkTag.rel = "stylesheet";',
'linkTag.type = "text/css";',
'linkTag.onload = resolve;',
]),
'linkTag.onerror = function(event) {',
Template.indent([
'var request = event && event.target && event.target.src || fullhref;',
Expand Down
3 changes: 3 additions & 0 deletions src/plugin-options.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
},
"ignoreOrder": {
"type": "boolean"
},
"cssPreload": {
"type": "boolean"
}
}
}
5 changes: 5 additions & 0 deletions test/manual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<p>An error should have appeared.</p>
<p>Now if you turn the network back on and click it again, it should turn aqua.</p>
</div>
<div class="test lazy-link-preload">
<p>Lazy CSS: Must be red.</p>
<p><button class="lazy-link-preload-button">Pressing this button</button> displays an alert with check for link preload</p>
<p>After click on alert should turn green.</p>
</div>
<div class="test preloaded-css1">
<p>Preloaded CSS: Must be green.</p>
<p><button class="preloaded-button1">Pressing this button</button> displays an alert and should turn red.</p>
Expand Down
17 changes: 17 additions & 0 deletions test/manual/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ const makeButton = (className, fn, shouldDisable = true) => {
makeButton('.lazy-button', () => import('./lazy.js'));
makeButton('.lazy-button2', () => import('./lazy2.css'));

makeButton('.lazy-link-preload-button', () => {
const promise = import('./lazy-link-preload.css');
const linkElement = Array.from(document.head.children)
.slice(-2)
.find((el) => {
return el.tagName === 'LINK';
});

alert(
`Is css preloaded by <link rel="preload"/>? - ${
linkElement.getAttribute('rel') === 'preload' ? 'yes' : 'no'
}`
);

return promise;
});

makeButton('.preloaded-button1', () =>
import(/* webpackChunkName: "preloaded1" */ './preloaded1')
);
Expand Down
3 changes: 3 additions & 0 deletions test/manual/src/lazy-link-preload.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.lazy-link-preload {
background: lightgreen;
}

0 comments on commit 116392a

Please sign in to comment.