Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit65748ec

Browse files
fix: warning and error serialization
1 parenta98d7d0 commit65748ec

File tree

6 files changed

+120
-117
lines changed

6 files changed

+120
-117
lines changed

‎src/Error.js‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎src/Warning.js‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

‎src/index.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
importpathfrom"path";
22

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

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

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

43-
constpostcssFactory=getPostcssImplementation(this,options.implementation);
42+
letimplementation;
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
newError(
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
letprocessor;
113121

114122
try{
115-
processor=postcssFactory(plugins);
123+
processor=implementation(plugins);
116124
result=awaitprocessor.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(constwarningofresult.warnings()){
178-
this.emitWarning(newWarning(warning));
186+
this.emitWarning(warningFactory(warning));
179187
}
180188

181189
for(constmessageofresult.messages){

‎src/utils.js‎

Lines changed: 55 additions & 12 deletions
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-
importSyntaxErrorfrom"./Error";
9-
108
constparentModule=module;
119

1210
conststat=(inputFileSystem,filePath)=>
@@ -541,15 +539,8 @@ function getPostcssImplementation(loaderContext, implementation) {
541539
if(!implementation||typeofimplementation==="string"){
542540
constpostcssImplPkg=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(newSyntaxError(error));
556+
callback(syntaxErrorFactory(error));
566557
}else{
567558
callback(error);
568559
}
569560
}
570561

562+
functionwarningFactory(obj){
563+
letmessage="";
564+
565+
if(typeofobj.line!=="undefined"){
566+
message+=`(${obj.line}:${obj.column}) `;
567+
}
568+
569+
if(typeofobj.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+
constwarning=newError(message);
580+
581+
warning.stack=null;
582+
583+
returnwarning;
584+
}
585+
586+
functionsyntaxErrorFactory(obj){
587+
letmessage="\nSyntaxError\n\n";
588+
589+
if(typeofobj.line!=="undefined"){
590+
message+=`(${obj.line}:${obj.column}) `;
591+
}
592+
593+
if(typeofobj.plugin!=="undefined"){
594+
message+=`from "${obj.plugin}" plugin: `;
595+
}
596+
597+
message+=obj.file ?`${obj.file} ` :"<css input> ";
598+
message+=`${obj.reason}`;
599+
600+
constcode=obj.showSourceCode();
601+
602+
if(code){
603+
message+=`\n\n${code}\n`;
604+
}
605+
606+
consterror=newError(message);
607+
608+
error.stack=null;
609+
610+
returnerror;
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‎

Lines changed: 1 addition & 3 deletions
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‎

Lines changed: 49 additions & 24 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp