Skip to content

Commit bc5546a

Browse files
committed
Stage change
1 parent 5c09ebd commit bc5546a

File tree

8 files changed

+46
-16
lines changed

8 files changed

+46
-16
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"editor.formatOnSave": true,
3+
"editor.defaultFormatter": "esbenp.prettier-vscode",
34
"workbench.colorCustomizations": {
45
"statusBar.background": "#000000",
56
"statusBar.foreground": "#e7e7e7",

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ module.exports = {
3232
new HtmlWebpackInjectPreload({
3333
files: [
3434
{
35-
match: /.*\.woff2/,
35+
match: /.*\.woff2$/,
3636
attributes: {as: 'font', type: 'font/woff2', crossorigin: true },
3737
},
3838
{
39-
match: /vendors\.[a-z-0-9]*.css/,
39+
match: /vendors\.[a-z-0-9]*.css$/,
4040
attributes: {as: 'style' },
4141
},
4242
]

src/main.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,38 +81,51 @@ class HtmlWebpackInjectPreload implements WebpackPluginInstance {
8181
compilation: Compilation,
8282
htmlPluginData: HtmlWebpackPluginData,
8383
) {
84+
//Get assets name
8485
const assets = new Set(Object.keys(compilation.assets));
8586
compilation.chunks.forEach(chunk => {
8687
chunk.files.forEach((file: string) => assets.add(file));
8788
});
8889

90+
//Find first link index to inject before
8991
const linkIndex = htmlPluginData.headTags.findIndex(
9092
tag => tag.tagName === 'link',
9193
);
9294

9395
assets.forEach(asset => {
9496
for (let index = 0; index < this.options.files.length; index++) {
9597
const file = this.options.files[index];
96-
let href = file.attributes ? file.attributes.href : false;
97-
if (!href) {
98-
href = asset;
99-
}
100-
101-
href = href[0] === '/' ? href : '/' + href;
10298

10399
if (file.match.test(asset)) {
100+
let href =
101+
file.attributes && file.attributes.href
102+
? file.attributes.href
103+
: false;
104+
if (href === false || typeof href === 'undefined') {
105+
href = asset;
106+
}
107+
href = href[0] === '/' ? href : '/' + href;
108+
104109
const preload: HtmlTagObject = {
105110
tagName: 'link',
106-
attributes: Object.assign(file.attributes, {rel: 'preload', href}),
111+
attributes: Object.assign(
112+
{
113+
rel: 'preload',
114+
href,
115+
},
116+
file.attributes,
117+
),
107118
voidTag: true,
108119
meta: {
109-
plugin: 'html-webpack-inject-preload'
110-
}
120+
plugin: 'html-webpack-inject-preload',
121+
},
111122
};
112123

113124
if (linkIndex > -1) {
125+
//before link
114126
htmlPluginData.headTags.splice(linkIndex, 0, preload);
115127
} else {
128+
// before everything
116129
htmlPluginData.headTags.unshift(preload);
117130
}
118131
}

test/Roboto-Bold.woff2

63.2 KB
Binary file not shown.

test/Roboto-Regular.woff

87.7 KB
Binary file not shown.

test/expected.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!doctype html><html><head><meta charset="utf-8"><title>Webpack App</title><meta name="viewport" content="width=device-width,initial-scale=1"><script defer="defer" src="/main.js"></script><link as="style" href="/test-alt.css" rel="preload"><link as="font" type="font/woff2" crossorigin rel="preload" href="/Roboto-Regular.woff2"><link href="/main.css" rel="stylesheet"></head><body></body></html>
1+
<!doctype html><html><head><meta charset="utf-8"><title>Webpack App</title><meta name="viewport" content="width=device-width,initial-scale=1"><script defer="defer" src="/main.js"></script><link rel="preload" href="test-alt.css" as="style"><link rel="preload" href="/Roboto-Bold.woff2" as="font" type="font/woff2" crossorigin><link rel="preload" href="/Roboto-Regular.woff" as="font" type="font/woff" crossorigin><link rel="preload" href="/Roboto-Regular.woff2" as="font" type="font/woff2" crossorigin><link href="/main.css" rel="stylesheet"></head><body></body></html>

test/index.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
import HtmlWebpackInjectPreload from '../src/main';
22
import HtmlWebpackPlugin from 'html-webpack-plugin';
33
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
4-
import webpack, { WebpackPluginInstance } from 'webpack';
4+
import webpack, {WebpackPluginInstance} from 'webpack';
55
import path from 'path';
66
import fs from 'fs';
77

88
const options: HtmlWebpackInjectPreload.Options = {
99
files: [
1010
{
11-
match: /.*\.woff2/,
11+
match: /.*\.woff2$/,
1212
attributes: {
1313
as: 'font',
1414
type: 'font/woff2',
1515
crossorigin: true,
1616
},
1717
},
1818
{
19-
match: /.*\.css/,
19+
match: /.*\.woff$/,
20+
attributes: {
21+
as: 'font',
22+
type: 'font/woff',
23+
crossorigin: true,
24+
},
25+
},
26+
{
27+
match: /.*\.css$/,
2028
attributes: {as: 'style', href: 'test-alt.css'},
2129
},
2230
{

test/style.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
@font-face {
22
font-family: 'Roboto';
33
font-weight: 400;
4-
src: url('./Roboto-Regular.woff2') format('woff2');
4+
src: url('./Roboto-Regular.woff2') format('woff2'),
5+
url('./Roboto-Regular.woff') format('woff');
56
}
67

8+
@font-face {
9+
font-family: 'Roboto';
10+
font-weight: 700;
11+
src: url('./Roboto-Bold.woff2') format('woff2');
12+
}
13+
14+
715
body {
816
font-family: Roboto, sans-serif;
917
background-color: red;

0 commit comments

Comments
 (0)