This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
vite.config.ts
160 lines (142 loc) · 4.39 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
import million from "million/compiler";
import { defineConfig } from 'vitest/config'
import { createHtmlPlugin } from 'vite-plugin-html'
import { execaSync } from 'execa'
import ViteEslint from '@nabla/vite-plugin-eslint'
import ViteInspect from 'vite-plugin-inspect'
import ViteLegacy from '@vitejs/plugin-legacy'
import ViteReact from '@vitejs/plugin-react'
import ViteVisualizer from 'rollup-plugin-visualizer'
import { VitePluginFonts } from 'vite-plugin-fonts'
import type { ConfigEnv, UserConfig } from 'vite'
const millionConfig = {
auto: { rsc: true },
};
// https://vitejs.dev/config/
export default defineConfig(({command, mode}: ConfigEnv): UserConfig => {
// figure out commands
const isBuild = command === 'build';
// figure out modes
const isDev = mode === "development";
const isProd = mode === "production";
const isReport = mode === "report";
// figure out custom build options
const isTest = process.env.NODE_ENV === 'test';
const isLegacy = process.env.VITE_LEGACY || false;
const isGitpodBuild = process.env.GITPOD_WORKSPACE_URL || false;
const isReplitBuild = process.env.REPL_SLUG || false;
const isStackblitzBuild = process.env.STACKBLITZ_ENV || false;
const isCodeSandboxBuild = process.env.CODESANDBOX_SSE || false;
const isGlitchBuild = process.env.PROJECT_REMIX_CHAIN || false;
const isCloudIdeBuild = isGitpodBuild || isReplitBuild || isStackblitzBuild || isCodeSandboxBuild || isGlitchBuild;
const isNetlifyBuild = process.env.NETLIFY || false;
const isNetlifyPreviewBuild = isNetlifyBuild && process.env.CONTEXT == 'deploy-preview';
const isNetlifyChannelBuild = isNetlifyBuild && process.env.CHANNEL !== undefined;
const config:UserConfig = {
base: "/",
mode,
plugins: [],
publicDir: "public",
clearScreen: true,
server: {
host: true,
port: 3000,
strictPort: true,
open: !isCloudIdeBuild,
},
build: {
outDir: "build",
assetsDir: "static",
sourcemap: !isDev,
rollupOptions: {},
manifest: false,
},
test: {
include: ["src/**/*.test.ts", "src/**/*.test.tsx"],
exclude: ['**/e2e-tests/**'],
},
preview: {
port: 3000,
}
};
config.plugins.push(
VitePluginFonts({
google: {
preconnect: true,
display: 'swap',
injectTo: 'head-prepend',
families: [
'Inter:wght@400;600;900',
'Lexend:wght@400;600;900',
'Open Sans:wght@400;600;900'
],
},
}),
ViteEslint(),
ViteInspect(),
ViteReact({
fastRefresh: !isTest,
// Exclude storybook stories
exclude: /\.stories\.(t|j)sx?$/,
include: "src/**/*.(t|j)sx?",
}),
createHtmlPlugin({
minify: isProd && isBuild,
inject: {
data: {
title: `Hot Open Sauced v${process.env.npm_package_version}`,
date: new Date().toISOString(),
},
},
})
);
isBuild && isLegacy && config.plugins.push(
ViteLegacy({
targets: [
'defaults',
'not IE 11'
]
})
);
isReport && config.plugins.push(
ViteVisualizer({
filename: "./build/bundle.html",
open: true,
gzipSize: true
})
);
// cloud container shared and specific build options
isCloudIdeBuild && (config.server.hmr = {
port: 443,
});
if (isGitpodBuild) {
config.base = process.env.GITPOD_WORKSPACE_URL;
}
if (isReplitBuild) {
config.base = `https://${process.env.REPL_SLUG}-${process.env.REPL_OWNER}.repl.co`;
}
if (isStackblitzBuild) {
const { stdout } = execaSync("hostname");
config.base = `https://${stdout}--${config.server.port}.local.webcontainer.io/`;
}
if (isGlitchBuild) {
config.base = `https://${process.env.PROJECT_DOMAIN}.glitch.me/`;
}
if (isCodeSandboxBuild) {
const [type, sandbox, id] = process.env.HOSTNAME.split('-');
config.base = `https://${id}.${type}.code${sandbox}.io/`;
}
// netlify build configuration (see netlify.toml)
if (isNetlifyBuild) {
config.base = `${process.env.URL}/`;
if (isNetlifyPreviewBuild) {
config.base = `${process.env.DEPLOY_PRIME_URL}/`;
} else {
if (isNetlifyChannelBuild && ['alpha', 'beta'].includes(process.env.CHANNEL)) {
const {protocol, hostname} = new URL(process.env.URL);
config.base = `${protocol}//${process.env.CHANNEL}.${hostname}/`;
}
}
}
return million.next(config,millionConfig);
})