Skip to content

Commit ecb9267

Browse files
authored
feat: support gtag cdn js in extension (#591)
1 parent c7bde43 commit ecb9267

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

src/modules/build-extension.ts

+41-35
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,41 @@ export function htmlScriptToVirtual(
6868
{
6969
name: `md:dev-html-prerender`,
7070
apply: `build`,
71-
async transform(code, id) {
72-
if (
73-
server == null
74-
|| !id.endsWith(`.html`)
75-
) {
76-
return
77-
}
78-
const { document } = parseHTML(code)
79-
// Replace inline script with virtual module served via dev server.
80-
// Extension CSP blocks inline scripts, so that's why we're pulling them out.
81-
const promises: Promise<void>[] = []
82-
const inlineScripts = document.querySelectorAll(`script[src^=http]`)
83-
inlineScripts.forEach(async (script) => {
84-
promises.push(new Promise<void>((resolve) => {
85-
const url = script.getAttribute(`src`) ?? ``
86-
doFetch(url).then((textContent) => {
87-
const hash = murmurHash(textContent)
88-
inlineScriptContents[hash] = textContent
89-
script.setAttribute(`src`, `${server.origin}/@id/${virtualInlineScript}?${hash}`)
90-
if (script.hasAttribute(`id`)) {
91-
script.setAttribute(`type`, `module`)
71+
transformIndexHtml: {
72+
order: `post`,
73+
async handler(html) {
74+
if (server == null) {
75+
return html
76+
}
77+
const { document } = parseHTML(html)
78+
// Replace inline script with virtual module served via dev server.
79+
// Extension CSP blocks inline scripts, so that's why we're pulling them out.
80+
const promises: Promise<void>[] = []
81+
const inlineScripts = document.querySelectorAll(`script[src^=http]`)
82+
inlineScripts.forEach(async (script) => {
83+
promises.push(new Promise<void>((resolve) => {
84+
const url = script.getAttribute(`src`) ?? ``
85+
if (url?.startsWith(`http://localhost`)) {
86+
resolve()
87+
return
9288
}
93-
resolve()
94-
})
95-
}))
96-
})
97-
await Promise.all(promises)
98-
const newHtml = document.toString()
99-
config.logger.debug(`transform ${id}`)
100-
config.logger.debug(`Old HTML:\n${code}`)
101-
config.logger.debug(`New HTML:\n${newHtml}`)
102-
return newHtml
89+
doFetch(url).then((textContent) => {
90+
const hash = murmurHash(textContent)
91+
inlineScriptContents[hash] = textContent
92+
script.setAttribute(`src`, `${server.origin}/@id/${virtualInlineScript}?${hash}`)
93+
if (script.hasAttribute(`id`)) {
94+
script.setAttribute(`type`, `module`)
95+
}
96+
resolve()
97+
})
98+
}))
99+
})
100+
await Promise.all(promises)
101+
const newHtml = document.toString()
102+
config.logger.debug(`\nhtmlScriptToVirtual Old HTML:\n${html}`)
103+
config.logger.debug(`\nhtmlScriptToVirtual New HTML:\n${newHtml}`)
104+
return newHtml
105+
},
103106
},
104107
},
105108
{
@@ -140,7 +143,7 @@ export function htmlScriptToLocal(
140143
name: `md:build-html-prerender`,
141144
apply: `build`,
142145
transformIndexHtml: {
143-
order: `pre`,
146+
order: `post`,
144147
async handler(html) {
145148
const { document } = parseHTML(html)
146149
const promises: Promise<void>[] = []
@@ -156,7 +159,10 @@ export function htmlScriptToLocal(
156159
}
157160
const textContent = await doFetch(url)
158161
const hash = murmurHash(textContent)
159-
const jsName = url.match(/\/([^/]+)\.js$/)?.[1] ?? `.js`
162+
let jsName = url.match(/\/([^/]+)\.js$/)?.[1] ?? `.js`
163+
if (url.indexOf(`?`) > 0) {
164+
jsName = `${url.substring(url.indexOf(`?`) + 1)}.js`
165+
}
160166
const fileName = `${jsName.split(`.`)[0]}-${hash}.js`
161167
// write to file
162168
const outFile = path.resolve(wxt.config.outDir, `./${fileName}`)
@@ -194,8 +200,8 @@ export function htmlScriptToLocal(
194200
}
195201
await Promise.all(promises)
196202
const newHtml = document.toString()
197-
wxt.config.logger.debug(`Old HTML:\n${html}`)
198-
wxt.config.logger.debug(`New HTML:\n${newHtml}`)
203+
wxt.config.logger.debug(`\nhtmlScriptToLocal Old HTML:\n${html}`)
204+
wxt.config.logger.debug(`\nhtmlScriptToLocal New HTML:\n${newHtml}`)
199205
return newHtml
200206
},
201207
},

0 commit comments

Comments
 (0)