Skip to content

Instantly share code, notes, and snippets.

@Igloczek
Created February 19, 2022 11:36
Show Gist options
  • Save Igloczek/2e8f5596490bc56eda1486e5ed8f4a07 to your computer and use it in GitHub Desktop.
Save Igloczek/2e8f5596490bc56eda1486e5ed8f4a07 to your computer and use it in GitHub Desktop.
Astro JS - Inline CSS
import path from 'node:path'
import fs from 'node:fs/promises'
import { globby } from 'globby'
const files = await globby('./dist/**/index.html')
await Promise.all(
files.map(async htmlPath => {
const pageStyles = []
const stylesPaths = []
let file = await fs.readFile(htmlPath, 'utf-8')
file = file.replace(
/<link rel="stylesheet" href="(.*?)">/g,
(match, p1) => {
stylesPaths.push(p1)
return ''
}
)
await Promise.all(
stylesPaths.map(async stylesPath => {
const styles = await fs.readFile(path.resolve(path.dirname(htmlPath), stylesPath), 'utf-8')
pageStyles.push(styles)
})
)
file = file.replace(
'<meta charset="UTF-8">',
`<style>${pageStyles.join(' ')}</style>\n<meta charset="UTF-8">`
)
await fs.writeFile(htmlPath, file)
})
)
@farolanf
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment