From ed6f3136f067e4c863077cb0d6c89c7ea8638bf8 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Sun, 28 May 2023 04:43:02 +0300 Subject: [PATCH] fix: warnings and errors serialization (#1142) --- src/SassError.js | 32 ---------- src/SassWarning.js | 17 ----- src/index.js | 4 +- src/utils.js | 29 +++++++-- test/__snapshots__/loader.test.js.snap | 88 +++++++++++++------------- 5 files changed, 70 insertions(+), 100 deletions(-) delete mode 100644 src/SassError.js delete mode 100644 src/SassWarning.js diff --git a/src/SassError.js b/src/SassError.js deleted file mode 100644 index c0a8cc57..00000000 --- a/src/SassError.js +++ /dev/null @@ -1,32 +0,0 @@ -class SassError extends Error { - constructor(sassError) { - super(); - - this.name = "SassError"; - - // Instruct webpack to hide the JS stack from the console. - // Usually you're only interested in the SASS error in this case. - this.hideStack = true; - - if ( - typeof sassError.line !== "undefined" || - typeof sassError.column !== "undefined" - ) { - this.loc = { - line: sassError.line, - column: sassError.column, - }; - } - - // Keep original error if `sassError.formatted` is unavailable - this.message = `${this.name}: ${ - typeof sassError.message !== "undefined" ? sassError.message : sassError - }`; - - if (sassError.formatted) { - this.message = sassError.formatted.replace(/^Error: /, ""); - } - } -} - -export default SassError; diff --git a/src/SassWarning.js b/src/SassWarning.js deleted file mode 100644 index bd84bf82..00000000 --- a/src/SassWarning.js +++ /dev/null @@ -1,17 +0,0 @@ -class SassWarning extends Error { - constructor(warning, options) { - super(warning); - - this.name = "SassWarning"; - this.hideStack = true; - - if (options.span) { - this.loc = { - line: options.span.start.line, - column: options.span.start.column, - }; - } - } -} - -export default SassWarning; diff --git a/src/index.js b/src/index.js index 0badec73..1541518f 100644 --- a/src/index.js +++ b/src/index.js @@ -9,8 +9,8 @@ import { getModernWebpackImporter, getCompileFn, normalizeSourceMap, + errorFactory, } from "./utils"; -import SassError from "./SassError"; /** * The sass-loader makes node-sass and dart-sass available to webpack modules. @@ -87,7 +87,7 @@ async function loader(content) { this.addDependency(path.normalize(error.file)); } - callback(new SassError(error)); + callback(errorFactory(error)); return; } diff --git a/src/utils.js b/src/utils.js index fff4c855..d29080cd 100644 --- a/src/utils.js +++ b/src/utils.js @@ -4,8 +4,6 @@ import path from "path"; import { klona } from "klona/full"; import async from "neo-async"; -import SassWarning from "./SassWarning"; - function getDefaultSassImplementation() { let sassImplPkg = "sass"; @@ -169,9 +167,12 @@ async function getSassOptions( } if (needEmitWarning) { - loaderContext.emitWarning( - new SassWarning(builtMessage, loggerOptions) - ); + const warning = new Error(builtMessage); + + warning.name = "SassWarning"; + warning.stack = null; + + loaderContext.emitWarning(warning); } else { logger.warn(builtMessage); } @@ -781,6 +782,23 @@ function normalizeSourceMap(map, rootContext) { return newMap; } +function errorFactory(error) { + let message; + + if (error.formatted) { + message = error.formatted.replace(/^Error: /, ""); + } else { + // Keep original error if `sassError.formatted` is unavailable + ({ message } = error); + } + + const obj = new Error(message, { cause: error }); + + obj.stack = null; + + return obj; +} + export { getSassImplementation, getSassOptions, @@ -790,4 +808,5 @@ export { getCompileFn, normalizeSourceMap, isSupportedFibers, + errorFactory, }; diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index dd810724..b61094f7 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -476,7 +476,7 @@ exports[`loader should not use .import.sass files ('dart-sass', 'legacy' API, 's exports[`loader should not use .import.sass files ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -494,7 +494,7 @@ exports[`loader should not use .import.sass files ('sass-embedded', 'legacy' API exports[`loader should not use .import.sass files ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `; @@ -512,7 +512,7 @@ exports[`loader should not use .import.scss files ('dart-sass', 'legacy' API, 's exports[`loader should not use .import.scss files ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -530,7 +530,7 @@ exports[`loader should not use .import.scss files ('sass-embedded', 'legacy' API exports[`loader should not use .import.scss files ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `; @@ -557,7 +557,7 @@ exports[`loader should output an understandable error ('dart-sass', 'legacy' API exports[`loader should output an understandable error ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Expected newline.", +Expected newline.", ] `; @@ -566,7 +566,7 @@ exports[`loader should output an understandable error ('dart-sass', 'modern' API exports[`loader should output an understandable error ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: expected "{".", +expected "{".", ] `; @@ -611,7 +611,7 @@ exports[`loader should output an understandable error ('sass-embedded', 'legacy' exports[`loader should output an understandable error ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Expected newline.", +Error: Expected newline.", ] `; @@ -620,7 +620,7 @@ exports[`loader should output an understandable error ('sass-embedded', 'modern' exports[`loader should output an understandable error ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: expected "{".", +Error: expected "{".", ] `; @@ -647,7 +647,7 @@ exports[`loader should output an understandable error when a file could not be f exports[`loader should output an understandable error when a file could not be found ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -656,7 +656,7 @@ exports[`loader should output an understandable error when a file could not be f exports[`loader should output an understandable error when a file could not be found ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -701,7 +701,7 @@ exports[`loader should output an understandable error when a file could not be f exports[`loader should output an understandable error when a file could not be found ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `; @@ -710,7 +710,7 @@ exports[`loader should output an understandable error when a file could not be f exports[`loader should output an understandable error when a file could not be found ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `; @@ -737,7 +737,7 @@ exports[`loader should output an understandable error when a file could not be f exports[`loader should output an understandable error when a file could not be found using "@use" rule ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -746,7 +746,7 @@ exports[`loader should output an understandable error when a file could not be f exports[`loader should output an understandable error when a file could not be found using "@use" rule ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -773,7 +773,7 @@ exports[`loader should output an understandable error when a file could not be f exports[`loader should output an understandable error when a file could not be found using "@use" rule ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `; @@ -782,7 +782,7 @@ exports[`loader should output an understandable error when a file could not be f exports[`loader should output an understandable error when a file could not be found using "@use" rule ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `; @@ -809,7 +809,7 @@ exports[`loader should output an understandable error when the problem in "@impo exports[`loader should output an understandable error when the problem in "@import" ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Expected newline.", +Expected newline.", ] `; @@ -818,7 +818,7 @@ exports[`loader should output an understandable error when the problem in "@impo exports[`loader should output an understandable error when the problem in "@import" ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: expected "{".", +expected "{".", ] `; @@ -863,7 +863,7 @@ exports[`loader should output an understandable error when the problem in "@impo exports[`loader should output an understandable error when the problem in "@import" ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Expected newline.", +Error: Expected newline.", ] `; @@ -872,7 +872,7 @@ exports[`loader should output an understandable error when the problem in "@impo exports[`loader should output an understandable error when the problem in "@import" ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: expected "{".", +Error: expected "{".", ] `; @@ -899,7 +899,7 @@ exports[`loader should output an understandable error with a problem in "@use" ( exports[`loader should output an understandable error with a problem in "@use" ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Expected newline.", +Expected newline.", ] `; @@ -908,7 +908,7 @@ exports[`loader should output an understandable error with a problem in "@use" ( exports[`loader should output an understandable error with a problem in "@use" ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: expected "{".", +expected "{".", ] `; @@ -935,7 +935,7 @@ exports[`loader should output an understandable error with a problem in "@use" ( exports[`loader should output an understandable error with a problem in "@use" ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Expected newline.", +Error: Expected newline.", ] `; @@ -944,7 +944,7 @@ exports[`loader should output an understandable error with a problem in "@use" ( exports[`loader should output an understandable error with a problem in "@use" ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: expected "{".", +Error: expected "{".", ] `; @@ -2409,7 +2409,7 @@ exports[`loader should throw an error on ambiguous import (only on "dart-sass") exports[`loader should throw an error on ambiguous import (only on "dart-sass") ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: It's not clear which file to import. Found:", +It's not clear which file to import. Found:", ] `; @@ -2418,7 +2418,7 @@ exports[`loader should throw an error on ambiguous import (only on "dart-sass") exports[`loader should throw an error on ambiguous import (only on "dart-sass") ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: It's not clear which file to import. Found:", +It's not clear which file to import. Found:", ] `; @@ -2453,7 +2453,7 @@ exports[`loader should throw an error on ambiguous import (only on "dart-sass") exports[`loader should throw an error on ambiguous import (only on "dart-sass") ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: It's not clear which file to import. Found:", +Error: It's not clear which file to import. Found:", ] `; @@ -2462,7 +2462,7 @@ exports[`loader should throw an error on ambiguous import (only on "dart-sass") exports[`loader should throw an error on ambiguous import (only on "dart-sass") ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: It's not clear which file to import. Found:", +Error: It's not clear which file to import. Found:", ] `; @@ -2489,7 +2489,7 @@ exports[`loader should throw an error on circular import ('dart-sass', 'legacy' exports[`loader should throw an error on circular import ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: This file is already being loaded.", +This file is already being loaded.", ] `; @@ -2498,7 +2498,7 @@ exports[`loader should throw an error on circular import ('dart-sass', 'modern' exports[`loader should throw an error on circular import ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: This file is already being loaded.", +This file is already being loaded.", ] `; @@ -2543,7 +2543,7 @@ exports[`loader should throw an error on circular import ('sass-embedded', 'lega exports[`loader should throw an error on circular import ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: This file is already being loaded.", +Error: This file is already being loaded.", ] `; @@ -2552,7 +2552,7 @@ exports[`loader should throw an error on circular import ('sass-embedded', 'mode exports[`loader should throw an error on circular import ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: This file is already being loaded.", +Error: This file is already being loaded.", ] `; @@ -2579,7 +2579,7 @@ exports[`loader should throw an error on circular use ('dart-sass', 'legacy' API exports[`loader should throw an error on circular use ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Module loop: this module is already being loaded.", +Module loop: this module is already being loaded.", ] `; @@ -2588,7 +2588,7 @@ exports[`loader should throw an error on circular use ('dart-sass', 'modern' API exports[`loader should throw an error on circular use ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: This file is already being loaded.", +This file is already being loaded.", ] `; @@ -2615,7 +2615,7 @@ exports[`loader should throw an error on circular use ('sass-embedded', 'legacy' exports[`loader should throw an error on circular use ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Module loop: this module is already being loaded.", +Error: Module loop: this module is already being loaded.", ] `; @@ -2624,7 +2624,7 @@ exports[`loader should throw an error on circular use ('sass-embedded', 'modern' exports[`loader should throw an error on circular use ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: This file is already being loaded.", +Error: This file is already being loaded.", ] `; @@ -2651,7 +2651,7 @@ exports[`loader should throw an error with a explicit file and a file does not e exports[`loader should throw an error with a explicit file and a file does not exist ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -2660,7 +2660,7 @@ exports[`loader should throw an error with a explicit file and a file does not e exports[`loader should throw an error with a explicit file and a file does not exist ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -2705,7 +2705,7 @@ exports[`loader should throw an error with a explicit file and a file does not e exports[`loader should throw an error with a explicit file and a file does not exist ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `; @@ -2714,7 +2714,7 @@ exports[`loader should throw an error with a explicit file and a file does not e exports[`loader should throw an error with a explicit file and a file does not exist ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `; @@ -2741,7 +2741,7 @@ exports[`loader should throw an error with a explicit file and a file does not e exports[`loader should throw an error with a explicit file and a file does not exist using "@use" rule ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -2750,7 +2750,7 @@ exports[`loader should throw an error with a explicit file and a file does not e exports[`loader should throw an error with a explicit file and a file does not exist using "@use" rule ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Can't find stylesheet to import.", +Can't find stylesheet to import.", ] `; @@ -2777,7 +2777,7 @@ exports[`loader should throw an error with a explicit file and a file does not e exports[`loader should throw an error with a explicit file and a file does not exist using "@use" rule ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `; @@ -2786,7 +2786,7 @@ exports[`loader should throw an error with a explicit file and a file does not e exports[`loader should throw an error with a explicit file and a file does not exist using "@use" rule ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = ` [ "ModuleBuildError: Module build failed (from ../src/cjs.js): -SassError: Error: Can't find stylesheet to import.", +Error: Can't find stylesheet to import.", ] `;