-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathvite.config.ts
More file actions
175 lines (163 loc) · 4.71 KB
/
vite.config.ts
File metadata and controls
175 lines (163 loc) · 4.71 KB
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
import { readFileSync } from 'node:fs';
import path from 'path';
import { analyzer } from 'vite-bundle-analyzer';
import mkcert from 'vite-plugin-mkcert';
import { defineConfig, type LibraryOptions } from 'vite-plus';
const {
config: { sdkVersion },
} = JSON.parse(readFileSync(path.resolve(import.meta.dirname, 'package.json'), 'utf-8'));
type Lib = 'sdk' | 'page' | 'worker';
let prefix: string;
switch (process.env.ENV) {
case 'production':
prefix = '';
break;
case 'staging':
prefix = 'Staging-';
break;
default:
prefix = 'Dev-';
}
const libConfig: Record<Lib, LibraryOptions> = {
sdk: {
entry: './src/entries/sdk.ts',
fileName: () => `${prefix}OneSignalSDK.page.js`,
},
page: {
entry: './src/entries/pageSdkInit.ts',
fileName: () => `${prefix}OneSignalSDK.page.es6.js`,
},
worker: {
entry: './src/entries/worker.ts',
fileName: () => `${prefix}OneSignalSDK.sw.js`,
},
};
const getBooleanEnv = (env?: string) => {
return env && env === 'true';
};
const isProdEnv = process.env.ENV === 'production';
const isTest = !!process.env.VITEST;
const isBuild = process.env.NODE_ENV === 'production';
const lib = process.env.LIB as Lib;
export default defineConfig({
staged: {
'*': 'vp check --fix',
},
fmt: {
singleQuote: true,
sortImports: {
enabled: true,
},
},
lint: {
rules: {
'typescript/require-await': 'error',
},
options: { typeAware: true, typeCheck: true },
},
resolve: {
tsconfigPaths: true,
alias: isTest
? {
src: path.resolve(import.meta.dirname, 'src'),
__test__: path.resolve(import.meta.dirname, '__test__'),
}
: undefined,
},
plugins: [
mkcert(),
...(isBuild
? [
analyzer({
analyzerMode: 'static',
fileName: `../stats/${lib}-stats`,
}),
]
: []),
],
build: {
/**
* NOTE: Need to specify 2022 or later to not have declarations like
* `var It=Object.defineProperty;` above the IIFE.
*/
target: 'es2022',
minify: isProdEnv ? 'terser' : false,
terserOptions: {
compress: {
passes: 2,
},
mangle: {
properties: {
// Enable property name mangling/minification for private-style properties (starting with _)
regex: /^_/,
keep_quoted: true,
},
},
},
lib: {
...libConfig[lib],
name: 'OneSignal',
formats: ['iife'],
},
outDir: 'build/releases',
emptyOutDir: false,
sourcemap: true,
rollupOptions: {
output: {
assetFileNames: `${prefix}OneSignalSDK.page.styles.css`,
},
// for getting rid of unused imports for builds otherwise we would need dynamic imports
treeshake: { moduleSideEffects: false },
},
},
// Could move some of these to .env.[ENV] file
// NOTE!!!: Since the service worker is registered separately, the define WONT probably replace it for the SW
// But it will be fine for builds. So check if field exists for development e.g. typeof __VERSION__ !== 'undefined'
define: isTest
? {
__API_ORIGIN__: JSON.stringify('onesignal.com'),
__API_TYPE__: JSON.stringify('staging'),
__BUILD_ORIGIN__: JSON.stringify('onesignal.com'),
__BUILD_TYPE__: JSON.stringify('production'),
__IS_HTTPS__: JSON.stringify(true),
__LOGGING__: JSON.stringify(false),
__NO_DEV_PORT__: JSON.stringify(true),
__VERSION__: JSON.stringify('160000'),
}
: {
__API_TYPE__: JSON.stringify(process.env.API),
__BUILD_TYPE__: JSON.stringify(process.env.ENV),
__LOGGING__: JSON.stringify(getBooleanEnv(process.env.LOGGING) ?? !isProdEnv),
__VERSION__: JSON.stringify(sdkVersion),
__IS_SERVICE_WORKER__: JSON.stringify(lib === 'worker'),
__API_ORIGIN__: JSON.stringify(process.env.API_ORIGIN),
__BUILD_ORIGIN__: JSON.stringify(process.env.BUILD_ORIGIN),
__IS_HTTPS__: JSON.stringify(getBooleanEnv(process.env.HTTPS)),
__NO_DEV_PORT__: JSON.stringify(getBooleanEnv(process.env.NO_DEV_PORT)),
},
server: {
open: true,
port: 4000,
https: getBooleanEnv(process.env.HTTPS)
? { cert: './certs/cert.pem', key: './certs/dev.pem' }
: {},
},
test: {
clearMocks: true,
coverage: {
include: ['src/**'],
reporter: ['text-summary', 'lcov'],
reportsDirectory: 'coverage',
reportOnFailure: true,
thresholds: {
statements: 75,
branches: 63,
functions: 81,
lines: 75,
},
},
environment: 'jsdom',
include: ['src/**/*.test.ts', '__test__/**/*.test.ts'],
setupFiles: ['fake-indexeddb/auto', './__test__/setupTests.ts'],
},
});