Skip to content

Commit 0084b93

Browse files
fix: warning output (#1173)
1 parent 8a5e16c commit 0084b93

8 files changed

+339
-66
lines changed

src/utils.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ async function getSassOptions(
127127
const needEmitWarning = loaderOptions.warnRuleAsWarning !== false;
128128
const logger = loaderContext.getLogger("sass-loader");
129129
const formatSpan = (span) =>
130-
`${span.url || "-"}:${span.start.line}:${span.start.column}: `;
130+
`Warning on line ${span.start.line}, column ${span.start.column} of ${
131+
span.url || "-"
132+
}:${span.start.line}:${span.start.column}:\n`;
131133
const formatDebugSpan = (span) =>
132134
`[debug:${span.start.line}:${span.start.column}] `;
133135

@@ -150,13 +152,17 @@ async function getSassOptions(
150152
builtMessage += "Deprecation ";
151153
}
152154

153-
if (loggerOptions.span && !loggerOptions.stack) {
154-
builtMessage = formatSpan(loggerOptions.span);
155+
if (loggerOptions.span) {
156+
builtMessage += formatSpan(loggerOptions.span);
155157
}
156158

157159
builtMessage += message;
158160

159-
if (loggerOptions.stack) {
161+
if (loggerOptions.span && loggerOptions.span.context) {
162+
builtMessage += `\n\n${loggerOptions.span.start.line} | ${loggerOptions.span.context}`;
163+
}
164+
165+
if (loggerOptions.stack && loggerOptions.stack !== "null") {
160166
builtMessage += `\n\n${loggerOptions.stack}`;
161167
}
162168

test/__snapshots__/loader.test.js.snap

+208-56
Large diffs are not rendered by default.

test/__snapshots__/warnRuleAsWarning.test.js.snap

+68
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`loader should emit good formatted warning ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = `""`;
4+
5+
exports[`loader should emit good formatted warning ('dart-sass', 'legacy' API, 'sass' syntax): errors 1`] = `[]`;
6+
7+
exports[`loader should emit good formatted warning ('dart-sass', 'legacy' API, 'sass' syntax): logs 1`] = `[]`;
8+
9+
exports[`loader should emit good formatted warning ('dart-sass', 'legacy' API, 'sass' syntax): warnings 1`] = `
10+
[
11+
"ModuleWarning: Module Warning (from ../src/cjs.js):
12+
Warning on line 5, column 3 of file:///test/sass/broken.sass:5:3:
13+
This selector doesn't have any properties and won't be rendered.
14+
15+
5 | asdf
16+
",
17+
]
18+
`;
19+
20+
exports[`loader should emit good formatted warning ('dart-sass', 'modern' API, 'sass' syntax): css 1`] = `""`;
21+
22+
exports[`loader should emit good formatted warning ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = `[]`;
23+
24+
exports[`loader should emit good formatted warning ('dart-sass', 'modern' API, 'sass' syntax): logs 1`] = `[]`;
25+
26+
exports[`loader should emit good formatted warning ('dart-sass', 'modern' API, 'sass' syntax): warnings 1`] = `
27+
[
28+
"ModuleWarning: Module Warning (from ../src/cjs.js):
29+
Warning on line 5, column 3 of file:///test/sass/broken.sass:5:3:
30+
This selector doesn't have any properties and won't be rendered.
31+
32+
5 | asdf
33+
",
34+
]
35+
`;
36+
37+
exports[`loader should emit good formatted warning ('sass-embedded', 'legacy' API, 'sass' syntax): css 1`] = `""`;
38+
39+
exports[`loader should emit good formatted warning ('sass-embedded', 'legacy' API, 'sass' syntax): errors 1`] = `[]`;
40+
41+
exports[`loader should emit good formatted warning ('sass-embedded', 'legacy' API, 'sass' syntax): logs 1`] = `[]`;
42+
43+
exports[`loader should emit good formatted warning ('sass-embedded', 'legacy' API, 'sass' syntax): warnings 1`] = `
44+
[
45+
"ModuleWarning: Module Warning (from ../src/cjs.js):
46+
Warning on line 5, column 3 of file:///test/sass/broken.sass:5:3:
47+
This selector doesn't have any properties and won't be rendered.
48+
49+
5 | asdf
50+
",
51+
]
52+
`;
53+
54+
exports[`loader should emit good formatted warning ('sass-embedded', 'modern' API, 'sass' syntax): css 1`] = `""`;
55+
56+
exports[`loader should emit good formatted warning ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = `[]`;
57+
58+
exports[`loader should emit good formatted warning ('sass-embedded', 'modern' API, 'sass' syntax): logs 1`] = `[]`;
59+
60+
exports[`loader should emit good formatted warning ('sass-embedded', 'modern' API, 'sass' syntax): warnings 1`] = `
61+
[
62+
"ModuleWarning: Module Warning (from ../src/cjs.js):
63+
Warning on line 5, column 3 of file:///test/sass/broken.sass:5:3:
64+
This selector doesn't have any properties and won't be rendered.
65+
66+
5 | asdf
67+
",
68+
]
69+
`;
70+
371
exports[`loader should emit warning by default ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = `
472
"a {
573
color: red;

test/helpers/getWarnings.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import normalizeErrors from "./normalizeErrors";
22

3-
export default (stats) => normalizeErrors(stats.compilation.warnings.sort());
3+
export default (stats, needVerbose) =>
4+
normalizeErrors(stats.compilation.warnings.sort(), needVerbose);

test/helpers/normalizeErrors.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ function removeCWD(str) {
99
cwd = cwd.replace(/\\/g, "/");
1010
}
1111

12-
return str.replace(new RegExp(cwd, "g"), "");
12+
return str.replace(new RegExp(cwd, "g"), "").replace("file:////", "file:///");
1313
}
1414

15-
export default (errors) =>
15+
export default (errors, needVerbose) =>
1616
errors.map((error) =>
17-
removeCWD(error.toString().split("\n").slice(0, 2).join("\n"))
17+
removeCWD(
18+
needVerbose
19+
? error.toString()
20+
: error.toString().split("\n").slice(0, 2).join("\n")
21+
)
1822
);

test/loader.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,7 @@ describe("loader", () => {
18531853

18541854
expect(codeFromBundle.css).toBe(codeFromSass.css);
18551855
expect(codeFromBundle.css).toMatchSnapshot("css");
1856-
expect(getWarnings(stats)).toMatchSnapshot("warnings");
1856+
expect(getWarnings(stats, true)).toMatchSnapshot("warnings");
18571857
expect(getErrors(stats)).toMatchSnapshot("errors");
18581858
});
18591859
}
@@ -2040,7 +2040,7 @@ describe("loader", () => {
20402040

20412041
expect(codeFromBundle.css).toBe(codeFromSass.css);
20422042
expect(codeFromBundle.css).toMatchSnapshot("css");
2043-
expect(getWarnings(stats)).toMatchSnapshot("warnings");
2043+
expect(getWarnings(stats, true)).toMatchSnapshot("warnings");
20442044
expect(getErrors(stats)).toMatchSnapshot("errors");
20452045
});
20462046

test/sass/broken.sass

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
4+
5+
6+
asdf

test/warnRuleAsWarning.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,42 @@ describe("loader", () => {
130130
expect(getErrors(stats)).toMatchSnapshot("errors");
131131
expect(logs).toMatchSnapshot("logs");
132132
});
133+
134+
if (syntax === "sass" && implementationName !== "node-sass") {
135+
it(`should emit good formatted warning ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => {
136+
const testId = getTestId("broken", syntax);
137+
const options = {
138+
implementation,
139+
api,
140+
};
141+
const compiler = getCompiler(testId, { loader: { options } });
142+
const stats = await compile(compiler);
143+
const codeFromBundle = getCodeFromBundle(stats, compiler);
144+
const logs = [];
145+
146+
for (const [name, value] of stats.compilation.logging) {
147+
if (/sass-loader/.test(name)) {
148+
logs.push(
149+
value.map((i) => {
150+
return {
151+
type: i.type,
152+
args: i.args.map((arg) =>
153+
arg
154+
.replace(url.pathToFileURL(__dirname), "file:///<cwd>")
155+
.replace(/\\/g, "/")
156+
),
157+
};
158+
})
159+
);
160+
}
161+
}
162+
163+
expect(codeFromBundle.css).toMatchSnapshot("css");
164+
expect(getWarnings(stats, true)).toMatchSnapshot("warnings");
165+
expect(getErrors(stats)).toMatchSnapshot("errors");
166+
expect(logs).toMatchSnapshot("logs");
167+
});
168+
}
133169
});
134170
});
135171
});

0 commit comments

Comments
 (0)