-
Notifications
You must be signed in to change notification settings - Fork 3
/
vite.config.js
121 lines (104 loc) · 3.31 KB
/
vite.config.js
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
import { resolve } from 'node:path'
import { homedir } from 'node:os'
import { env } from 'node:process'
import { defineConfig } from 'vite'
import { createHtmlPlugin } from 'vite-plugin-html'
import { viteSingleFile } from 'vite-plugin-singlefile'
import browserslistToEsbuild from 'browserslist-to-esbuild'
import eslintPlugin from '@rollup/plugin-eslint'
const isProd = env?.NODE_ENV === 'production'
let outDir = env?.APP_BUILD_DIR || 'public'
// Prevents to output app files outside of the project root.
if (outDir.includes('../')) {
throw new Error('APP_BUILD_DIR (in the .env file) can’t be outside of the app root directory. Remove all `../` from `APP_BUILD_DIR`.')
}
// ESLint Options
const esLintOptions = {
cache: true, // cache is cleaned on `npm install`
cacheStrategy: 'content',
fix: env?.ES_LINT_AUTOFIX == 'true',
formatter: env?.ES_LINT_FORMATTER ?? 'stylish',
include: '**/*.+(js|mjs|ts)',
}
// HTML plugin option (https://github.com/vbenjs/vite-plugin-html#parameter-description)
const htmlOptions = {
minify: {
collapseWhitespace: true,
keepClosingSlash: false,
removeComments: true,
},
}
// Inline assets (https://github.com/richardtallent/vite-plugin-singlefile#config)
const singleFileOptions = {
useRecommendedBuildConfig: false,
inlinePattern: [
'js/helpers/idbDetect.js',
],
}
/**
* HTTPS, works well with Laravel Valet (macOS, https://github.com/laravel/docs/blob/master/valet.md#securing-sites-with-tls) and Laravel Herd (macOS, https://herd.laravel.com/docs/1/advanced-usage/securing-sites#securing-sites-with-tls).
* Not tested with other setups.
*/
const host = env?.SERVER_HOST ?? null
let https = env?.SERVER_HTTPS === 'true'
if (https && host && env.SERVER_CERTIFICATES_DIR) {
const certificatesPath = `${homedir()}/${env.SERVER_CERTIFICATES_DIR}/${host}`
https = {
key: `${certificatesPath}.key`,
cert: `${certificatesPath}.crt`,
}
}
export default defineConfig({
root: 'src',
css: {
devSourcemap: true,
},
build: {
envDir: './',
outDir: `../${outDir}`,
emptyOutDir: true,
cssCodeSplit: false, // might be reconsidered when app evolves
target: browserslistToEsbuild(),
modulePreload: {
polyfill: false,
},
define: {
idbAvailable: 'idbAvailable',
idbAvailabilityDetected: 'idbAvailabilityDetected',
},
rollupOptions: {
input: {
'js/burokku': resolve('./src/index.html'),
'js/helpers/idbDetect': resolve('./src/js/helpers/idbDetect.js'),
'js/utils/ServiceWorker': resolve('./src/js/utils/ServiceWorker/index.js'),
'block-service-worker': resolve('./src/js/service-worker.js'),
},
output: {
// Preserve filenames, needed for Service Worker caching.
entryFileNames: '[name].js', // JS entries
chunkFileNames: 'js/modules/[name].js', // JS chunks
assetFileNames: 'css/burokku.css', // other files (CSS only for now)
},
},
},
// envPrefix: ['VITE_'],
resolve: {
alias: {
'@': resolve(__dirname, './src'),
},
},
plugins: isProd ? [
createHtmlPlugin(htmlOptions),
viteSingleFile(singleFileOptions),
] : [
{
...eslintPlugin(esLintOptions),
enforce: 'pre',
},
],
server: {
open: env?.BROWSER_OPEN === 'true',
...(host ? { host } : null),
https,
},
})