Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim leading and trailing newlines #91

Merged
merged 3 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
Loading