-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP - preload CSS Inspired by webpack-contrib/mini-css-extract-plugin#344 * feat: preload css * test(TestCases): re-enable logging
- Loading branch information
Showing
14 changed files
with
74 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,10 +323,13 @@ class ExtractCssChunksPlugin { | |
} | ||
); | ||
const { insert } = this.options; | ||
const supportsPreload = | ||
'(function() { try { return document.createElement("link").relList.supports("preload"); } catch(e) { return false; }}());'; | ||
return Template.asString([ | ||
source, | ||
'', | ||
`// ${pluginName} CSS loading`, | ||
`var supportsPreload = ${supportsPreload}`, | ||
`var cssChunks = ${JSON.stringify(chunkMap)};`, | ||
'if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);', | ||
'else if(installedCssChunks[chunkId] !== 0 && cssChunks[chunkId]) {', | ||
|
@@ -340,8 +343,7 @@ class ExtractCssChunksPlugin { | |
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");', | ||
'for(var i = 0; i < existingStyleTags.length; i++) {', | ||
|
@@ -352,8 +354,8 @@ class ExtractCssChunksPlugin { | |
]), | ||
'}', | ||
'var linkTag = document.createElement("link");', | ||
'linkTag.rel = "stylesheet";', | ||
'linkTag.type = "text/css";', | ||
'linkTag.rel = supportsPreload ? "preload": "stylesheet";', | ||
'supportsPreload ? linkTag.as = "style" : linkTag.type = "text/css";', | ||
'linkTag.onload = resolve;', | ||
'linkTag.onerror = function(event) {', | ||
Template.indent([ | ||
|
@@ -383,7 +385,18 @@ class ExtractCssChunksPlugin { | |
: 'var head = document.getElementsByTagName("head")[0]; head.appendChild(linkTag)', | ||
]), | ||
'}).then(function() {', | ||
Template.indent(['installedCssChunks[chunkId] = 0;']), | ||
Template.indent([ | ||
'installedCssChunks[chunkId] = 0;', | ||
'if(supportsPreload) {', | ||
Template.indent([ | ||
'var execLinkTag = document.createElement("link");', | ||
`execLinkTag.href = ${mainTemplate.requireFn}.p + ${linkHrefPath};`, | ||
'execLinkTag.rel = "stylesheet";', | ||
'execLinkTag.type = "text/css";', | ||
'document.body.appendChild(execLinkTag);', | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
SuperOleg39
|
||
]), | ||
'}', | ||
]), | ||
'}));', | ||
]), | ||
'}', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
body { | ||
background: red; | ||
background-image: url(cd0bb358c45b584743d8ce4991777c42.svg); | ||
background-image: url(c9e192c015437a21dea1faa1d30f4941.svg); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
body { | ||
background: green; | ||
background-image: url(../../cd0bb358c45b584743d8ce4991777c42.svg); | ||
background-image: url(../../c9e192c015437a21dea1faa1d30f4941.svg); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
body { | ||
background: red; | ||
background-image: url(../cd0bb358c45b584743d8ce4991777c42.svg); | ||
background-image: url(../c9e192c015437a21dea1faa1d30f4941.svg); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
body { | ||
background: red; | ||
background-image: url(/static/img/cd0bb358c45b584743d8ce4991777c42.svg); | ||
background-image: url(/static/img/c9e192c015437a21dea1faa1d30f4941.svg); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
body { | ||
background: red; | ||
background-image: url(/static/img/cd0bb358c45b584743d8ce4991777c42.svg); | ||
background-image: url(/static/img/c9e192c015437a21dea1faa1d30f4941.svg); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
// eslint-disable-next-line | ||
import 'bootstrap.css'; | ||
import './style.css'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hi!
It's look like breaking change, because at line#346 is checking an existing link, and prevent append new link at this case.
But now, even if the link exists on the page, we get an additional unnecessary link.
Maybe better to add extra flag, checking that line#357 already append preload link?