Skip to content

Commit

Permalink
try fix new URL
Browse files Browse the repository at this point in the history
  • Loading branch information
bddjr committed Nov 27, 2024
1 parent 2337bb6 commit ff92240
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { minify as htmlMinify, Options as htmlMinifierOptions } from 'html-minif
import zlib from 'zlib'
import path from 'path'
import fs from 'fs'
import { pathToFileURL } from "url"

export interface Options {
/**
Expand Down Expand Up @@ -46,11 +47,7 @@ const template = fs.readFileSync(path.join(import.meta.dirname, "template.js")).

const templateAssets = fs.readFileSync(path.join(import.meta.dirname, "template-assets.js")).toString()

const fileProtocolDistPath = (p =>
p.startsWith('/')
? `file://${p}/`
: `file:///${p.replaceAll('\\', '/')}/`
)(path.resolve("dist"))
const distURL = pathToFileURL(path.resolve("dist")) + "/"

function gzipToBase64(buf: zlib.InputType) {
return zlib.gzipSync(buf, {
Expand Down Expand Up @@ -99,8 +96,8 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin
let oldSize = newHtml.length
const thisDel = new Set<string>()

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

// get css tag
newHtml = newHtml.replace(/\s*<link rel="stylesheet"[^>]* href="\.\/(assets\/[^"]+)"[^>]*>/,
Expand Down Expand Up @@ -172,7 +169,7 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin
htmlChunk.source = newHtml
console.log(
"\n"
+ " " + pc.underline(pc.cyan(fileProtocolDistPath) + pc.greenBright(htmlFileName)) + '\n'
+ " " + pc.underline(pc.cyan(distURL) + pc.greenBright(htmlFileName)) + '\n'
+ " " + pc.gray(KiB(oldSize) + " -> ") + pc.cyanBright(KiB(newHtml.length)) + '\n'
)

Expand Down

0 comments on commit ff92240

Please sign in to comment.