forked from Leniolabs/layoutit-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
187 lines (182 loc) · 5.81 KB
/
vite.config.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { defineConfig } from 'vite'
import type { ResolvedConfig, Plugin } from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import viteComponentsPlugin from 'vite-plugin-components'
import { VitePWA } from 'vite-plugin-pwa'
import { OutputAsset, OutputChunk } from 'rollup'
const LayouitPlugin = (): Plugin => {
let viteConfig: ResolvedConfig
const mainEntryName = 'LAYOUTIT-MAIN-ENTRY'
const transformDynamicImport = (base: string, entry: string, map: Map<string, OutputAsset | OutputChunk>) => {
const module = map.get(entry)
if (module && module['isDynamicEntry'] === true) {
return `<link rel="modulepreload" href="${entry.startsWith('/') ? entry : `${base}${entry}`}" />`
}
return null
}
return {
name: 'vite-layouit-grid-inject',
enforce: 'post',
apply: 'build',
configResolved(config) {
viteConfig = config
},
async buildEnd(error) {
if (error)
throw error
},
transformIndexHtml: {
enforce: 'post',
transform(html, { bundle }) {
/* VITE WILL NOT EXPOSE CSS NOR ASSETS */
if (bundle) {
const assets = Object.entries(bundle).reduce((acc, [key, value]) => {
if (value['isEntry'] === true) {
acc.set(mainEntryName, value as OutputChunk)
} else {
acc.set(key, value as OutputAsset | OutputChunk)
}
return acc
}, new Map<string, OutputAsset | OutputChunk>())
const mainEntry: OutputChunk | undefined = assets.has(mainEntryName)
? (assets.get(mainEntryName) as OutputChunk)
: undefined
if (mainEntry) {
const { base = '/' } = viteConfig
const { fileName, dynamicImports = [] } = mainEntry
const dynamicImportsEntries = dynamicImports.map((dy) => {
return transformDynamicImport(base, dy, assets)
})
const moduleName = fileName.startsWith('/') ? fileName : `${base}${fileName}`
const module = `<script type="module" crossorigin src="${moduleName}"></script>`
// return html.replace(
// module,
// `<link rel="modulepreload" href="${moduleName}" />\n${
// dynamicImportsEntries ? dynamicImportsEntries.filter((dy) => !!dy).join('\n') : ''
// }\n${module}`
// )
let result = html.replace(
module,
`<link rel="modulepreload" href="${moduleName}" />\n${dynamicImportsEntries ? dynamicImportsEntries.filter((dy) => !!dy).join('\n') : ''
}\n${module}`
)
// the css will be something like this
// '<link rel="stylesheet" href="/assets/index.c517effc.css">'
// we just replace it with the new one non blocking
result = result.replace(
/<link rel="stylesheet" href="(.*?)">/g,
`<link
rel="preload"
as="style"
onload="this.onload=null;this.rel='stylesheet'"
href="$1"
/>
<noscript><link rel="stylesheet" href="$1" /></noscript>`
)
return result
}
}
return html
},
},
}
}
export default defineConfig({
plugins: [
vuePlugin({
refTransform: true
}),
viteComponentsPlugin(),
LayouitPlugin(),
VitePWA({
base: '/',
scope: '/',
registerType: 'autoUpdate',
includeAssets: [
'favicon.ico',
'robots.txt',
'img/icons/icon-44x44.png',
'img/icons/safari-pinned-tab.svg',
'img/screenshot.png',
],
manifest: {
name: 'Interactive CSS Grid Generator',
// short_name: 'CSS Grid Generator',
short_name: 'LayoutIt Grid',
description: 'Layoutit Grid is an interactive open source CSS Grid generator.',
display: 'standalone',
orientation: 'portrait-primary',
background_color: '#1D032D',
lang: 'en',
theme_color: '#1D032D',
categories: ['education', 'personalization', 'productivity', 'utilities'],
screenshots: [
{
src: 'img/screenshot.png',
sizes: '1280x800',
type: 'image/png',
},
],
icons: [
{
src: 'img/icons/icon-128x128.png',
sizes: '128x128',
purpose: 'maskable',
type: 'image/png',
},
{
src: 'img/icons/android-icon-36x36.png',
sizes: '36x36',
type: 'image/png',
purpose: 'maskable',
density: '0.75',
},
{
src: 'img/icons/android-icon-48x48.png',
sizes: '48x48',
type: 'image/png',
purpose: 'maskable',
density: '1.0',
},
{
src: 'img/icons/android-icon-72x72.png',
sizes: '72x72',
type: 'image/png',
purpose: 'maskable',
density: '1.5',
},
{
src: 'img/icons/android-icon-96x96.png',
sizes: '96x96',
type: 'image/png',
purpose: 'maskable',
density: '2.0',
},
{
src: 'img/icons/android-icon-144x144.png',
sizes: '144x144',
type: 'image/png',
purpose: 'maskable',
density: '3.0',
},
{
src: 'img/icons/android-icon-192x192.png',
sizes: '192x192',
type: 'image/png',
purpose: 'maskable',
density: '4.0',
},
{
src: 'img/icons/android-icon-512x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'any maskable',
},
],
},
}),
],
css: {
postcss: '.',
},
})