Skip to content

Commit

Permalink
1.3.2 optimize script import.meta.url
Browse files Browse the repository at this point in the history
  • Loading branch information
bddjr committed Jan 13, 2025
1 parent 4fd58ca commit aa3395c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ vite v6.0.7 building for production...
45 modules transformed.
rendering chunks (1)...

vite-plugin-singlefile-compression 1.3.1 building...
vite-plugin-singlefile-compression 1.3.2 building...

file:///D:/bddjr/Desktop/code/js/vite-plugin-singlefile-compression/test/dist/index.html
101.56 KiB -> 46.76 KiB
file:///d/bddjr/Desktop/code/js/vite-plugin-singlefile-compression/test/dist/index.html
101.56 KiB -> 46.74 KiB

Finish.

dist/index.html 47.88 kB
built in 677ms
dist/index.html 47.86 kB
built in 634ms
```
![](effect.jpg)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-singlefile-compression",
"version": "1.3.1",
"version": "1.3.2",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down
8 changes: 4 additions & 4 deletions src/getTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'

const files = {
base64: fs.readFileSync(
path.join(import.meta.dirname, "template/base64.js")
fileURLToPath(import.meta.resolve("./template/base64.js"))
).toString(),

base128: fs.readFileSync(
path.join(import.meta.dirname, "template/base128.js")
fileURLToPath(import.meta.resolve("./template/base128.js"))
).toString(),

assets: fs.readFileSync(
path.join(import.meta.dirname, "template/assets.js")
fileURLToPath(import.meta.resolve("./template/assets.js"))
).toString().split('{"":""}', 2),
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ async function generateBundle(bundle: OutputBundle, config: ResolvedConfig, opti
const js = bundle[name] as OutputChunk
oldSize += js.code.length
// fix new URL
newJSCode.push(`import.meta.url=location.origin+location.pathname.replace(/[^/]*$/,${JSON.stringify(name)})`)
newJSCode.push(`import.meta.url=new URL(${JSON.stringify(name)},location).href`)
// do not delete not inlined asset
for (const name of bundleAssetsNames) {
const assetName = name.slice('assets/'.length)
Expand Down

0 comments on commit aa3395c

Please sign in to comment.