Skip to content

Commit

Permalink
Merge pull request #91 from mataha/fix/trailing-newline
Browse files Browse the repository at this point in the history
Trim leading and trailing newlines
  • Loading branch information
richardtallent authored Nov 3, 2024
2 parents 29dc598 + d101400 commit 4febbc0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 20 deletions.
11 changes: 0 additions & 11 deletions fixup

This file was deleted.

22 changes: 22 additions & 0 deletions fixup.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node

const fs = require("node:fs")
const path = require("node:path")

function main(/* {string} */ project) {
const types = {
cjs: "commonjs",
esm: "module",
}

for (const [distribution, type] of Object.entries(types)) {
fs.writeFileSync(
path.join(project, "dist", distribution, "package.json"),
JSON.stringify({ type: type }, null, "\t")
)
}
}

if (require.main === module) {
main(path.dirname(require.main.filename))
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"node": ">18.0.0"
},
"scripts": {
"dev": "rimraf dist && tsc -w --p tsconfig.json",
"dev": "rimraf dist && tsc -w -p tsconfig.json",
"prepare": "npm run build",
"build": "rimraf dist && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && ./fixup",
"build": "rimraf dist && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && node fixup.cjs",
"test": "vitest",
"test:watch": "vitest watch",
"lint": "eslint src/index.ts"
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export function replaceScript(html: string, scriptFilename: string, scriptCode:
const reScript = new RegExp(`<script([^>]*?) src="[./]*${scriptFilename}"([^>]*)></script>`)
const preloadMarker = /"?__VITE_PRELOAD__"?/g
const newCode = scriptCode.replace(preloadMarker, "void 0").replace(/(<)(\/script>|!--)/g, '\\x3C$2')
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 legacyCharSetDeclaration = /@charset "UTF-8";/
const inlined = html.replace(reStyle, (_, beforeSrc, afterSrc) => `<style${beforeSrc}${afterSrc}>${scriptCode.replace(legacyCharSetDeclaration, "")}</style>`);
const newCode = scriptCode.replace(`@charset "UTF-8";`, "")
const inlined = html.replace(reStyle, (_, beforeSrc, afterSrc) => `<style${beforeSrc}${afterSrc}>${newCode.trim()}</style>`);
return inlined
}

Expand Down
4 changes: 1 addition & 3 deletions tsconfig-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
"declarationMap": false,
"esModuleInterop": true,
"inlineSourceMap": false,
// es2020 is what Node.JS v14 supports
// see https://github.com/tsconfig/bases/blob/main/bases/node14.json
"lib": ["es2020", "dom"],
"lib": ["es2021", "dom"],
"listEmittedFiles": false,
"moduleResolution": "node",
"noUnusedLocals": true,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig-cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"declarationDir": "dist/cjs/declarations",
"module": "commonjs",
"outDir": "dist/cjs",
"target": "es2015"
"target": "es2021"
}
}

0 comments on commit 4febbc0

Please sign in to comment.