Skip to content

Commit 888d72e

Browse files
fix: unexpected failing on CSS syntax error (#593)
1 parent 004334a commit 888d72e

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/index.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { satisfies } from "semver";
44
import postcssPackage from "postcss/package.json";
55

66
import Warning from "./Warning";
7-
import SyntaxError from "./Error";
87
import schema from "./options.json";
98
import {
109
loadConfig,
@@ -14,6 +13,7 @@ import {
1413
normalizeSourceMapAfterPostcss,
1514
findPackageJSONDir,
1615
getPostcssImplementation,
16+
reportError,
1717
} from "./utils";
1818

1919
let hasExplicitDependencyOnPostCSS = false;
@@ -169,15 +169,7 @@ export default async function loader(content, sourceMap, meta) {
169169
}
170170
}
171171

172-
if (error.file) {
173-
this.addDependency(error.file);
174-
}
175-
176-
if (error.name === "CssSyntaxError") {
177-
callback(new SyntaxError(error));
178-
} else {
179-
callback(error);
180-
}
172+
reportError(this, callback, error);
181173

182174
return;
183175
}
@@ -223,11 +215,19 @@ export default async function loader(content, sourceMap, meta) {
223215
map = normalizeSourceMapAfterPostcss(map, this.context);
224216
}
225217

226-
const ast = {
227-
type: "postcss",
228-
version: result.processor.version,
229-
root: result.root,
230-
};
218+
let ast;
219+
220+
try {
221+
ast = {
222+
type: "postcss",
223+
version: result.processor.version,
224+
root: result.root,
225+
};
226+
} catch (error) {
227+
reportError(this, callback, error);
228+
229+
return;
230+
}
231231

232232
callback(null, result.css, map, { ast });
233233
}

src/utils.js

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Module from "module";
44
import { klona } from "klona/full";
55
import { cosmiconfig } from "cosmiconfig";
66

7+
import SyntaxError from "./Error";
8+
79
const parentModule = module;
810

911
const stat = (inputFileSystem, filePath) =>
@@ -450,6 +452,18 @@ function getPostcssImplementation(loaderContext, implementation) {
450452
return resolvedImplementation;
451453
}
452454

455+
function reportError(loaderContext, callback, error) {
456+
if (error.file) {
457+
loaderContext.addDependency(error.file);
458+
}
459+
460+
if (error.name === "CssSyntaxError") {
461+
callback(new SyntaxError(error));
462+
} else {
463+
callback(error);
464+
}
465+
}
466+
453467
export {
454468
loadConfig,
455469
getPostcssOptions,
@@ -458,4 +472,5 @@ export {
458472
normalizeSourceMapAfterPostcss,
459473
findPackageJSONDir,
460474
getPostcssImplementation,
475+
reportError,
461476
};

0 commit comments

Comments
 (0)