Skip to content

Commit 727fd70

Browse files
authored
fix: apply patches to vue.worker file in build (#333)
1 parent ee951e5 commit 727fd70

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

vite.config.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,22 @@ export default defineConfig({
3535
{
3636
name: 'vue.worker',
3737
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-
38+
if (id.includes('vue.worker')) {
5039
return {
51-
code,
52-
map: null
40+
code: patchVueWorker(code),
41+
map: null,
5342
}
5443
}
55-
56-
}
44+
},
45+
generateBundle(_, bundle) {
46+
for (const [fileName, file] of Object.entries(bundle)) {
47+
if (fileName.includes('vue.worker')) {
48+
// @ts-ignore
49+
file.source = patchVueWorker(file.source.toString())
50+
break
51+
}
52+
}
53+
},
5754
},
5855
vue({
5956
script: {
@@ -84,3 +81,16 @@ export default defineConfig({
8481
exclude: ['@vue/repl'],
8582
},
8683
})
84+
85+
function patchVueWorker(code: string) {
86+
return `${code}
87+
const pr = new URL(location.href).searchParams.get('pr')
88+
if(pr) {
89+
const _fetch = self.fetch
90+
self.fetch = (...args) => {
91+
const shouldReplace = args[0].startsWith("https://cdn.jsdelivr.net/npm/element-plus/es/")
92+
if(shouldReplace) { args[0] = args[0].replace("https://cdn.jsdelivr.net/npm/element-plus", \`https://preview-\${pr}-element-plus.surge.sh/bundle\`) }
93+
return _fetch(...args)
94+
}
95+
}`
96+
}

0 commit comments

Comments
 (0)