Skip to content

Commit

Permalink
only fail on css parse error in strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Apr 9, 2023
1 parent cd2190e commit ee81a14
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ generate({
| request | `object` | `{}` | Configuration options for [`got`](hhttps://github.com/sindresorhus/got). |
| user | `string` | `undefined` | RFC2617 basic authorization: user |
| pass | `string` | `undefined` | RFC2617 basic authorization: pass |
| strict | `boolean` | `false` | Throw an error if no css is found |
| strict | `boolean` | `false` | Throw an error on css parsing errors or if no css is found. |

## CLI

Expand Down
7 changes: 6 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ Options:
--ignoreInlinedStyles Ignore inlined stylesheets
--include RegExp, @type or selector to include
--include-[OPTION] Pass options to inline-critical. See https://goo.gl/w6SHJM
--assetPaths Directories/Urls where the inliner should start looking for assets.
--assetPaths Directories/Urls where the inliner should start looking for assets
--user RFC2617 basic authorization user
--pass RFC2617 basic authorization password
--penthouse-[OPTION] Pass options to penthouse. See https://goo.gl/PQ5HLL
--ua, --userAgent User agent to use when fetching remote src
--strict Throw an error on css parsing errors or if no css is found
`;

const meowOpts = {
Expand Down Expand Up @@ -75,6 +76,10 @@ const meowOpts = {
user: {
type: 'string',
},
strict: {
type: 'boolean',
default: false,
},
pass: {
type: 'string',
},
Expand Down
65 changes: 39 additions & 26 deletions src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,15 @@ function glob(pattern, {base} = {}) {
* @param {Buffer|string} css Stylesheet
* @param {string} from Rebase from url
* @param {string} to Rebase to url
* @param {string|function} method Rebase method. See https://github.com/postcss/postcss-url#options-combinations
* @param {opject} options
* method: {string|function} method Rebase method. See https://github.com/postcss/postcss-url#options-combinations
* strict: fail on invalid css
* inlined: boolean flag indicating inlined css
* @param {boolean} strict fail on invalid css
* @returns {Buffer} Rebased css
*/
async function rebaseAssets(css, from, to, method = 'rebase', strict = false) {
async function rebaseAssets(css, from, to, options = {}) {
const {method = 'rebase', strict = false, inlined = false} = options;
let rebased = css.toString();

debug('Rebase assets', {from, to});
Expand Down Expand Up @@ -308,6 +312,9 @@ async function rebaseAssets(css, from, to, method = 'rebase', strict = false) {
}
} catch (error) {
if (strict) {
if (inlined) {
error.message = error.message.replace(from, 'Inlined stylesheet');
}
throw error;

Check failure on line 318 in src/file.js

View workflow job for this annotation

GitHub Actions / Node 14.x on ubuntu-latest

Expected blank line before this statement.

Check failure on line 318 in src/file.js

View workflow job for this annotation

GitHub Actions / Node 14.x on windows-latest

Expected blank line before this statement.

Check failure on line 318 in src/file.js

View workflow job for this annotation

GitHub Actions / Node 16.x on ubuntu-latest

Expected blank line before this statement.

Check failure on line 318 in src/file.js

View workflow job for this annotation

GitHub Actions / Node 16.x on windows-latest

Expected blank line before this statement.

Check failure on line 318 in src/file.js

View workflow job for this annotation

GitHub Actions / Node 14.x on ubuntu-latest

Expected blank line before this statement.

Check failure on line 318 in src/file.js

View workflow job for this annotation

GitHub Actions / Node 14.x on windows-latest

Expected blank line before this statement.

Check failure on line 318 in src/file.js

View workflow job for this annotation

GitHub Actions / Node 16.x on ubuntu-latest

Expected blank line before this statement.

Check failure on line 318 in src/file.js

View workflow job for this annotation

GitHub Actions / Node 16.x on windows-latest

Expected blank line before this statement.
}

Expand Down Expand Up @@ -845,44 +852,50 @@ export async function getStylesheet(document, filepath, options = {}) {
}

if (rebase.from && rebase.to) {
file.contents = await rebaseAssets(file.contents, rebase.from, rebase.to, 'rebase', options.strict);
file.contents = await rebaseAssets(file.contents, rebase.from, rebase.to, {
method: 'rebase',
strict: options.strict,
inlined: Buffer.isBuffer(originalPath),
});
} else if (typeof rebase === 'function') {
file.contents = await rebaseAssets(file.contents, stylepath, document.virtualPath, rebase, options.strict);
file.contents = await rebaseAssets(file.contents, stylepath, document.virtualPath, {
method: rebase,
strict: options.strict,
inlined: Buffer.isBuffer(originalPath),
});
// Next rebase to the stylesheet url
} else if (isRemote(rebase.to || stylepath)) {
const from = rebase.from || stylepath;
const to = rebase.to || stylepath;
const method = (asset) => (isRemote(asset.originUrl) ? asset.originUrl : urlResolve(to, asset.originUrl));
file.contents = await rebaseAssets(file.contents, from, to, method, options.strict);
file.contents = await rebaseAssets(file.contents, from, to, {
method,
strict: options.strict,
inlined: Buffer.isBuffer(originalPath),
});

// Use relative path to document (local)
} else if (document.virtualPath) {
file.contents = await rebaseAssets(
file.contents,
rebase.from || stylepath,
rebase.to || document.virtualPath,
'rebase',
options.strict
);
file.contents = await rebaseAssets(file.contents, rebase.from || stylepath, rebase.to || document.virtualPath, {
method: 'rebase',
strict: options.strict,
inlined: Buffer.isBuffer(originalPath),
});
} else if (document.remote) {
const {pathname} = document.urlObj;
file.contents = await rebaseAssets(
file.contents,
rebase.from || stylepath,
rebase.to || pathname,
'rebase',
options.strict
);
file.contents = await rebaseAssets(file.contents, rebase.from || stylepath, rebase.to || pathname, {
method: 'rebase',
strict: options.strict,
inlined: Buffer.isBuffer(originalPath),
});

// Make images absolute if we have an absolute positioned stylesheet
} else if (isAbsolute(stylepath)) {
file.contents = await rebaseAssets(
file.contents,
rebase.from || stylepath,
rebase.to || '/index.html',
(asset) => normalizePath(asset.absolutePath),
options.strict
);
file.contents = await rebaseAssets(file.contents, rebase.from || stylepath, rebase.to || '/index.html', {
method: (asset) => normalizePath(asset.absolutePath),
strict: options.strict,
inlined: Buffer.isBuffer(originalPath),
});
} else {
warn(`Not rebasing assets for ${originalPath}. Use "rebase" option`);
}
Expand Down

0 comments on commit ee81a14

Please sign in to comment.