forked from open-sauced/open-sauced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
181 lines (164 loc) · 4.51 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
import { defineConfig } from 'vite'
import ViteReact from '@vitejs/plugin-react'
import ViteEslint from '@nabla/vite-plugin-eslint'
import { createHtmlPlugin } from 'vite-plugin-html'
import ViteInspect from 'vite-plugin-inspect'
import ViteLegacy from '@vitejs/plugin-legacy'
import { VitePWA, VitePWAOptions } from 'vite-plugin-pwa'
import ViteReplace from '@rollup/plugin-replace'
import ViteTimeReporter from 'vite-plugin-time-reporter'
import ViteVisualizer from 'rollup-plugin-visualizer'
import { sync } from 'execa'
import type { ConfigEnv, UserConfig } from 'vite'
// 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 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,
},
preview: {
port: 3000,
}
};
config.plugins.push(
ViteTimeReporter(),
ViteEslint(),
ViteInspect(),
ViteReact({
fastRefresh: !isTest,
// Exclude storybook stories
exclude: /\.stories\.(t|j)sx?$/,
// Only .jsx files
include: "**/*.jsx",
babel: {
plugins: [
[
'babel-plugin-styled-components',
{
displayName: true,
fileName: false
}
]
]
}
}),
createHtmlPlugin({
minify: isProd && isBuild,
inject: {
data: {
title: `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
})
);
const pwaOptions: Partial<VitePWAOptions> = {
disable: !isProd,
includeAssets: [
'favicon.svg',
'favicon.ico',
'robots.txt',
'apple-touch-icon.png'
],
manifest: {
name: 'Open Sauced',
short_name: 'Open Sauced',
description: 'Open Sauced',
theme_color: '#FFFFFF',
icons: [
{
src: 'android-chrome-192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: 'android-chrome-512x512.png',
sizes: '512x512',
type: 'image/png',
},
],
},
registerType: 'autoUpdate',
strategies: 'injectManifest',
srcDir: 'src',
filename: 'claims-sw.ts',
injectManifest: {
maximumFileSizeToCacheInBytes: 5 * 1024 * 1024
}
};
const replaceOptions = {
preventAssignment: true,
__RELOAD_SW__: true,
};
config.plugins.push(
VitePWA(pwaOptions),
ViteReplace(replaceOptions),
);
// 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 } = sync("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/`;
}
return config;
});