-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
122 lines (113 loc) · 3.34 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
import type { Plugin } from 'vite'
import { viteSingleFile as singleFile } from 'vite-plugin-singlefile'
import mockDevServer from 'vite-plugin-mock-dev-server'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { browserslistToTargets } from 'lightningcss'
import { minify } from 'html-minifier-terser'
import browserslist from 'browserslist'
import { defineConfig } from 'vite'
import fs from 'node:fs/promises'
import path from 'node:path'
import os from 'node:os'
import { data } from './preview/mock/data'
let htmlMinify = (): Plugin => ({
transformIndexHtml: async (html): Promise<void> => {
try {
await minify(html, {
removeStyleLinkTypeAttributes: true,
removeScriptTypeAttributes: true,
removeRedundantAttributes: true,
collapseWhitespace: true,
useShortDoctype: true,
removeComments: true,
minifyCSS: true,
minifyJS: true,
})
} catch (error) {
console.error('HTML minification failed', error)
}
},
name: 'vite-plugin-html-minify',
})
let injectBeforeHead = (html: string): Plugin => ({
transformIndexHtml: (htmlValue: string) =>
htmlValue.replace('</head>', `${html}</head>`),
name: 'vite-plugin-inject-before-head',
})
let createFilePlugin = (filename: string, content: string): Plugin => ({
generateBundle: async () => {
let outputDirectory = path.resolve(import.meta.dirname, 'dist')
try {
await fs.mkdir(outputDirectory, { recursive: true })
let filePath = path.join(outputDirectory, filename)
await fs.writeFile(filePath, content, 'utf8')
} catch (error) {
console.error(`Creation file ${filename}:`, error)
}
},
name: 'vite-plugin-create-file',
apply: 'build',
})
let isDocumentation = process.env.DOCS === 'true'
export default defineConfig({
plugins: [
mockDevServer({
include: ['preview/mock/**/*.mock.ts'],
prefix: ['^/data.json$'],
}),
...(isDocumentation
? [
injectBeforeHead(
`<script>window.data = ${JSON.stringify(data)};</script>`,
),
injectBeforeHead(
'<script defer src="https://analytics.azat.io/script.js" data-website-id="43d46bcc-915b-46c0-92b4-9e290eb8a5dc"></script></head>',
),
createFilePlugin(
'_redirects',
'https://todoctor.netlify.app/* https://todoctor.azat.io/:splat 301!',
),
]
: [
singleFile({
inlinePattern: ['**/*.js', '**/*.css'],
useRecommendedBuildConfig: true,
removeViteModuleLoader: true,
deleteInlinedFiles: true,
}),
]),
htmlMinify(),
svelte(),
],
css: {
lightningcss: {
targets: browserslistToTargets(
browserslist(null, {
config: path.join(import.meta.dirname, '.browserslistrc'),
}),
),
},
transformer: 'lightningcss',
},
resolve: {
alias: {
'~': path.join(import.meta.dirname, 'preview'),
},
extensions: ['.js', '.ts', '.svelte', '.css'],
},
build: {
rollupOptions: {
external: ['data.json'],
},
outDir: path.join(import.meta.dirname, 'dist'),
},
server: {
host: os.networkInterfaces().eth0?.[0].address,
port: 3000,
},
root: path.join(import.meta.dirname, 'preview'),
esbuild: {
legalComments: 'none',
},
base: './',
})