Skip to content

Commit

Permalink
Merge branch 'master' into bump
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Dec 11, 2022
2 parents 46b5962 + de51d5c commit c8ffbf9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions cli.js
Expand Up @@ -44,6 +44,7 @@ const meowOpts = {
css: {
type: 'string',
alias: 'c',
isMultiple: true,
},
width: {
alias: 'w',
Expand Down
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.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "critical",
"version": "5.0.2",
"version": "5.0.3",
"description": "Extract & Inline Critical-path CSS from HTML",
"author": "Addy Osmani",
"license": "Apache-2.0",
Expand Down
8 changes: 5 additions & 3 deletions src/file.js
Expand Up @@ -35,6 +35,8 @@ const unlinkAsync = promisify(fs.unlink);
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);

export const checkCssOption = (css) => Boolean((!Array.isArray(css) && css) || (Array.isArray(css) && css.length > 0));

export async function outputFileAsync(file, data) {
const dir = path.dirname(file);

Expand Down Expand Up @@ -793,7 +795,7 @@ export async function getStylesheet(document, filepath, options = {}) {

// Create absolute file paths for local files passed via css option
// to prevent document relative stylesheet paths if they are not relative specified
if (!Buffer.isBuffer(originalPath) && !isVinyl(filepath) && !isRemote(filepath) && css) {
if (!Buffer.isBuffer(originalPath) && !isVinyl(filepath) && !isRemote(filepath) && checkCssOption(css)) {
filepath = path.resolve(filepath);
}

Expand All @@ -803,7 +805,7 @@ export async function getStylesheet(document, filepath, options = {}) {
}

// Restore original path for local files referenced from document and not from options
if (!Buffer.isBuffer(originalPath) && !isRemote(originalPath) && !css) {
if (!Buffer.isBuffer(originalPath) && !isRemote(originalPath) && !checkCssOption(css)) {
file.path = originalPath;
}

Expand Down Expand Up @@ -868,7 +870,7 @@ async function getCss(document, options = {}) {
const {css} = options;
let stylesheets = [];

if (css) {
if (checkCssOption(css)) {
const files = await glob(css, options);
stylesheets = await mapAsync(files, (file) => getStylesheet(document, file, options));
debug('(getCss) css option set', files, stylesheets);
Expand Down
10 changes: 10 additions & 0 deletions test/file.test.js
Expand Up @@ -16,6 +16,7 @@ import {FileNotFoundError} from '../src/errors.js';
import {
BASE_WARNING,
isRemote,
checkCssOption,
fileExists,
joinPath,
urlParse,
Expand Down Expand Up @@ -61,6 +62,15 @@ afterEach(() => {
stderr.mockRestore();
});

test('checkCssOption', () => {
expect(checkCssOption(undefined)).toEqual(false);
expect(checkCssOption('')).toEqual(false);
expect(checkCssOption(false)).toEqual(false);
expect(checkCssOption([])).toEqual(false);
expect(checkCssOption(['abc'])).toEqual(true);
expect(checkCssOption('abc')).toEqual(true);
});

test('Normalize paths', () => {
const plattform = process.platform;
Object.defineProperty(process, 'platform', {value: 'win32'});
Expand Down

0 comments on commit c8ffbf9

Please sign in to comment.