This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
vite.config.js
135 lines (123 loc) · 4.16 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
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
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
import childProcess from 'node:child_process';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const prod = process.env.NODE_ENV === 'production';
const CONFIG = {
staticUrlPrefix: process.env.PRECHOSTER_STATIC || 'https://cohost.org/static/',
gitCommitHash: process.env.PRECHOSTER_GIT_COMMIT_HASH?.substring(0, 7) || childProcess
.execSync('git rev-parse --short HEAD', {
cwd: __dirname,
encoding: 'utf-8',
})
.trim(),
};
console.error(`\x1b[32mUsing PRECHOSTER_STATIC=${CONFIG.staticUrlPrefix}\x1b[m`);
if (CONFIG.staticUrlPrefix.includes('//cohost.org')) {
console.error('\x1b[31m+------------------------------------------------+\x1b[m');
console.error('\x1b[31m| loading assets directly from cohost dot org!!! |\x1b[m');
console.error('\x1b[31m| links may be unreliable... |\x1b[m');
console.error('\x1b[31m+------------------------------------------------+\x1b[m');
}
export default defineConfig({
base: './',
plugins: [react(), string(), stringNodeModules(), config(), hackToFixSvelteWebWorker()],
build: {
rollupOptions: {
// i dont know why but some of these need to be repeated here for some reason
plugins: [stringNodeModules(), hackToFixSvelteWebWorker()],
},
sourcemap: true,
},
});
function readFileAsModule(id) {
return new Promise((resolve, reject) => {
fs.readFile(id, 'utf-8', (err, file) => {
if (err) reject(err);
else {
resolve(`export default ${JSON.stringify(file)};`);
}
});
});
}
/** The `string:` loader can be used to load files as strings */
function string() {
const scheme = 'string:';
return {
name: 'string',
resolveId(id, importer) {
if (id.startsWith(scheme)) {
const importerDir = path.dirname(importer) + '/';
const resolved = path.resolve(importerDir, id.substring(scheme.length));
return '\0' + scheme + resolved;
}
return null;
},
load(id) {
if (id.startsWith('\0' + scheme)) {
return readFileAsModule(id.substring(scheme.length + 1));
}
return null;
},
};
}
function stringNodeModules() {
// don't let vite catch on that we're importing something from node modules
const scheme = 'string-node-modules:';
return {
name: 'string-node-modules',
resolveId(id, importer) {
if (id.startsWith(scheme)) {
return '\0' + id;
}
return null;
},
load(id) {
if (id.startsWith('\0' + scheme)) {
return readFileAsModule(
__dirname + '/node_modules/' + id.substring(scheme.length + 1)
);
}
return null;
},
};
}
/** load build config from js */
function config() {
const scheme = 'prechoster:';
return {
name: 'config',
resolveId(id, importer) {
if (id.startsWith(scheme)) {
return '\0' + id;
}
return null;
},
load(id) {
if (id.startsWith('\0' + scheme)) {
const k = id.substring(scheme.length + 1);
if (k === 'config') {
return Object.keys(CONFIG)
.map((k) => `export const ${k} = ${JSON.stringify(CONFIG[k])};`)
.join('\n');
}
}
return null;
},
};
}
/** Fix svelte's incorrect use of `window`, which does not exist in web workers */
function hackToFixSvelteWebWorker() {
return {
transform(code, id) {
if (id.includes('svelte/') || id.match(/node_modules.+svelte/)) {
return { code: code.replace(/\bwindow\b/g, 'globalThis'), map: null };
}
return null;
},
enforce: 'post',
};
}