-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.js
94 lines (87 loc) · 2.72 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
// vite.config.js
import { defineConfig } from 'vite';
import { resolve } from 'path';
import fs from 'fs-extra';
import dotenv from 'dotenv';
dotenv.config();
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
const baseVersion = packageJson.version;
export default defineConfig(({ command, mode }) => {
const isProduction = mode === 'production';
const useMockData = !isProduction && process.env.USE_MOCK_DATA === 'true';
const displayVersion = isProduction ? baseVersion : `${baseVersion}-d`;
const uiDisplayVersion = useMockData ? `${displayVersion}-m` : displayVersion;
return {
define: {
__APP_VERSION__: JSON.stringify(displayVersion),
__GA4_MEASUREMENT_ID__: JSON.stringify(process.env.GA4_MEASUREMENT_ID),
__GA4_API_SECRET__: JSON.stringify(process.env.GA4_API_SECRET),
__USE_MOCK_DATA__: useMockData,
},
build: {
outDir: 'dist',
emptyOutDir: true,
rollupOptions: {
input: {
popup: resolve(__dirname, 'src/popup/popup.html'),
background: resolve(__dirname, 'src/background/background.js'),
eventHandler: resolve(__dirname, 'src/background/eventHandler.js'),
content: resolve(__dirname, 'src/content/content.js'),
},
output: {
entryFileNames: '[name].js',
chunkFileNames: 'shared/[name].[hash].js',
assetFileNames: 'assets/[name].[ext]',
},
external: [/\/__tests__\//, /\.test\.js$/, /\.spec\.js$/],
},
},
plugins: [
{
name: 'exclude-mock-data',
resolveId(source) {
if (isProduction && source.includes('mockData.js')) {
return false;
}
},
},
{
name: 'version-injection',
transformIndexHtml(html) {
return html.replace(
/v\d+\.\d+\.\d+(-dev)?(-mock)?/g,
`v${uiDisplayVersion}`
);
},
},
{
name: 'manifest-version',
writeBundle() {
const manifestPath = resolve(__dirname, 'dist/manifest.json');
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
manifest.version = baseVersion; // Always use the base version for manifest
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
},
},
{
name: 'clean-up-utils',
writeBundle(options, bundle) {
const distPath = resolve(__dirname, 'dist');
const sharedPath = resolve(distPath, 'shared');
// Remove unhashed helpers.js if it exists
const unhashedHelpersPath = resolve(distPath, 'helpers.js');
if (fs.existsSync(unhashedHelpersPath)) {
fs.unlinkSync(unhashedHelpersPath);
}
// Remove mockData.js in production
if (isProduction) {
const mockDataPath = resolve(sharedPath, 'mockData.js');
if (fs.existsSync(mockDataPath)) {
fs.unlinkSync(mockDataPath);
}
}
},
},
],
};
});