From 5e0308e106feec767b44a5fa29696009f95d3a2a Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Thu, 25 May 2023 23:33:56 +0300 Subject: [PATCH] fix: handling error better (#515) --- src/index.js | 10 +++++++++- src/utils.js | 11 ++--------- test/__snapshots__/implementation.test.js.snap | 13 ++++++++++--- test/fixtures/implementation-error.js | 1 + test/implementation.test.js | 11 +++++++++++ 5 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 test/fixtures/implementation-error.js diff --git a/src/index.js b/src/index.js index 3ec5911..fa3c200 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,15 @@ import LessError from "./LessError"; async function lessLoader(source) { const options = this.getOptions(schema); const callback = this.async(); - const implementation = getLessImplementation(this, options.implementation); + let implementation; + + try { + implementation = getLessImplementation(this, options.implementation); + } catch (error) { + callback(error); + + return; + } if (!implementation) { callback( diff --git a/src/utils.js b/src/utils.js index fe3a306..d742385 100644 --- a/src/utils.js +++ b/src/utils.js @@ -232,15 +232,8 @@ function getLessImplementation(loaderContext, implementation) { if (!implementation || typeof implementation === "string") { const lessImplPkg = implementation || "less"; - try { - // eslint-disable-next-line import/no-dynamic-require, global-require - resolvedImplementation = require(lessImplPkg); - } catch (error) { - loaderContext.emitError(error); - - // eslint-disable-next-line consistent-return - return; - } + // eslint-disable-next-line import/no-dynamic-require, global-require + resolvedImplementation = require(lessImplPkg); } // eslint-disable-next-line consistent-return diff --git a/test/__snapshots__/implementation.test.js.snap b/test/__snapshots__/implementation.test.js.snap index c7a7a4c..9214f35 100644 --- a/test/__snapshots__/implementation.test.js.snap +++ b/test/__snapshots__/implementation.test.js.snap @@ -3,14 +3,21 @@ exports[`"implementation" option should throw error when unresolved package: errors 1`] = ` [ "ModuleBuildError: Module build failed (from \`replaced original path\`): -Error: The Less implementation "unresolved" not found", - "ModuleError: Module Error (from \`replaced original path\`): -(Emitted value instead of an instance of Error) Error: Cannot find module 'unresolved' from 'src/utils.js'", +NonErrorEmittedError: (Emitted value instead of an instance of Error) Error: Cannot find module 'unresolved' from 'src/utils.js'", +] +`; + +exports[`"implementation" option should throw error when unresolved package: errors 2`] = ` +[ + "ModuleBuildError: Module build failed (from \`replaced original path\`): +Error: The Less implementation "/test/fixtures/implementation-error.js" not found", ] `; exports[`"implementation" option should throw error when unresolved package: warnings 1`] = `[]`; +exports[`"implementation" option should throw error when unresolved package: warnings 2`] = `[]`; + exports[`"implementation" option should work when implementation option is string: css 1`] = ` ".box { color: #fe33ac; diff --git a/test/fixtures/implementation-error.js b/test/fixtures/implementation-error.js new file mode 100644 index 0000000..a5d3020 --- /dev/null +++ b/test/fixtures/implementation-error.js @@ -0,0 +1 @@ +module.exports = false; diff --git a/test/implementation.test.js b/test/implementation.test.js index b2a32ce..6206088 100644 --- a/test/implementation.test.js +++ b/test/implementation.test.js @@ -49,4 +49,15 @@ describe('"implementation" option', () => { expect(getWarnings(stats)).toMatchSnapshot("warnings"); expect(getErrors(stats)).toMatchSnapshot("errors"); }); + + it("should throw error when unresolved package", async () => { + const testId = "./basic.less"; + const compiler = getCompiler(testId, { + implementation: require.resolve("./fixtures/implementation-error.js"), + }); + const stats = await compile(compiler); + + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); });