Skip to content

Commit 1aff534

Browse files
refactor: code
1 parent 3931470 commit 1aff534

File tree

8 files changed

+69
-122
lines changed

8 files changed

+69
-122
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
strategy:
5656
matrix:
5757
os: [ubuntu-latest, windows-latest, macos-latest]
58-
node-version: [10.x, 12.x, 13.x]
58+
node-version: [10.x, 12.x, 14.x]
5959
webpack-version: [latest, next]
6060

6161
runs-on: ${{ matrix.os }}

src/LessError.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class LessError extends Error {
2+
constructor(error) {
3+
super();
4+
5+
this.message = [
6+
'\n',
7+
...LessError.getFileExcerptIfPossible(error),
8+
error.message.charAt(0).toUpperCase() + error.message.slice(1),
9+
` Error in ${error.filename} (line ${error.line}, column ${error.column})`,
10+
].join('\n');
11+
12+
this.hideStack = true;
13+
}
14+
15+
static getFileExcerptIfPossible(lessErr) {
16+
if (lessErr.extract === 'undefined') {
17+
return [];
18+
}
19+
20+
const excerpt = lessErr.extract.slice(0, 2);
21+
const column = Math.max(lessErr.column - 1, 0);
22+
23+
if (typeof excerpt[0] === 'undefined') {
24+
excerpt.shift();
25+
}
26+
27+
excerpt.push(`${new Array(column).join(' ')}^`);
28+
29+
return excerpt;
30+
}
31+
}
32+
33+
export default LessError;

src/createWebpackLessPlugin.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,16 @@ function createWebpackLessPlugin(loaderContext) {
9898
result = await super.loadFile(filename, ...args);
9999
} catch (error) {
100100
if (error.type !== 'File' && error.type !== 'Next') {
101-
loaderContext.emitError(error);
102-
103101
return Promise.reject(error);
104102
}
105103

106104
try {
107105
result = await this.resolveFilename(filename, ...args);
108-
} catch (e) {
109-
loaderContext.emitError(e);
106+
} catch (webpackResolveError) {
107+
error.message =
108+
`Less resolver error:\n${error.message}\n\n` +
109+
`Webpack resolver error details:\n${webpackResolveError.details}\n\n` +
110+
`Webpack resolver error missing:\n${webpackResolveError.missing}\n\n`;
110111

111112
return Promise.reject(error);
112113
}

src/formatLessError.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/getLessOptions.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,9 @@ import createWebpackLessPlugin from './createWebpackLessPlugin';
77
*
88
* @param {object} loaderContext
99
* @param {object} loaderOptions
10-
* @param {string} content
1110
* @returns {Object}
1211
*/
13-
function getLessOptions(loaderContext, loaderOptions, content) {
14-
function prependData(target, addedData) {
15-
if (!addedData) {
16-
return target;
17-
}
18-
19-
return typeof addedData === 'function'
20-
? `${addedData(loaderContext)}\n${target}`
21-
: `${addedData}\n${target}`;
22-
}
23-
24-
function appendData(target, addedData) {
25-
if (!addedData) {
26-
return target;
27-
}
28-
29-
return typeof addedData === 'function'
30-
? `${target}\n${addedData(loaderContext)}`
31-
: `${target}\n${addedData}`;
32-
}
33-
12+
function getLessOptions(loaderContext, loaderOptions) {
3413
const options = clone(
3514
loaderOptions.lessOptions
3615
? typeof loaderOptions.lessOptions === 'function'
@@ -39,17 +18,12 @@ function getLessOptions(loaderContext, loaderOptions, content) {
3918
: {}
4019
);
4120

42-
let data = content;
43-
data = prependData(data, loaderOptions.prependData);
44-
data = appendData(data, loaderOptions.appendData);
45-
4621
const lessOptions = {
4722
plugins: [],
4823
relativeUrls: true,
4924
// We need to set the filename because otherwise our WebpackFileManager will receive an undefined path for the entry
5025
filename: loaderContext.resourcePath,
5126
...options,
52-
data,
5327
};
5428

5529
lessOptions.plugins.push(createWebpackLessPlugin(loaderContext));

src/index.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import validateOptions from 'schema-utils';
55

66
import schema from './options.json';
77
import getLessOptions from './getLessOptions';
8-
import removeSourceMappingUrl from './removeSourceMappingUrl';
9-
import formatLessError from './formatLessError';
8+
import LessError from './LessError';
109

1110
function lessLoader(source) {
1211
const options = getOptions(this);
@@ -17,28 +16,48 @@ function lessLoader(source) {
1716
});
1817

1918
const callback = this.async();
20-
const lessOptions = getLessOptions(this, options, source);
19+
const lessOptions = getLessOptions(this, options);
20+
21+
let data = source;
22+
data = prependData(data, options.prependData);
23+
data = appendData(data, options.appendData);
2124

2225
less
23-
.render(lessOptions.data, lessOptions)
26+
.render(data, lessOptions)
2427
.then(({ css, map, imports }) => {
2528
imports.forEach(this.addDependency, this);
2629

2730
// Removing the sourceMappingURL comment.
2831
// See removeSourceMappingUrl.js for the reasoning behind this.
29-
callback(
30-
null,
31-
removeSourceMappingUrl(css),
32-
typeof map === 'string' ? JSON.parse(map) : map
33-
);
32+
callback(null, css, typeof map === 'string' ? JSON.parse(map) : map);
3433
})
3534
.catch((lessError) => {
3635
if (lessError.filename) {
3736
this.addDependency(lessError.filename);
3837
}
3938

40-
callback(formatLessError(lessError));
39+
callback(new LessError(lessError));
4140
});
41+
42+
function prependData(target, addedData) {
43+
if (!addedData) {
44+
return target;
45+
}
46+
47+
return typeof addedData === 'function'
48+
? `${addedData(this)}\n${target}`
49+
: `${addedData}\n${target}`;
50+
}
51+
52+
function appendData(target, addedData) {
53+
if (!addedData) {
54+
return target;
55+
}
56+
57+
return typeof addedData === 'function'
58+
? `${target}\n${addedData(this)}`
59+
: `${target}\n${addedData}`;
60+
}
4261
}
4362

4463
export default lessLoader;

src/removeSourceMappingUrl.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/__snapshots__/loader.test.js.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ exports[`loader should provide a useful error message if the import could not be
166166
Array [
167167
"ModuleBuildError: Module build failed (from \`replaced original path\`):
168168
",
169-
"ModuleError: Module Error (from \`replaced original path\`):
170-
Can't resolve 'not-existing.less' in '/test/fixtures'",
171169
]
172170
`;
173171

0 commit comments

Comments
 (0)