Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = false;
11 changes: 11 additions & 0 deletions test/implementation.test.js
Original file line number Diff line number Diff line change
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");
});
});