Skip to content

Commit

Permalink
Merge 0ab46fe into 43fb06b
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Oct 1, 2022
2 parents 43fb06b + 0ab46fe commit 2d26174
Show file tree
Hide file tree
Showing 10 changed files with 2,951 additions and 11,843 deletions.
36 changes: 28 additions & 8 deletions .github/workflows/test.yml
Expand Up @@ -11,31 +11,51 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [12, 14, 16]
node: [14, 16]
os: [ubuntu-latest, windows-latest]

steps:
- name: Clone repository
uses: actions/checkout@v1
with:
fetch-depth: 5
uses: actions/checkout@v2

- name: Set Node.js version
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}

- run: node --version
- run: npm --version

- name: Set up npm cache
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-v${{ matrix.node }}-${{ hashFiles('package.json') }}-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-v${{ matrix.node }}-${{ hashFiles('package.json') }}-${{ hashFiles('package-lock.json') }}
${{ runner.OS }}-node-v${{ matrix.node }}-
- name: Install npm dependencies
run: npm ci

- name: Run tests
run: npm test

- name: Run Coveralls
- name: Coveralls Parallel
uses: coverallsapp/github-action@master
if: startsWith(matrix.os, 'ubuntu') && matrix.node == 12
env:
NODE_COVERALLS_DEBUG: 1
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
github-token: ${{ secrets.github_token }}
flag-name: run-${{ matrix.os }}-${{ matrix.node }}
parallel: true

finish:
needs: run
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
37 changes: 18 additions & 19 deletions cli.js
@@ -1,17 +1,15 @@
#!/usr/bin/env node

'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('.');
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';

let ok;
const help = `
Expand All @@ -33,6 +31,7 @@ Options:
const cli = meow(help, {
autoHelp: true,
autoVersion: true,
importMeta: import.meta,
flags: {
css: {
type: 'string',
Expand Down Expand Up @@ -121,15 +120,15 @@ cli.flags = Object.entries(cli.flags).reduce((result, [key, value]) => {
}, {});

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));
process.exit(1);
}

function read(file) {
try {
return fs.readFileSync(file, 'utf8');
return readFileSync(file, 'utf8');
} catch (error) {
processError(error);
}
Expand All @@ -142,7 +141,7 @@ function run(data) {
if (data) {
// Detect html
try {
css.parse(data);
parse(data);
options_.css = data;
} catch {
options_.html = data;
Expand All @@ -152,7 +151,7 @@ function run(data) {
(cli.input || []).forEach((file) => {
const temporary = read(file);
try {
css.parse(temporary);
parse(temporary);
options_.css = temporary;
} catch {
options_.html = temporary;
Expand All @@ -167,7 +166,7 @@ function run(data) {

try {
const out = inlineCritical(html, styles, options);
process.stdout.write(out.toString(), process.exit);
process.stdout.write(out?.toString() ?? '', process.exit);
} catch (error) {
processError(error);
}
Expand Down
38 changes: 19 additions & 19 deletions index.js
Expand Up @@ -9,17 +9,17 @@
* All rights reserved.
*/

'use strict';

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');

const Dom = require('./src/dom');
const {removeDuplicateStyles} = require('./src/css');
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';

import Dom from './src/dom.js';
import {removeDuplicateStyles} from './src/css.js';

const DEFAULT_OPTIONS = {
extract: false,
Expand All @@ -46,8 +46,8 @@ function normalizePath(string) {
* @param {object} options Options
* @returns {string} HTML Source with inlined critical css
*/
function inline(html, styles, options) {
const o = {...DEFAULT_OPTIONS, ...(options || {})};
export function inline(html, styles, options) {
const o = {...DEFAULT_OPTIONS, ...options};

if (!isString(html)) {
html = String(html);
Expand All @@ -58,7 +58,7 @@ function inline(html, styles, options) {
}

if (!Array.isArray(o.ignore)) {
o.ignore = [o.ignore].filter((i) => i);
o.ignore = [o.ignore].filter(Boolean);
}

const document = new Dom(html, o);
Expand Down Expand Up @@ -162,14 +162,14 @@ function inline(html, styles, options) {
const integrity = link.getAttribute('integrity');

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)));
} else if (!/\/\//.test(href)) {
throw new Error(`Error: file "${href}" not found in "${o.basePath || process.cwd}". Specify base path.`);
Expand Down Expand Up @@ -224,4 +224,4 @@ function inline(html, styles, options) {
return Buffer.from(document.serialize());
}

module.exports = inline;
export default inline;

0 comments on commit 2d26174

Please sign in to comment.