-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathvite.config.ts
138 lines (130 loc) · 4.5 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
import { defineConfig, LibraryFormats, PluginOption } from 'vite';
import webExtension, { readJsonFile } from 'vite-plugin-web-extension';
import zip from 'vite-plugin-zip-pack';
import * as path from 'path';
import type { PackageJson } from 'type-fest';
import react from '@vitejs/plugin-react';
import semver from 'semver';
const emptyOutDir = !process.argv.includes('--watch');
function useSpecialFormat(
entriesToUse: string[],
format: LibraryFormats,
): PluginOption {
return {
name: 'use-special-format',
config(config) {
// entry can be string | string[] | {[entryAlias: string]: string}
const entry = config.build?.lib && config.build.lib.entry;
let shouldUse = false;
if (typeof entry === 'string') {
shouldUse = entriesToUse.includes(entry);
} else if (Array.isArray(entry)) {
shouldUse = entriesToUse.some((e) => entry.includes(e));
} else if (entry && typeof entry === 'object') {
const entryKeys = Object.keys(entry);
shouldUse = entriesToUse.some((e) => entryKeys.includes(e));
}
if (shouldUse) {
config.build = config.build ?? {};
// @ts-expect-error: lib needs to be an object, forcing it.
config.build.lib =
typeof config.build.lib == 'object' ? config.build.lib : {};
// @ts-expect-error: lib is an object
config.build.lib.formats = [format];
}
},
};
}
/**
* Get the extension version based on the rrweb version.
*/
function getExtensionVersion(rrwebVersion: string): string {
const parsedVersion = semver.parse(rrwebVersion.replace('^', ''));
if (!parsedVersion) {
throw new Error('Invalid version format');
}
if (parsedVersion.prerelease.length > 0) {
// If it's a pre-release version like alpha or beta, strip the pre-release identifier
return `${parsedVersion.major}.${parsedVersion.minor}.${
parsedVersion.patch
}.${parsedVersion.prerelease[1] || 0}`;
} else if (rrwebVersion === '2.0.0') {
// This version has already been released as the first version. We need to add a patch version to it to avoid publishing conflicts.
return '2.0.0.100';
} else {
return rrwebVersion;
}
}
export default defineConfig({
root: 'src',
// Configure our outputs - nothing special, this is normal vite config
build: {
outDir: path.resolve(
__dirname,
'dist',
process.env.TARGET_BROWSER as string,
),
emptyOutDir,
},
// Add the webExtension plugin
plugins: [
react(),
webExtension({
// A function to generate manifest file dynamically.
manifest: () => {
const packageJson = readJsonFile('package.json') as PackageJson;
type ManifestBase = {
common: Record<string, unknown>;
chrome: Record<string, unknown>;
firefox: Record<string, unknown>;
};
const originalManifest = readJsonFile('./src/manifest.json') as {
common: Record<string, unknown>;
v2: ManifestBase;
v3: ManifestBase;
};
const ManifestVersion =
process.env.TARGET_BROWSER === 'chrome' ? 'v3' : 'v2';
const BrowserName =
process.env.TARGET_BROWSER === 'chrome' ? 'chrome' : 'firefox';
const commonManifest = originalManifest.common;
const rrwebVersion = packageJson.dependencies!.rrweb!.replace('^', '');
const manifest = {
version: getExtensionVersion(rrwebVersion),
author: packageJson.author,
version_name: rrwebVersion,
...commonManifest,
};
Object.assign(
manifest,
originalManifest[ManifestVersion].common,
originalManifest[ManifestVersion][BrowserName],
);
return manifest;
},
browser: process.env.TARGET_BROWSER,
webExtConfig: {
startUrl: ['github.com/rrweb-io/rrweb'],
watchIgnored: ['*.md', '*.log'],
},
additionalInputs: ['pages/index.html', 'content/inject.ts'],
}) as PluginOption,
// /~https://github.com/aklinker1/vite-plugin-web-extension/issues/50#issuecomment-1317922947
// transfer inject.ts to iife format to avoid error
useSpecialFormat(
[path.resolve(__dirname, 'src/content/inject.ts')],
'iife',
),
process.env.ZIP === 'true' &&
zip({
inDir: `dist/${process.env.TARGET_BROWSER || 'chrome'}`,
outDir: 'dist',
outFileName: `${process.env.TARGET_BROWSER || 'chrome'}.zip`,
}),
],
resolve: {
alias: {
'~': path.resolve(__dirname, './src'),
},
},
});