Skip to content

Commit

Permalink
refactor: moving processResult in index (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Apr 22, 2020
1 parent bffbc5e commit a1cf249
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
6 changes: 2 additions & 4 deletions src/formatLessError.js
@@ -1,5 +1,3 @@
const os = require('os');

/**
* Tries to get an excerpt of the file where the error happened.
* Uses err.line and err.column.
Expand Down Expand Up @@ -51,11 +49,11 @@ function formatLessError(err) {
err.hideStack = true;

err.message = [
os.EOL,
'\n',
...getFileExcerptIfPossible(err),
msg.charAt(0).toUpperCase() + msg.slice(1),
` in ${err.filename} (line ${err.line}, column ${err.column})`,
].join(os.EOL);
].join('\n');

return err;
} /* eslint-enable no-param-reassign */
Expand Down
23 changes: 21 additions & 2 deletions src/index.js
Expand Up @@ -6,8 +6,9 @@ import { getOptions } from 'loader-utils';
import validateOptions from 'schema-utils';

import schema from './options.json';
import processResult from './processResult';
import getLessOptions from './getLessOptions';
import removeSourceMappingUrl from './removeSourceMappingUrl';
import formatLessError from './formatLessError';

const render = promisify(less.render.bind(less));

Expand All @@ -22,7 +23,25 @@ function lessLoader(source) {
const callback = this.async();
const lessOptions = getLessOptions(this, options, source);

processResult(this, render(lessOptions.data, lessOptions), callback);
render(lessOptions.data, lessOptions)
.then(({ css, map, imports }) => {
imports.forEach(this.addDependency, this);

// Removing the sourceMappingURL comment.
// See removeSourceMappingUrl.js for the reasoning behind this.
callback(
null,
removeSourceMappingUrl(css),
typeof map === 'string' ? JSON.parse(map) : map
);
})
.catch((lessError) => {
if (lessError.filename) {
this.addDependency(lessError.filename);
}

callback(formatLessError(lessError));
});
}

export default lessLoader;
35 changes: 0 additions & 35 deletions src/processResult.js

This file was deleted.

0 comments on commit a1cf249

Please sign in to comment.