forked from 0xMiden/wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.contentScripts.config.ts
More file actions
110 lines (102 loc) · 4.3 KB
/
Copy pathvite.contentScripts.config.ts
File metadata and controls
110 lines (102 loc) · 4.3 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
/**
* Vite config for Chrome-extension content scripts.
*
* Chrome MV3 content_scripts (and scripts injected into the page via
* `document.createElement('script')` without `type="module"`) run as
* CLASSIC scripts. ES-module output with `import` statements fails to
* parse silently, leaving window.midenWallet never set.
*
* This config builds a single entry at a time as an IIFE with all
* dynamic imports inlined. Invoke once per entry via CS_ENTRY:
* CS_ENTRY=contentScript vite build --config vite.contentScripts.config.ts
* CS_ENTRY=addToWindow vite build --config vite.contentScripts.config.ts
*/
import { resolve } from 'path';
import { defineConfig, type Plugin } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
const pkg = require('./package.json');
const TARGET_BROWSER = process.env.TARGET_BROWSER ?? 'chrome';
const ENTRY = process.env.CS_ENTRY;
if (ENTRY !== 'contentScript' && ENTRY !== 'addToWindow') {
throw new Error(`CS_ENTRY must be 'contentScript' or 'addToWindow', got: ${ENTRY ?? '(unset)'}`);
}
// Content scripts run in extension pages only — never in mobile or desktop
// hosts — so the intercom mobile/desktop adapters are statically dead.
// `inlineDynamicImports: true` would still drag their transitive deps (the
// full backend `lib/miden/back/*`, which pulls the wasm-bindgen SDK) into
// the bundle, which breaks the IIFE build (TLA in eager.js) and would
// bloat the bundle by ~15 MB. Stub the adapters to empty modules for this
// build. Match both relative specifiers (`./mobile-adapter`) and resolved
// absolute paths.
function stubIntercomPlatformAdapters(): Plugin {
const isTarget = (id: string) => /(?:^|[\\/])(?:mobile|desktop)-adapter(?:\.[tj]sx?)?$/.test(id);
const stubSource = 'export default {};';
return {
name: 'stub-intercom-platform-adapters',
enforce: 'pre',
resolveId(source) {
if (isTarget(source)) return '\0cs-stub:' + source;
},
load(id) {
if (id.startsWith('\0cs-stub:')) return stubSource;
}
};
}
export default defineConfig({
// Public assets (especially the vendor-prefixed manifest) are handled by
// vite.extension.config.ts. This partial build must not copy them verbatim.
publicDir: false,
plugins: [
stubIntercomPlatformAdapters(),
nodePolyfills({
include: ['buffer', 'util', 'stream', 'assert', 'process'],
globals: { Buffer: true, process: true }
})
],
build: {
outDir: `dist/${TARGET_BROWSER}_unpacked`,
emptyOutDir: false,
sourcemap: process.env.MODE_ENV !== 'production',
target: 'es2022',
minify: process.env.MODE_ENV === 'production',
modulePreload: { polyfill: false },
rollupOptions: {
input: resolve(__dirname, `src/${ENTRY}.ts`),
output: {
entryFileNames: `${ENTRY}.js`,
format: 'iife',
inlineDynamicImports: true
}
}
},
resolve: {
alias: {
lib: resolve(__dirname, 'src/lib'),
app: resolve(__dirname, 'src/app'),
shared: resolve(__dirname, 'src/shared'),
components: resolve(__dirname, 'src/components'),
screens: resolve(__dirname, 'src/screens'),
utils: resolve(__dirname, 'src/utils'),
buffer: 'buffer',
stream: 'stream-browserify',
assert: 'assert'
}
},
define: {
'process.env.VERSION': JSON.stringify(pkg.version),
'process.env.TARGET_BROWSER': JSON.stringify(TARGET_BROWSER),
'process.env.MIDEN_USE_MOCK_CLIENT': JSON.stringify(process.env.MIDEN_USE_MOCK_CLIENT ?? 'false'),
'process.env.MIDEN_NETWORK': JSON.stringify(process.env.MIDEN_NETWORK ?? ''),
'process.env.MIDEN_NOTE_TRANSPORT_URL': JSON.stringify(process.env.MIDEN_NOTE_TRANSPORT_URL ?? ''),
'process.env.MIDEN_E2E_TEST': JSON.stringify(process.env.MIDEN_E2E_TEST ?? 'false'),
'process.env.MIDEN_ENABLE_BRIDGE_UI': JSON.stringify(process.env.MIDEN_ENABLE_BRIDGE_UI ?? 'false'),
'process.env.EPOCH_POSITIONS_URL': JSON.stringify(
process.env.EPOCH_POSITIONS_URL ?? 'https://positions-testnet-dev.epochprotocol.xyz'
),
'process.env.E2E_EVM_RPC_URL': JSON.stringify(process.env.E2E_EVM_RPC_URL ?? ''),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV ?? 'development'),
'process.env.MODE_ENV': JSON.stringify(process.env.MODE_ENV ?? 'development'),
'process.browser': 'true',
global: 'globalThis'
}
});