Skip to content

Commit ee951e5

Browse files
authored
feat: support fetching .d.ts files from PR bundles (#331)
1 parent 9718146 commit ee951e5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/composables/store.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export const useStore = (initial: Initial) => {
8181
})
8282
const hideFile = !IS_DEV && !userOptions.showHidden
8383

84+
if(pr) useWorker(pr)
8485
const [nightly, toggleNightly] = useToggle(false)
8586
const builtinImportMap = computed<ImportMap>(() => {
8687
let importMap = genImportMap(versions, nightly.value)
@@ -290,4 +291,16 @@ export const useStore = (initial: Initial) => {
290291
return store as typeof store & typeof utils
291292
}
292293

294+
function useWorker(pr: string) {
295+
const _worker = window.Worker;
296+
window.Worker = class extends _worker {
297+
constructor(url: URL | string, options?: WorkerOptions) {
298+
if(typeof url === 'string' && url.includes('vue.worker')) {
299+
url = `${url}?pr=${pr}`
300+
}
301+
super(url, options);
302+
}
303+
}
304+
}
305+
293306
export type Store = ReturnType<typeof useStore>

vite.config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ export default defineConfig({
3232
host: true,
3333
},
3434
plugins: [
35+
{
36+
name: 'vue.worker',
37+
transform(code, id) {
38+
if(id.includes('vue.worker')) {
39+
code = `${code}
40+
const pr = new URL(location.href).searchParams.get('pr')
41+
if(pr) {
42+
const _fetch = self.fetch
43+
self.fetch = (...args) => {
44+
const shouldReplace = args[0].startsWith("https://cdn.jsdelivr.net/npm/element-plus/es/")
45+
if(shouldReplace) { args[0] = args[0].replace("https://cdn.jsdelivr.net/npm/element-plus", \`https://preview-\${pr}-element-plus.surge.sh/bundle\`) }
46+
return _fetch(...args)
47+
}
48+
}`
49+
50+
return {
51+
code,
52+
map: null
53+
}
54+
}
55+
56+
}
57+
},
3558
vue({
3659
script: {
3760
defineModel: true,

0 commit comments

Comments
 (0)