Skip to content

Commit

Permalink
fix: unexpected failing on CSS syntax error (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jul 11, 2022
1 parent 004334a commit 888d72e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/index.js
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
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,
};

0 comments on commit 888d72e

Please sign in to comment.