From a1cf249dcedf90f9b968b0b19b2bb4dc74f99931 Mon Sep 17 00:00:00 2001 From: Alexey Lavinsky Date: Wed, 22 Apr 2020 17:09:40 +0300 Subject: [PATCH] refactor: moving processResult in index (#344) --- src/formatLessError.js | 6 ++---- src/index.js | 23 +++++++++++++++++++++-- src/processResult.js | 35 ----------------------------------- 3 files changed, 23 insertions(+), 41 deletions(-) delete mode 100644 src/processResult.js diff --git a/src/formatLessError.js b/src/formatLessError.js index adbe9785..bbfd832c 100644 --- a/src/formatLessError.js +++ b/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. @@ -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 */ diff --git a/src/index.js b/src/index.js index 3215240e..8b0f87b2 100644 --- a/src/index.js +++ b/src/index.js @@ -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)); @@ -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; diff --git a/src/processResult.js b/src/processResult.js deleted file mode 100644 index fff9074d..00000000 --- a/src/processResult.js +++ /dev/null @@ -1,35 +0,0 @@ -const removeSourceMappingUrl = require('./removeSourceMappingUrl'); -const formatLessError = require('./formatLessError'); - -/** - * Removes the sourceMappingURL from the generated CSS, parses the source map and calls the next loader. - * - * @param {loaderContext} loaderContext - * @param {Promise} resultPromise - * @param {Function} callback - */ -function processResult(loaderContext, resultPromise, callback) { - resultPromise - .then( - ({ css, map, imports }) => { - imports.forEach(loaderContext.addDependency, loaderContext); - return { - // Removing the sourceMappingURL comment. - // See removeSourceMappingUrl.js for the reasoning behind this. - css: removeSourceMappingUrl(css), - map: typeof map === 'string' ? JSON.parse(map) : map, - }; - }, - (lessError) => { - if (lessError.filename) { - loaderContext.addDependency(lessError.filename); - } - throw formatLessError(lessError); - } - ) - .then(({ css, map }) => { - callback(null, css, map); - }, callback); -} - -module.exports = processResult;