Skip to content

Commit

Permalink
Trim leading and trailing newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
mataha committed Feb 12, 2024
1 parent 214fd1b commit d4ca753
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ const defaultConfig = { useRecommendedBuildConfig: true, removeViteModuleLoader:
export function replaceScript(html: string, scriptFilename: string, scriptCode: string, removeViteModuleLoader = false): string {
const reScript = new RegExp(`<script([^>]*?) src="[./]*${scriptFilename}"([^>]*)></script>`)
const newCode = scriptCode.replaceAll(`"__VITE_PRELOAD__"`, "void 0")
const inlined = html.replace(reScript, (_, beforeSrc, afterSrc) => `<script${beforeSrc}${afterSrc}>${newCode}</script>`)
const inlined = html.replace(reScript, (_, beforeSrc, afterSrc) => `<script${beforeSrc}${afterSrc}>${newCode.trim()}</script>`)
return removeViteModuleLoader ? _removeViteModuleLoader(inlined) : inlined
}

export function replaceCss(html: string, scriptFilename: string, scriptCode: string): string {
const reStyle = new RegExp(`<link([^>]*?) href="[./]*${scriptFilename}"([^>]*?)>`)
const newCode = scriptCode.replace(`@charset "UTF-8";`, "")
const inlined = html.replace(reStyle, (_, beforeSrc, afterSrc) => `<style${beforeSrc}${afterSrc}>${newCode}</style>`);
const inlined = html.replace(reStyle, (_, beforeSrc, afterSrc) => `<style${beforeSrc}${afterSrc}>${newCode.trim()}</style>`);
return inlined
}

Expand Down

0 comments on commit d4ca753

Please sign in to comment.