Skip to content

Commit 8a94a65

Browse files
committed
Merge branch 'feature/webpack-entrypoint-preload'
2 parents 081eeeb + 7e33ce5 commit 8a94a65

14 files changed

+150
-16
lines changed

src/entry-point-webpack-preload.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function addLinkForEntryPointWebpackPreload(
2222

2323
// Prepare link tags for HtmlWebpackPlugin
2424
const publicPath = getPublicPath(compilation, htmlPluginData);
25-
const entryHtmlTagObjectMap = generateHtmlTagObject(entryFileMap, publicPath);
25+
const entryHtmlTagObjectMap = generateHtmlTagObject(entryFileMap, publicPath, compilation);
2626

2727
// Related files's link tags should follow parent script tag (the entries scripts' tag)
2828
// according to this [blog](https://web.dev/priority-hints/#using-preload-after-chrome-95).
@@ -89,20 +89,32 @@ function prepareEntryFileMap(
8989
* @param publicPath
9090
* @returns
9191
*/
92+
<<<<<<< HEAD
9293
function generateHtmlTagObject(entryFileMap: Map<string, Set<string>>, publicPath: string): Map<EntryName, Set<HtmlTagObject>> {
94+
=======
95+
function generateHtmlTagObject(entryFileMap: Map<string, Set<string>>, publicPath: string, compilation: Compilation): Map<EntryName, Set<HtmlTagObject>> {
96+
>>>>>>> feature/webpack-entrypoint-preload
9397
const map = new Map();
9498
for (const [key, filesNames] of entryFileMap) {
9599
map.set(key, [...filesNames].map(fileName => {
96100
const href = `${publicPath}${fileName}`;
97101
const as = getTypeOfResource(fileName);
102+
<<<<<<< HEAD
98103
const crossOrigin = as === 'font';
104+
=======
105+
const crossOrigin = as === 'font' ? 'anonymous' : compilation.outputOptions.crossOriginLoading;
106+
>>>>>>> feature/webpack-entrypoint-preload
99107
let attributes: HtmlTagObject['attributes'] = {
100108
rel: 'preload',
101109
href,
102110
as
103111
}
104112
if (crossOrigin) {
113+
<<<<<<< HEAD
105114
attributes = { ...attributes, crossorigin: undefined }
115+
=======
116+
attributes = { ...attributes, crossorigin: crossOrigin }
117+
>>>>>>> feature/webpack-entrypoint-preload
106118
}
107119
return {
108120
tagName: 'link',
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
body {
2+
background-color: blue;
3+
font-family: Roboto, sans-serif;
4+
}
5+
6+
@font-face {
7+
font-family: 'Roboto';
8+
font-weight: 400;
9+
src: url('../../../../Roboto-Regular.woff2') format('woff2'),
10+
url('../../../..//Roboto-Regular.woff') format('woff');
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './await-imported-by-async-chunk.css';
2+
export async function awaitImportByAsyncChunk() {
3+
console.log('awaitImportByAsyncChunk');
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
body {
2+
background-color: green;
3+
font-family: Roboto, sans-serif;
4+
}
5+
6+
@font-face {
7+
font-family: 'Roboto';
8+
font-weight: 400;
9+
src: url('../../../../Roboto-Regular.woff2') format('woff2'),
10+
url('../../../..//Roboto-Regular.woff') format('woff');
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import './await-imported-by-intial-chunk.css';
2+
export async function awaitImportByInitialChunk() {
3+
const { awaitImportByAsyncChunk } = await import(/* webpackPreload: true */'./await-imported-by-async-chunk');
4+
awaitImportByAsyncChunk();
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
async function main() {
2+
const { awaitImportByInitialChunk } = await import(/* webpackPreload: true */ './await-imported-by-intial-chunk');
3+
awaitImportByInitialChunk();
4+
}
5+
6+
main();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title><%= htmlWebpackPlugin.options.title %></title>
6+
</head>
7+
<body>
8+
foo
9+
</body>
10+
</html>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { webpack } from "webpack";
2+
import { getWebpackConfig } from "../../webpack-base-config";
3+
import path from 'path';
4+
import fs from 'fs';
5+
import { expectSuccessfulBuild } from "../utils";
6+
describe('HTMLWebpackInjectPreload test entry point webpack preload with cross origin loading', () => {
7+
it('should add crossorigin attributions when wepack output crossOriginLoading is \'anonymous\'', done => {
8+
let config = getWebpackConfig('./fixtures/entry.js');
9+
if (config.output) {
10+
config.output.crossOriginLoading = 'anonymous';
11+
}
12+
const compiler = webpack(config);
13+
14+
compiler.run((err, stats) => {
15+
expectSuccessfulBuild(err, stats);
16+
17+
const html = fs.readFileSync(
18+
path.join(__dirname, 'dist/index.html'),
19+
'utf8',
20+
);
21+
const preloadRegex = /<link[^>]+rel=["']preload["'][^>]+as=["']([^"']+)["'][^>]*>/;
22+
const globalPreloadRegex = new RegExp(preloadRegex, "gm");
23+
const result = html.match(globalPreloadRegex);
24+
expect(result).not.toBeNull();
25+
expect(result?.length).toBe(2);
26+
27+
const corssOriginRegex = /<link[^>]+rel=["']preload["'][^>]+crossorigin=["']([^"']+)["'][^>]*>/;
28+
const cssPreload = result![0].match(corssOriginRegex);
29+
expect(cssPreload![1]).toBe('anonymous');
30+
const jsPreload = result![1].match(corssOriginRegex);
31+
expect(jsPreload![1]).toBe('anonymous');
32+
done();
33+
});
34+
});
35+
36+
it('should add crossorigin attributions when wepack output crossOriginLoading is \'use-credentials\'', done => {
37+
let config = getWebpackConfig('./fixtures/entry.js');
38+
if (config.output) {
39+
config.output.crossOriginLoading = 'use-credentials';
40+
}
41+
const compiler = webpack(config);
42+
43+
compiler.run((err, stats) => {
44+
expectSuccessfulBuild(err, stats);
45+
46+
const html = fs.readFileSync(
47+
path.join(__dirname, 'dist/index.html'),
48+
'utf8',
49+
);
50+
const preloadRegex = /<link[^>]+rel=["']preload["'][^>]+as=["']([^"']+)["'][^>]*>/;
51+
const globalPreloadRegex = new RegExp(preloadRegex, "gm");
52+
const result = html.match(globalPreloadRegex);
53+
expect(result).not.toBeNull();
54+
expect(result?.length).toBe(2);
55+
56+
const corssOriginRegex = /<link[^>]+rel=["']preload["'][^>]+crossorigin=["']([^"']+)["'][^>]*>/;
57+
const cssPreload = result![0].match(corssOriginRegex);
58+
expect(cssPreload![1]).toBe('use-credentials');
59+
const jsPreload = result![1].match(corssOriginRegex);
60+
expect(jsPreload![1]).toBe('use-credentials');
61+
done();
62+
});
63+
});
64+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
body {
2+
background-color: blue;
3+
font-family: Roboto, sans-serif;
4+
}
5+
6+
@font-face {
7+
font-family: 'Roboto';
8+
font-weight: 400;
9+
src: url('../../../../Roboto-Regular.woff2') format('woff2'),
10+
url('../../../..//Roboto-Regular.woff') format('woff');
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './await-imported-by-async-chunk.css';
12
export async function awaitImportByAsyncChunk() {
23
console.log('awaitImportByAsyncChunk');
34
}

0 commit comments

Comments
 (0)