Skip to content

Commit

Permalink
fix: handling error better (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 25, 2023
1 parent 8f05f7b commit 5e0308e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/index.js
Expand Up @@ -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(
Expand Down
11 changes: 2 additions & 9 deletions src/utils.js
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions test/__snapshots__/implementation.test.js.snap
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/implementation-error.js
@@ -0,0 +1 @@
module.exports = false;
11 changes: 11 additions & 0 deletions test/implementation.test.js
Expand Up @@ -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");
});
});

0 comments on commit 5e0308e

Please sign in to comment.