inline-critical
Advanced tools
Comparing version 9.0.1 to 10.0.0
37
cli.js
#!/usr/bin/env node | ||
import {EOL} from 'node:os'; | ||
import {readFileSync} from 'node:fs'; | ||
import process from 'node:process'; | ||
import meow from 'meow'; | ||
import picocolors from 'picocolors'; | ||
import indentString from 'indent-string'; | ||
import stdin from 'get-stdin'; | ||
import {parse} from 'css'; | ||
import escapeRegExp from 'lodash.escaperegexp'; | ||
import defaults from 'lodash.defaults'; | ||
import {inline as inlineCritical} from './index.js'; | ||
'use strict'; | ||
const os = require('os'); | ||
const fs = require('fs'); | ||
const meow = require('meow'); | ||
const chalk = require('chalk'); | ||
const indentString = require('indent-string'); | ||
const stdin = require('get-stdin'); | ||
const css = require('css'); | ||
const escapeRegExp = require('lodash.escaperegexp'); | ||
const defaults = require('lodash.defaults'); | ||
const inlineCritical = require('.'); | ||
let ok; | ||
@@ -36,2 +34,3 @@ const help = ` | ||
autoVersion: true, | ||
importMeta: import.meta, | ||
flags: { | ||
@@ -125,4 +124,4 @@ css: { | ||
function processError(error) { | ||
process.stderr.write(chalk.red(indentString(`Error: ${error.message || error}`, 2))); | ||
process.stderr.write(os.EOL); | ||
process.stderr.write(picocolors.red(indentString(`Error: ${error.message || error}`, 2))); | ||
process.stderr.write(EOL); | ||
process.stderr.write(indentString(help, 2)); | ||
@@ -134,3 +133,3 @@ process.exit(1); | ||
try { | ||
return fs.readFileSync(file, 'utf8'); | ||
return readFileSync(file, 'utf8'); | ||
} catch (error) { | ||
@@ -148,3 +147,3 @@ processError(error); | ||
try { | ||
css.parse(data); | ||
parse(data); | ||
options_.css = data; | ||
@@ -159,3 +158,3 @@ } catch { | ||
try { | ||
css.parse(temporary); | ||
parse(temporary); | ||
options_.css = temporary; | ||
@@ -175,3 +174,3 @@ } catch { | ||
const out = inlineCritical(html, styles, options); | ||
process.stdout.write(out.toString(), process.exit); | ||
process.stdout.write(out?.toString() ?? '', process.exit); | ||
} catch (error) { | ||
@@ -178,0 +177,0 @@ processError(error); |
36
index.js
@@ -12,14 +12,14 @@ /** | ||
'use strict'; | ||
import {existsSync, readFileSync, writeFileSync} from 'node:fs'; | ||
import process from 'node:process'; | ||
import {Buffer} from 'node:buffer'; | ||
import {resolve, join} from 'node:path'; | ||
import isString from 'lodash.isstring'; | ||
import isRegExp from 'lodash.isregexp'; | ||
import reaver from 'reaver'; | ||
import slash from 'slash'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const isString = require('lodash.isstring'); | ||
const isRegExp = require('lodash.isregexp'); | ||
const reaver = require('reaver'); | ||
const slash = require('slash'); | ||
import Dom from './src/dom.js'; | ||
import {removeDuplicateStyles} from './src/css.js'; | ||
const Dom = require('./src/dom'); | ||
const {removeDuplicateStyles} = require('./src/css'); | ||
const DEFAULT_OPTIONS = { | ||
@@ -50,4 +50,4 @@ extract: false, | ||
*/ | ||
function inline(html, styles, options) { | ||
const o = {...DEFAULT_OPTIONS, ...(options || {})}; | ||
export function inline(html, styles, options) { | ||
const o = {...DEFAULT_OPTIONS, ...options}; | ||
@@ -63,3 +63,3 @@ if (!isString(html)) { | ||
if (!Array.isArray(o.ignore)) { | ||
o.ignore = [o.ignore].filter((i) => i); | ||
o.ignore = [o.ignore].filter(Boolean); | ||
} | ||
@@ -168,10 +168,10 @@ | ||
if (o.extract) { | ||
const file = path.resolve(path.join(o.basePath || process.cwd, href)); | ||
const file = resolve(join(o.basePath || process.cwd, href)); | ||
if (fs.existsSync(file)) { | ||
const orig = fs.readFileSync(file); | ||
if (existsSync(file)) { | ||
const orig = readFileSync(file); | ||
const diff = removeDuplicateStyles(orig, inlined); | ||
const filename = reaver.rev(file, diff); | ||
fs.writeFileSync(filename, diff); | ||
writeFileSync(filename, diff); | ||
link.setAttribute('href', normalizePath(reaver.rev(href, diff))); | ||
@@ -231,2 +231,2 @@ } else if (!/\/\//.test(href)) { | ||
module.exports = inline; | ||
export default inline; |
{ | ||
"name": "inline-critical", | ||
"version": "9.0.1", | ||
"version": "10.0.0", | ||
"description": "Inline critical-path css and load the existing stylesheets asynchronously", | ||
"main": "index.js", | ||
"exports": "./index.js", | ||
"type": "module", | ||
"scripts": { | ||
"jest": "jest --coverage", | ||
"xo": "xo", | ||
"jest": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage --runInBand --detectOpenHandles", | ||
"xo": "xo --fix", | ||
"test": "npm run xo && npm run jest" | ||
@@ -35,10 +37,9 @@ }, | ||
"dependencies": { | ||
"chalk": "^4.1.2", | ||
"clean-css": "^5.2.0", | ||
"clean-css": "^5.3.1", | ||
"css": "^3.0.0", | ||
"detect-indent": "^6.1.0", | ||
"detect-indent": "^7.0.1", | ||
"fg-loadcss": "^2.1.0", | ||
"get-stdin": "^8.0.0", | ||
"indent-string": "^4.0.0", | ||
"jsdom": "^17.0.0", | ||
"get-stdin": "^9.0.0", | ||
"indent-string": "^5.0.0", | ||
"jsdom": "^20.0.0", | ||
"lodash.defaults": "^4.2.0", | ||
@@ -48,16 +49,19 @@ "lodash.escaperegexp": "^4.1.2", | ||
"lodash.isstring": "^4.0.1", | ||
"meow": "^9.0.0", | ||
"normalize-newline": "^3.0.0", | ||
"postcss": "^8.3.8", | ||
"meow": "^10.1.3", | ||
"normalize-newline": "^4.1.0", | ||
"picocolors": "^1.0.0", | ||
"postcss": "^8.4.16", | ||
"postcss-discard": "^2.0.0", | ||
"reaver": "^2.0.0", | ||
"slash": "^3.0.0", | ||
"uglify-js": "^3.14.2" | ||
"slash": "^5.0.0", | ||
"uglify-js": "^3.17.1" | ||
}, | ||
"devDependencies": { | ||
"execa": "^5.1.1", | ||
"fs-extra": "^9.1.0", | ||
"jest": "^27.2.2", | ||
"read-pkg-up": "^7.0.1", | ||
"xo": "^0.44.0" | ||
"cross-env": "^7.0.3", | ||
"eslint": "^8.23.1", | ||
"execa": "^6.1.0", | ||
"fs-extra": "^10.1.0", | ||
"jest": "^29.0.3", | ||
"read-pkg-up": "^9.1.0", | ||
"xo": "^0.52.3" | ||
}, | ||
@@ -91,4 +95,4 @@ "xo": { | ||
"engines": { | ||
"node": "^10 || ^12 || >=14" | ||
"node": ">=14.16" | ||
} | ||
} |
@@ -1,8 +0,6 @@ | ||
'use strict'; | ||
import CleanCSS from 'clean-css'; | ||
import postcss from 'postcss'; | ||
import discard from 'postcss-discard'; | ||
import normalizeNewline from 'normalize-newline'; | ||
const CleanCSS = require('clean-css'); | ||
const postcss = require('postcss'); | ||
const discard = require('postcss-discard'); | ||
const normalizeNewline = require('normalize-newline'); | ||
/** | ||
@@ -13,3 +11,3 @@ * Minify CSS | ||
*/ | ||
function minifyCss(styles) { | ||
export function minifyCss(styles) { | ||
return new CleanCSS().minify(styles).styles; | ||
@@ -24,3 +22,3 @@ } | ||
*/ | ||
function removeDuplicateStyles(styles, ...css) { | ||
export function removeDuplicateStyles(styles, ...css) { | ||
const _styles = normalizeNewline(minifyCss(styles || '')); | ||
@@ -34,6 +32,1 @@ const _css = normalizeNewline(minifyCss(css.join('\n'))); | ||
} | ||
module.exports = { | ||
minifyCss, | ||
removeDuplicateStyles, | ||
}; |
@@ -1,9 +0,9 @@ | ||
'use strict'; | ||
import {readFileSync} from 'node:fs'; | ||
import {join, dirname} from 'node:path'; | ||
import {createRequire} from 'node:module'; | ||
import {JSDOM} from 'jsdom'; | ||
import detectIndent from 'detect-indent'; | ||
import {minify as _minify} from 'uglify-js'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const {JSDOM} = require('jsdom'); | ||
const detectIndent = require('detect-indent'); | ||
const UglifyJS = require('uglify-js'); | ||
const require = createRequire(import.meta.url); | ||
const loadCssMain = require.resolve('fg-loadcss'); | ||
@@ -19,5 +19,5 @@ | ||
function getScript() { | ||
const loadCSS = fs.readFileSync(path.join(path.dirname(loadCssMain), 'cssrelpreload.js'), 'utf8'); | ||
const loadCSS = readFileSync(join(dirname(loadCssMain), 'cssrelpreload.js'), 'utf8'); | ||
return UglifyJS.minify(loadCSS).code.trim(); | ||
return _minify(loadCSS).code.trim(); | ||
} | ||
@@ -154,5 +154,3 @@ | ||
querySelector(...selector) { | ||
const s = flatten(selector) | ||
.filter((s) => s) | ||
.join(','); | ||
const s = flatten(selector).filter(Boolean).join(','); | ||
@@ -163,5 +161,3 @@ return this.document.querySelector(s); | ||
querySelectorAll(...selector) { | ||
const s = flatten(selector) | ||
.filter((s) => s) | ||
.join(','); | ||
const s = flatten(selector).filter(Boolean).join(','); | ||
@@ -255,2 +251,2 @@ return this.document.querySelectorAll(s); | ||
module.exports = Dom; | ||
export default Dom; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
26670
0
Yes
7
584
+ Addedpicocolors@^1.0.0
+ Added@tootallnate/once@2.0.0(transitive)
+ Addedacorn-globals@7.0.1(transitive)
+ Addedacorn-walk@8.3.4(transitive)
+ Addedcamelcase@6.3.0(transitive)
+ Addedcamelcase-keys@7.0.2(transitive)
+ Addeddecamelize@5.0.1(transitive)
+ Addeddetect-indent@7.0.1(transitive)
+ Addeddomexception@4.0.0(transitive)
+ Addedentities@4.5.0(transitive)
+ Addedfind-up@5.0.0(transitive)
+ Addedget-stdin@9.0.0(transitive)
+ Addedhtml-encoding-sniffer@3.0.0(transitive)
+ Addedhttp-proxy-agent@5.0.0(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedindent-string@5.0.0(transitive)
+ Addedjsdom@20.0.3(transitive)
+ Addedlocate-path@6.0.0(transitive)
+ Addedmeow@10.1.5(transitive)
+ Addednormalize-newline@4.1.0(transitive)
+ Addedp-limit@3.1.0(transitive)
+ Addedp-locate@5.0.0(transitive)
+ Addedparse5@7.2.1(transitive)
+ Addedquick-lru@5.1.1(transitive)
+ Addedread-pkg@6.0.0(transitive)
+ Addedread-pkg-up@8.0.0(transitive)
+ Addedredent@4.0.0(transitive)
+ Addedreplace-buffer@1.2.1(transitive)
+ Addedsaxes@6.0.0(transitive)
+ Addedslash@5.1.0(transitive)
+ Addedstrip-indent@4.0.0(transitive)
+ Addedtrim-newlines@4.1.1(transitive)
+ Addedtype-fest@1.4.0(transitive)
+ Addedw3c-xmlserializer@4.0.0(transitive)
+ Addedwhatwg-encoding@2.0.0(transitive)
+ Addedxml-name-validator@4.0.0(transitive)
+ Addedyocto-queue@0.1.0(transitive)
- Removedchalk@^4.1.2
- Removed@tootallnate/once@1.1.2(transitive)
- Removedacorn@7.4.1(transitive)
- Removedacorn-globals@6.0.0(transitive)
- Removedacorn-walk@7.2.0(transitive)
- Removedansi-styles@4.3.0(transitive)
- Removedbrowser-process-hrtime@1.0.0(transitive)
- Removedcamelcase@5.3.1(transitive)
- Removedcamelcase-keys@6.2.2(transitive)
- Removedchalk@4.1.2(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removeddetect-indent@6.1.0(transitive)
- Removeddomexception@2.0.1(transitive)
- Removedfind-up@4.1.0(transitive)
- Removedget-stdin@8.0.0(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedhosted-git-info@2.8.9(transitive)
- Removedhtml-encoding-sniffer@2.0.1(transitive)
- Removedhttp-proxy-agent@4.0.1(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedindent-string@4.0.0(transitive)
- Removedjsdom@17.0.0(transitive)
- Removedlocate-path@5.0.0(transitive)
- Removedmeow@9.0.0(transitive)
- Removednormalize-newline@3.0.0(transitive)
- Removednormalize-package-data@2.5.0(transitive)
- Removedp-limit@2.3.0(transitive)
- Removedp-locate@4.1.0(transitive)
- Removedp-try@2.2.0(transitive)
- Removedparse5@6.0.1(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedquick-lru@4.0.1(transitive)
- Removedread-pkg@5.2.0(transitive)
- Removedread-pkg-up@7.0.1(transitive)
- Removedredent@3.0.0(transitive)
- Removedresolve@1.22.8(transitive)
- Removedsaxes@5.0.1(transitive)
- Removedsemver@5.7.2(transitive)
- Removedslash@3.0.0(transitive)
- Removedstrip-indent@3.0.0(transitive)
- Removedsupports-color@7.2.0(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedtr46@2.1.0(transitive)
- Removedtrim-newlines@3.0.1(transitive)
- Removedtype-fest@0.18.10.6.00.8.1(transitive)
- Removedw3c-hr-time@1.0.2(transitive)
- Removedw3c-xmlserializer@2.0.0(transitive)
- Removedwebidl-conversions@5.0.06.1.0(transitive)
- Removedwhatwg-encoding@1.0.5(transitive)
- Removedwhatwg-mimetype@2.3.0(transitive)
- Removedwhatwg-url@9.1.0(transitive)
- Removedxml-name-validator@3.0.0(transitive)
Updatedclean-css@^5.3.1
Updateddetect-indent@^7.0.1
Updatedget-stdin@^9.0.0
Updatedindent-string@^5.0.0
Updatedjsdom@^20.0.0
Updatedmeow@^10.1.3
Updatednormalize-newline@^4.1.0
Updatedpostcss@^8.4.16
Updatedslash@^5.0.0
Updateduglify-js@^3.17.1