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 2 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-lock.json

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

8 changes: 4 additions & 4 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 Expand Up @@ -58,9 +58,9 @@
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"eslint": "^8.56.0",
"vitest": "^1.2.2",
"rimraf": "^5.0.5",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"vitest": "^1.2.2"
},
"prettier": {
"useTabs": true,
Expand Down
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ 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>`)
// we can't use String.prototype.replaceAll since it isn't supported in Node.JS 14
const preloadMarker = /"__VITE_PRELOAD__"/g
const newCode = scriptCode.replace(preloadMarker, "void 0")
const inlined = html.replace(reScript, (_, beforeSrc, afterSrc) => `<script${beforeSrc}${afterSrc}>${newCode}</script>`)
const newCode = scriptCode.replaceAll(`"__VITE_PRELOAD__"`, "void 0")
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"
}
}