@idrinth-api-bench/inline-critical-css
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "@idrinth-api-bench/inline-critical-css", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
import { parse, stringify, } from 'css'; | ||
import { readdirSync, writeFileSync, readFileSync, } from 'fs'; | ||
import { createHash, } from "crypto"; | ||
import minify from "css-minify"; | ||
import { createHash, } from 'crypto'; | ||
import minify from 'css-minify'; | ||
const source = `${process.cwd()}/dist/assets`; | ||
@@ -87,3 +87,13 @@ const critical = []; | ||
const finalHash = hash.digest('hex'); | ||
writeFileSync(`${source}/critical-${finalHash}.css`, await minify(stringify({ type: 'stylesheet', stylesheet: { rules: critical, } }))); | ||
writeFileSync(`${source}/../index.html`, readFileSync(`${source}/../index.html`, 'utf8').replace(/<\/head>/iug, `<link rel="stylesheet" href="/assets/critical-${finalHash}.css"/></head>`), 'utf8'); | ||
const index = readFileSync(`${source}/../index.html`, 'utf8'); | ||
const styles = await minify(stringify({ | ||
type: 'stylesheet', | ||
stylesheet: { rules: critical, } | ||
})); | ||
if (process.argv.indexOf('--inline') !== -1) { | ||
writeFileSync(`${source}/../index.html`, index.replace(/<\/title>/iug, `</title><style>${styles}</style>`), 'utf8'); | ||
} | ||
else { | ||
writeFileSync(`${source}/critical-${finalHash}.css`, styles); | ||
writeFileSync(`${source}/../index.html`, index.replace(/<\/title>/iug, `</title><link rel="stylesheet" href="/assets/critical-${finalHash}.css"/>`), 'utf8'); | ||
} |
@@ -12,4 +12,4 @@ import { | ||
createHash, | ||
} from "crypto"; | ||
import minify from "css-minify" | ||
} from 'crypto'; | ||
import minify from 'css-minify' | ||
@@ -97,7 +97,20 @@ const source = `${ process.cwd() }/dist/assets`; | ||
const finalHash = hash.digest('hex'); | ||
writeFileSync(`${source}/critical-${finalHash}.css`, await minify(stringify({type: 'stylesheet', stylesheet: {rules: critical,}}))); | ||
writeFileSync( | ||
`${source}/../index.html`, | ||
readFileSync(`${source}/../index.html`, 'utf8').replace(/<\/head>/iug, `<link rel="stylesheet" href="/assets/critical-${finalHash}.css"/></head>`), | ||
'utf8', | ||
) | ||
const index = readFileSync(`${source}/../index.html`, 'utf8'); | ||
const styles = await minify(stringify({ | ||
type: 'stylesheet', | ||
stylesheet: {rules: critical,} | ||
})); | ||
if (process.argv.indexOf('--inline') !== -1) { | ||
writeFileSync( | ||
`${source}/../index.html`, | ||
index.replace(/<\/title>/iug, `</title><style>${styles}</style>`), | ||
'utf8', | ||
); | ||
} else { | ||
writeFileSync(`${source}/critical-${finalHash}.css`, styles); | ||
writeFileSync( | ||
`${source}/../index.html`, | ||
index.replace(/<\/title>/iug, `</title><link rel="stylesheet" href="/assets/critical-${finalHash}.css"/>`), | ||
'utf8', | ||
); | ||
} |
7972
211