Skip to content

Commit 65748ec

Browse files
fix: warning and error serialization
1 parent a98d7d0 commit 65748ec

File tree

6 files changed

+120
-117
lines changed

6 files changed

+120
-117
lines changed

src/Error.js

-39
This file was deleted.

src/Warning.js

-32
This file was deleted.

src/index.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import path from "path";
22

3-
import { satisfies } from "semver";
43
import postcssPackage from "postcss/package.json";
54

6-
import Warning from "./Warning";
75
import schema from "./options.json";
86
import {
97
loadConfig,
@@ -14,6 +12,7 @@ import {
1412
findPackageJSONDir,
1513
getPostcssImplementation,
1614
reportError,
15+
warningFactory,
1716
} from "./utils";
1817

1918
let hasExplicitDependencyOnPostCSS = false;
@@ -40,9 +39,17 @@ export default async function loader(content, sourceMap, meta) {
4039
? true
4140
: options.postcssOptions.config;
4241

43-
const postcssFactory = getPostcssImplementation(this, options.implementation);
42+
let implementation;
4443

45-
if (!postcssFactory) {
44+
try {
45+
implementation = getPostcssImplementation(this, options.implementation);
46+
} catch (error) {
47+
callback(error);
48+
49+
return;
50+
}
51+
52+
if (!implementation) {
4653
callback(
4754
new Error(
4855
`The Postcss implementation "${options.implementation}" not found`
@@ -98,7 +105,8 @@ export default async function loader(content, sourceMap, meta) {
98105
meta &&
99106
meta.ast &&
100107
meta.ast.type === "postcss" &&
101-
satisfies(meta.ast.version, `^${postcssPackage.version}`)
108+
// eslint-disable-next-line global-require
109+
require("semver").satisfies(meta.ast.version, `^${postcssPackage.version}`)
102110
) {
103111
({ root } = meta.ast);
104112
}
@@ -112,7 +120,7 @@ export default async function loader(content, sourceMap, meta) {
112120
let processor;
113121

114122
try {
115-
processor = postcssFactory(plugins);
123+
processor = implementation(plugins);
116124
result = await processor.process(root || content, processOptions);
117125
} catch (error) {
118126
// Check postcss versions to avoid using PostCSS 7.
@@ -175,7 +183,7 @@ export default async function loader(content, sourceMap, meta) {
175183
}
176184

177185
for (const warning of result.warnings()) {
178-
this.emitWarning(new Warning(warning));
186+
this.emitWarning(warningFactory(warning));
179187
}
180188

181189
for (const message of result.messages) {

src/utils.js

+55-12
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import Module from "module";
55
import { klona } from "klona/full";
66
import { cosmiconfig, defaultLoaders } from "cosmiconfig";
77

8-
import SyntaxError from "./Error";
9-
108
const parentModule = module;
119

1210
const stat = (inputFileSystem, filePath) =>
@@ -541,15 +539,8 @@ function getPostcssImplementation(loaderContext, implementation) {
541539
if (!implementation || typeof implementation === "string") {
542540
const postcssImplPkg = implementation || "postcss";
543541

544-
try {
545-
// eslint-disable-next-line import/no-dynamic-require, global-require
546-
resolvedImplementation = require(postcssImplPkg);
547-
} catch (error) {
548-
loaderContext.emitError(error);
549-
550-
// eslint-disable-next-line consistent-return
551-
return;
552-
}
542+
// eslint-disable-next-line import/no-dynamic-require, global-require
543+
resolvedImplementation = require(postcssImplPkg);
553544
}
554545

555546
// eslint-disable-next-line consistent-return
@@ -562,12 +553,63 @@ function reportError(loaderContext, callback, error) {
562553
}
563554

564555
if (error.name === "CssSyntaxError") {
565-
callback(new SyntaxError(error));
556+
callback(syntaxErrorFactory(error));
566557
} else {
567558
callback(error);
568559
}
569560
}
570561

562+
function warningFactory(obj) {
563+
let message = "";
564+
565+
if (typeof obj.line !== "undefined") {
566+
message += `(${obj.line}:${obj.column}) `;
567+
}
568+
569+
if (typeof obj.plugin !== "undefined") {
570+
message += `from "${obj.plugin}" plugin: `;
571+
}
572+
573+
message += obj.text;
574+
575+
if (obj.node) {
576+
message += `\n\nCode:\n ${obj.node.toString()}\n`;
577+
}
578+
579+
const warning = new Error(message);
580+
581+
warning.stack = null;
582+
583+
return warning;
584+
}
585+
586+
function syntaxErrorFactory(obj) {
587+
let message = "\nSyntaxError\n\n";
588+
589+
if (typeof obj.line !== "undefined") {
590+
message += `(${obj.line}:${obj.column}) `;
591+
}
592+
593+
if (typeof obj.plugin !== "undefined") {
594+
message += `from "${obj.plugin}" plugin: `;
595+
}
596+
597+
message += obj.file ? `${obj.file} ` : "<css input> ";
598+
message += `${obj.reason}`;
599+
600+
const code = obj.showSourceCode();
601+
602+
if (code) {
603+
message += `\n\n${code}\n`;
604+
}
605+
606+
const error = new Error(message);
607+
608+
error.stack = null;
609+
610+
return error;
611+
}
612+
571613
export {
572614
loadConfig,
573615
getPostcssOptions,
@@ -577,4 +619,5 @@ export {
577619
findPackageJSONDir,
578620
getPostcssImplementation,
579621
reportError,
622+
warningFactory,
580623
};

test/__snapshots__/implementation.test.js.snap

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
exports[`"implementation" option should throw error when unresolved package: errors 1`] = `
44
[
55
"ModuleBuildError: Module build failed (from \`replaced original path\`):
6-
Error: The Postcss implementation "unresolved" not found",
7-
"ModuleError: Module Error (from \`replaced original path\`):
8-
(Emitted value instead of an instance of Error) Error: Cannot find module 'unresolved' from 'src/utils.js'",
6+
NonErrorEmittedError: (Emitted value instead of an instance of Error) Error: Cannot find module 'unresolved' from 'src/utils.js'",
97
]
108
`;
119

test/__snapshots__/loader.test.js.snap

+49-24
Original file line numberDiff line numberDiff line change
@@ -106,53 +106,77 @@ exports[`loader should emit warning using the "messages" API: errors 1`] = `[]`;
106106
exports[`loader should emit warning using the "messages" API: warnings 1`] = `
107107
[
108108
"ModuleWarning: Module Warning (from \`replaced original path\`):
109-
Warning
109+
(10:3) from "postcss-plugin" plugin: <Message>
110110
111-
(10:3) postcss-plugin: <Message>",
111+
Code:
112+
color: green
113+
",
112114
"ModuleWarning: Module Warning (from \`replaced original path\`):
113-
Warning
115+
(14:3) from "postcss-plugin" plugin: <Message>
114116
115-
(14:3) postcss-plugin: <Message>",
117+
Code:
118+
color: blue
119+
",
116120
"ModuleWarning: Module Warning (from \`replaced original path\`):
117-
Warning
121+
(18:3) from "postcss-plugin" plugin: <Message>
118122
119-
(18:3) postcss-plugin: <Message>",
123+
Code:
124+
-x-border-color: blue blue *
125+
",
120126
"ModuleWarning: Module Warning (from \`replaced original path\`):
121-
Warning
127+
(19:3) from "postcss-plugin" plugin: <Message>
122128
123-
(19:3) postcss-plugin: <Message>",
129+
Code:
130+
-x-color: * #fafafa
131+
",
124132
"ModuleWarning: Module Warning (from \`replaced original path\`):
125-
Warning
133+
(23:3) from "postcss-plugin" plugin: <Message>
126134
127-
(23:3) postcss-plugin: <Message>",
135+
Code:
136+
-z-border-color: blue blue *
137+
",
128138
"ModuleWarning: Module Warning (from \`replaced original path\`):
129-
Warning
139+
(24:3) from "postcss-plugin" plugin: <Message>
130140
131-
(24:3) postcss-plugin: <Message>",
141+
Code:
142+
-z-color: * #fafafa
143+
",
132144
"ModuleWarning: Module Warning (from \`replaced original path\`):
133-
Warning
145+
(29:5) from "postcss-plugin" plugin: <Message>
134146
135-
(29:5) postcss-plugin: <Message>",
147+
Code:
148+
width: 500px
149+
",
136150
"ModuleWarning: Module Warning (from \`replaced original path\`):
137-
Warning
151+
(2:3) from "postcss-plugin" plugin: <Message>
138152
139-
(2:3) postcss-plugin: <Message>",
153+
Code:
154+
color: black
155+
",
140156
"ModuleWarning: Module Warning (from \`replaced original path\`):
141-
Warning
157+
(32:7) from "postcss-plugin" plugin: <Message>
142158
143-
(32:7) postcss-plugin: <Message>",
159+
Code:
160+
width: auto
161+
",
144162
"ModuleWarning: Module Warning (from \`replaced original path\`):
145-
Warning
163+
(36:7) from "postcss-plugin" plugin: <Message>
146164
147-
(36:7) postcss-plugin: <Message>",
165+
Code:
166+
color: white
167+
",
148168
"ModuleWarning: Module Warning (from \`replaced original path\`):
149-
Warning
169+
(41:5) from "postcss-plugin" plugin: <Message>
150170
151-
(41:5) postcss-plugin: <Message>",
171+
Code:
172+
display: block
173+
",
152174
"ModuleWarning: Module Warning (from \`replaced original path\`):
153-
Warning
175+
(6:3) from "postcss-plugin" plugin: <Message>
154176
155-
(6:3) postcss-plugin: <Message>",
177+
Code:
178+
color: red
179+
",
156180
]
157181
`;
158182
@@ -214,6 +238,7 @@ exports[`loader should reuse PostCSS AST: warnings 1`] = `[]`;
214238
exports[`loader should throw an error on invalid syntax: errors 1`] = `
215239
[
216240
"ModuleBuildError: Module build failed (from \`replaced original path\`):
241+
217242
SyntaxError
218243
219244
(1:3) /test/fixtures/css/style.css Unnecessary curly bracket

0 commit comments

Comments
 (0)