Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unexpected failing on CSS syntax error #593

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { satisfies } from "semver";
import postcssPackage from "postcss/package.json";

import Warning from "./Warning";
import SyntaxError from "./Error";
import schema from "./options.json";
import {
loadConfig,
Expand All @@ -14,6 +13,7 @@ import {
normalizeSourceMapAfterPostcss,
findPackageJSONDir,
getPostcssImplementation,
reportError,
} from "./utils";

let hasExplicitDependencyOnPostCSS = false;
Expand Down Expand Up @@ -169,15 +169,7 @@ export default async function loader(content, sourceMap, meta) {
}
}

if (error.file) {
this.addDependency(error.file);
}

if (error.name === "CssSyntaxError") {
callback(new SyntaxError(error));
} else {
callback(error);
}
reportError(this, callback, error);

return;
}
Expand Down Expand Up @@ -223,11 +215,19 @@ export default async function loader(content, sourceMap, meta) {
map = normalizeSourceMapAfterPostcss(map, this.context);
}

const ast = {
type: "postcss",
version: result.processor.version,
root: result.root,
};
let ast;

try {
ast = {
type: "postcss",
version: result.processor.version,
root: result.root,
};
} catch (error) {
reportError(this, callback, error);

return;
}

callback(null, result.css, map, { ast });
}
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Module from "module";
import { klona } from "klona/full";
import { cosmiconfig } from "cosmiconfig";

import SyntaxError from "./Error";

const parentModule = module;

const stat = (inputFileSystem, filePath) =>
Expand Down Expand Up @@ -450,6 +452,18 @@ function getPostcssImplementation(loaderContext, implementation) {
return resolvedImplementation;
}

function reportError(loaderContext, callback, error) {
if (error.file) {
loaderContext.addDependency(error.file);
}

if (error.name === "CssSyntaxError") {
callback(new SyntaxError(error));
} else {
callback(error);
}
}

export {
loadConfig,
getPostcssOptions,
Expand All @@ -458,4 +472,5 @@ export {
normalizeSourceMapAfterPostcss,
findPackageJSONDir,
getPostcssImplementation,
reportError,
};