-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
232 lines (201 loc) · 6.98 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// @ts-check
import { defineConfig } from 'vite'
import { resolve } from 'import-meta-resolve'
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { join, parse, sep } from 'node:path'
// #region Package Helpers
/**
* @param {string} pkg - Package name
* @param {string} [linkRoot] - npm link root
* @returns {Promise<import('type-fest').PackageJson>}
*/
const packageJson = async (pkg, linkRoot) => {
const direct = async () => {
const path = await resolve(`${pkg}/package.json`, import.meta.url)
const json = await readFile(fileURLToPath(path), 'utf8')
return json
}
const indirect = async () => {
const root = await resolve(pkg, import.meta.url)
const { dir } = parse(fileURLToPath(root))
const dirs = dir.split(sep)
const offset = pkg.startsWith('@') ? 3 : 2
const idx = dirs.length - dirs.indexOf('node_modules') - offset
let path = dir
for (let i = 0; i < idx; i++) {
path = join(path, '..')
}
const jsonPath = join(path, 'package.json')
const json = await readFile(jsonPath, 'utf8')
return json
}
const linked = async () => {
if (!linkRoot) throw new Error('cannot read linked')
const path = join(
fileURLToPath(import.meta.url),
'..',
'node_modules',
linkRoot,
'node_modules',
pkg,
)
const jsonPath = join(path, 'package.json')
const json = await readFile(jsonPath, 'utf8')
return json
}
// @ts-ignore
const json = await Promise.any([direct(), indirect(), linked()])
return JSON.parse(json)
}
/**
* @param {string} pkg - Package name
* @param {string} mode - Build mode
* @param {string} [linkRoot] - npm link root
*/
const esmLink = async (pkg, mode, linkRoot, { pin = 'v135', exports = '' } = {}) => {
const { version } = await packageJson(pkg, linkRoot)
if (!version) throw new Error(`unknown version for package: ${pkg}`)
if (mode === 'discord') {
return `/esm/${pkg}@${version}${exports}${pin ? `?pin=${pin}` : ''}`
}
return `https://esm.sh/${pkg}@${version}${exports}?pin=${pin}`
}
// #endregion
/**
* @param {string} mode
* @param {string|undefined} localCoreUrl
* @returns {import('vite').Plugin}
*/
const importMapPlugin = (mode, localCoreUrl) => ({
name: 'import-map',
transformIndexHtml: async () => {
const core = '@dreamlab.gg/core'
const ui = '@dreamlab.gg/ui'
const react = 'react'
const reactDom = 'react-dom'
const matter = 'matter-js'
const pixi = 'pixi.js'
const [
corePkg,
reactPkg,
jsxPkg,
jsxDevPkg,
reactDomPkg,
reactDomClientPkg,
matterPkg,
pixiPkg,
] = await Promise.all([
esmLink(core, mode, undefined, { exports: '/dist/bundled' }),
esmLink(react, mode),
esmLink(react, mode, undefined, { exports: '/jsx-runtime' }),
esmLink(react, mode, undefined, { exports: '/jsx-dev-runtime' }),
esmLink(reactDom, mode),
esmLink(reactDom, mode, undefined, { exports: '/client' }),
esmLink(matter, mode, core),
esmLink(pixi, mode, core),
])
const map = {
imports: {
[react]: reactPkg,
[`${react}/jsx-runtime`]: jsxPkg,
[`${react}/jsx-dev-runtime`]: jsxDevPkg,
[reactDom]: reactDomPkg,
[`${reactDom}/client`]: reactDomClientPkg,
[`https://external/${react}`]: reactPkg,
[`https://external/${react}/jsx-runtime`]: jsxPkg,
[`https://external/${react}/jsx-dev-runtime`]: jsxDevPkg,
[`https://external/reactDom`]: reactDomPkg,
[`https://external/${reactDom}/client`]: reactDomClientPkg,
[matter]: matterPkg,
[pixi]: pixiPkg,
[core]: corePkg,
},
}
//#region Core Modules
const coreModulesPath = await resolve(`${core}/modules.json`, import.meta.url)
const coreModulesJson = await readFile(fileURLToPath(coreModulesPath), 'utf8')
const coreModules = JSON.parse(coreModulesJson)
for (const module of coreModules) {
map.imports[`${core}/${module}`] = localCoreUrl ? localCoreUrl : corePkg
map.imports[`${core}/dist/${module}`] = localCoreUrl ? localCoreUrl : corePkg
}
//#endregion
//#region UI Modules
const uiModulesPath = await resolve(`${ui}/modules.json`, import.meta.url)
const uiModulesJson = await readFile(fileURLToPath(uiModulesPath), 'utf8')
const uiModules = JSON.parse(uiModulesJson)
const { version: uiVersion } = await packageJson(ui)
for (const module of uiModules) {
const getUrl = () => {
if (mode === 'discord') {
return `/jsdelivr/npm/@dreamlab.gg/ui@${uiVersion}/dist/${module}.js`
}
return `https://cdn.jsdelivr.net/npm/@dreamlab.gg/ui@${uiVersion}/dist/${module}.js`
}
const url = getUrl()
map.imports[`${ui}/${module}`] = url
map.imports[`${ui}/dist/${module}`] = url
}
//#endregion
return [
{
tag: 'script',
attrs: { type: 'importmap' },
children: JSON.stringify(map),
},
]
},
})
export default defineConfig(async ({ mode }) => {
const port = 5173
const corePath = resolve('@dreamlab.gg/core', import.meta.url)
const getLocalCoreUrl = () => {
// only put the dreamlab-core URL in the sourcemap if it's linked and we're in dev mode
if (mode !== 'development') return undefined
if (corePath.includes('node_modules')) return undefined
// TODO: unhack this
const bundled = corePath.split('file://')[1].split('index.js').slice(0, -1)[0] + 'bundled.js'
return `http://localhost:${port}/@fs${bundled}`
}
// const localCoreUrl = getLocalCoreUrl()
// temporarily disable this because it causes the pixi error https://worldql.slack.com/archives/C03MFTRB9BJ/p1711580004918849
const localCoreUrl = undefined
return {
resolve: {
alias: [
{ find: /^react$/, replacement: 'https://external/react' },
{ find: /^react\/(.+)$/, replacement: 'https://external/react/$1' },
{ find: /^react-dom$/, replacement: 'https://external/react-dom' },
{ find: /^react-dom\/(.+)$/, replacement: 'https://external/react-dom/$1' },
...(mode === 'discord' ? [{ find: 'https://esm.sh', replacement: '/esm' }] : []),
],
},
plugins: [importMapPlugin(mode, localCoreUrl)],
preview: {
port: port,
},
server: {
port: port,
strictPort: true,
fs: {
strict: false,
},
},
build: {
rollupOptions: {
external: (source, importer, isResolved) => {
if (source.startsWith('/esm/')) return true
if (source.startsWith('/jsdelivr/')) return true
if (source.startsWith('react')) return true
if (source.startsWith('react-dom')) return true
if (source.includes('matter-js')) return true
if (source.includes('pixi.js')) return true
if (source.includes('@pixi/')) return true
if (source.startsWith('@dreamlab.gg/core')) return true
if (source.startsWith('@dreamlab.gg/ui')) return true
},
},
},
}
})