-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for lazy loading CSS using fetch and document.adoptedS…
…tyleSheets
- Loading branch information
Showing
13 changed files
with
159 additions
and
14 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
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
Binary file added
BIN
+959 Bytes
test/cases/js-import-css-lazy-url-fetch/expected/favicon.edda23bf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions
13
test/cases/js-import-css-lazy-url-fetch/expected/index.html
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Test</title> | ||
<link href="favicon.edda23bf.png" rel="icon"> | ||
<link href="style.7f152e84.css" rel="stylesheet" /> | ||
<script src="main.js" defer></script> | ||
</head> | ||
<body> | ||
<h1>Hello World!</h1> | ||
<p class="lazy">Lazy load CSS</p> | ||
</body> | ||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
test/cases/js-import-css-lazy-url-fetch/expected/style-dynamic.1f7039c8.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* extracted by HTMLBundler CSSLoader */.lazy{color:green} |
1 change: 1 addition & 0 deletions
1
test/cases/js-import-css-lazy-url-fetch/expected/style.7f152e84.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1{color:red} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Test</title> | ||
<link href="@images/favicons/favicon.png" rel="icon"> | ||
<link href="./style.scss" rel="stylesheet" /> | ||
<script src="main.js" defer></script> | ||
</head> | ||
<body> | ||
<h1>Hello World!</h1> | ||
<p class="lazy">Lazy load CSS</p> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import cssUrl from './style-dynamic.scss?url'; | ||
|
||
// universal way | ||
function lazyLoad(url) { | ||
const style = document.createElement('link'); | ||
style.rel = 'stylesheet'; | ||
style.href = url; | ||
document.head.appendChild(style); | ||
console.log('lazyLoad: ', { cssUrl }); | ||
} | ||
|
||
// Using fetch and document.adoptedStyleSheets | ||
// Browser compatibility (since early 2023): | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets#browser_compatibility | ||
|
||
// modern way, using promise | ||
function lazyLoad2(url) { | ||
fetch(url).then((response) => { | ||
response.text().then((css) => { | ||
const sheet = new CSSStyleSheet(); | ||
sheet.replaceSync(css); | ||
document.adoptedStyleSheets = [sheet]; | ||
console.log('lazyLoad2: ', { cssUrl }); | ||
}); | ||
}); | ||
} | ||
|
||
// modern way, using async await | ||
async function lazyLoad3(url) { | ||
const response = await fetch(url); | ||
const css = await response.text(); | ||
const sheet = new CSSStyleSheet(); | ||
sheet.replaceSync(css); | ||
document.adoptedStyleSheets = [sheet]; | ||
console.log('lazyLoad3: ', { cssUrl }); | ||
} | ||
|
||
//lazyLoad(cssUrl); | ||
//lazyLoad2(cssUrl); | ||
lazyLoad3(cssUrl); |
4 changes: 4 additions & 0 deletions
4
test/cases/js-import-css-lazy-url-fetch/src/style-dynamic.scss
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$color: green; | ||
.lazy { | ||
color: $color; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$color: red; | ||
h1 { | ||
color: $color; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const path = require('path'); | ||
const HtmlBundlerPlugin = require('@test/html-bundler-webpack-plugin'); | ||
|
||
module.exports = { | ||
mode: 'production', | ||
|
||
output: { | ||
path: path.join(__dirname, 'dist/'), | ||
}, | ||
|
||
resolve: { | ||
alias: { | ||
'@images': path.join(__dirname, '../../fixtures/images'), | ||
}, | ||
}, | ||
|
||
plugins: [ | ||
new HtmlBundlerPlugin({ | ||
entry: { | ||
index: './src/index.html', | ||
}, | ||
css: { | ||
filename: '[name].[contenthash:8].css', | ||
}, | ||
// test disable - OK | ||
preprocessor: false, | ||
// test output of lazy loaded CSS file | ||
verbose: true, | ||
}), | ||
], | ||
|
||
module: { | ||
rules: [ | ||
{ | ||
test: /\.s?css$/, | ||
use: ['css-loader', 'sass-loader'], | ||
}, | ||
{ | ||
test: /\.(png|jpe?g|ico|svg)$/, | ||
type: 'asset/resource', | ||
generator: { | ||
filename: '[name].[hash:8][ext]', | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
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