-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove missing sourcemap vue-router warnings (#172)
- Loading branch information
Showing
4 changed files
with
89 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { join } from 'node:path' | ||
import fs from 'node:fs' | ||
import { eventHandler } from 'h3' | ||
|
||
export function dev( | ||
swMap: string, | ||
resolvedSwMapFile: string, | ||
worboxMap: string, | ||
buildDir: string, | ||
baseURL: string, | ||
) { | ||
return eventHandler(async (event) => { | ||
const url = event.path | ||
if (!url) | ||
return | ||
|
||
const file = url === swMap | ||
? resolvedSwMapFile | ||
: url.startsWith(worboxMap) && url.endsWith('.js.map') | ||
? join( | ||
buildDir, | ||
'dev-sw-dist', | ||
url.slice(baseURL.length), | ||
) | ||
: undefined | ||
|
||
if (file) { | ||
try { | ||
await waitFor(() => fs.existsSync(file)) | ||
const map = fs.readFileSync(file, 'utf-8') | ||
event.headers.set('Content-Type', 'application/json') | ||
event.headers.set('Cache-Control', 'public, max-age=0, must-revalidate') | ||
event.headers.set('Content-Length', `${map.length}`) | ||
event.node.res.end(map) | ||
} | ||
catch { | ||
} | ||
} | ||
}) | ||
} | ||
|
||
async function waitFor(method: () => boolean, retries = 5): Promise<void> { | ||
if (method()) | ||
return | ||
|
||
if (retries === 0) | ||
throw new Error('Timeout in waitFor') | ||
|
||
await new Promise(resolve => setTimeout(resolve, 300)) | ||
|
||
return waitFor(method, retries - 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters