-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
162 lines (147 loc) · 3.33 KB
/
Copy pathvite.config.ts
File metadata and controls
162 lines (147 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { URL, fileURLToPath } from 'node:url';
import { readdirSync } from 'node:fs';
import { defineConfig } from 'vite';
import viteReact from '@vitejs/plugin-react';
import preload from 'vite-plugin-preload';
import type { Plugin } from 'vite';
const MAX_PRELOADED_IMAGES = 5;
const metaTags = [
// {
// title: 'Almost Prohibited - Browse items from your favourite retailers',
// },
{
name: 'description',
content:
"Canada's upcoming aggregator for firearms, parts, and accessories",
},
{
name: 'viewport',
content: 'width=device-width, initial-scale=1.0',
},
{
charSet: 'UTF-8',
},
{
name: 'theme-color',
content: '#364fc7',
},
{
property: 'og:title',
content:
'Almost Prohibited - Browse items from your favourite retailers',
},
{
property: 'og:url',
content: 'https://almostprohibited.ca',
},
{
property: 'og:description',
content:
"Canada's upcoming aggregator for firearms, parts, and accessories",
},
{
property: 'og:image',
content: 'https://almostprohibited.ca/favicon.svg',
},
{
property: 'og:type',
content: 'website',
},
];
const linkTags = [
{
rel: 'shortcut icon',
href: '/favicon.ico',
},
{
rel: 'icon',
type: 'image/png',
href: '/favicon-96x96.png',
sizes: '96x96',
},
{
rel: 'icon',
type: 'image/svg+xml',
href: '/favicon.svg',
},
{
rel: 'apple-touch-icon',
href: '/apple-touch-icon.png',
sizes: '180x180',
},
{
rel: 'manifest',
href: '/site.webmanifest',
},
{
rel: 'preconnect',
href: 'https://fonts.googleapis.com',
},
{
rel: 'preconnect',
href: 'https://fonts.gstatic.com',
},
{
rel: 'stylesheet',
href: 'https://fonts.googleapis.com/css2?family=Google+Sans:ital,opsz,wght@0,17..18,400..700;1,17..18,400..700&display=swap',
},
];
function generateImageRetailerPreloads(): Array<string> {
const retailerImagePath = fileURLToPath(
new URL('./public/retailers', import.meta.url),
);
let imageCount = 0;
const preloadTags = readdirSync(retailerImagePath)
.filter((__) => {
// this should work out since retailers are displayed
// in alphabetical order
return imageCount++ < MAX_PRELOADED_IMAGES;
})
.map((imageName) => {
return `<link rel="preload" href="/retailers/${imageName}" as="image" />`;
});
return preloadTags;
}
// @ts-expect-error
function preloadAssets(): Plugin {
return {
name: 'preload-images',
transformIndexHtml(html, _) {
const tags = [...generateImageRetailerPreloads()];
return html.replace('</head>', `${tags.join('\n')}\n</head>`);
},
};
}
function generateTags(
elementType: string,
tagAttributes: Array<object>,
): Array<string> {
return tagAttributes.map((tag) => {
const attributes = Object.entries(tag).map(
([key, value]) => `${key}="${value}"`,
);
return `<${elementType} ${attributes.join(' ')} />`;
});
}
function insertTags(): Plugin {
return {
name: 'insert-tags',
transformIndexHtml(html, _) {
const tags = [
'<title>Almost Prohibited - Browse items from your favourite retailers</title>',
...generateTags('meta', metaTags),
...generateTags('link', linkTags),
];
return html.replace('</head>', `${tags.join('\n')}\n</head>`);
},
};
}
// https://vitejs.dev/config/
export default defineConfig({
plugins: [viteReact(), preload(), insertTags()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
});