Skip to content

Commit

Permalink
fix new URL
Browse files Browse the repository at this point in the history
  • Loading branch information
bddjr committed Nov 27, 2024
1 parent ff92240 commit e0568fd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README-zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ rendering chunks (1)...
vite-plugin-singlefile-compression building...

file:///D:/bddjr/Desktop/code/js/vite-plugin-singlefile-compression/test/dist/index.html
97.31 KiB -> 50.83 KiB
97.52 KiB -> 50.98 KiB

Finish.

dist/index.html 52.05 kB
built in 738ms
dist/index.html 52.19 kB
built in 685ms
```
```html
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ rendering chunks (1)...
vite-plugin-singlefile-compression building...

file:///D:/bddjr/Desktop/code/js/vite-plugin-singlefile-compression/test/dist/index.html
97.31 KiB -> 50.83 KiB
97.52 KiB -> 50.98 KiB

Finish.

dist/index.html 52.05 kB
built in 738ms
dist/index.html 52.19 kB
built in 685ms
```
```html
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin
const thisDel = new Set<string>()

// Fix async import, fix new URL
const newJSCode = ["self.__VITE_PRELOAD__=void 0;import.meta.url=location.origin+location.pathname"]
const newJSCode = ["self.__VITE_PRELOAD__=void 0"]

// get css tag
newHtml = newHtml.replace(/\s*<link rel="stylesheet"[^>]* href="\.\/(assets\/[^"]+)"[^>]*>/,
Expand Down Expand Up @@ -153,6 +153,9 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin
thisDel.add(name)
const js = bundle[name] as OutputChunk
oldSize += js.code.length
// fix new URL
newJSCode.push(`import.meta.url=location.origin+location.pathname.replace(/[^/]*$/,"${name}")`)
// add script
newJSCode.push(js.code.replace(/;?\n?$/, ''))
// gzip
return '<script type="module">'
Expand Down

0 comments on commit e0568fd

Please sign in to comment.