Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Apr 24, 2020
1 parent 3931470 commit 1aff534
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Expand Up @@ -55,7 +55,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10.x, 12.x, 13.x]
node-version: [10.x, 12.x, 14.x]
webpack-version: [latest, next]

runs-on: ${{ matrix.os }}
Expand Down
33 changes: 33 additions & 0 deletions src/LessError.js
@@ -0,0 +1,33 @@
class LessError extends Error {
constructor(error) {
super();

this.message = [
'\n',
...LessError.getFileExcerptIfPossible(error),
error.message.charAt(0).toUpperCase() + error.message.slice(1),
` Error in ${error.filename} (line ${error.line}, column ${error.column})`,
].join('\n');

this.hideStack = true;
}

static getFileExcerptIfPossible(lessErr) {
if (lessErr.extract === 'undefined') {
return [];
}

const excerpt = lessErr.extract.slice(0, 2);
const column = Math.max(lessErr.column - 1, 0);

if (typeof excerpt[0] === 'undefined') {
excerpt.shift();
}

excerpt.push(`${new Array(column).join(' ')}^`);

return excerpt;
}
}

export default LessError;
9 changes: 5 additions & 4 deletions src/createWebpackLessPlugin.js
Expand Up @@ -98,15 +98,16 @@ function createWebpackLessPlugin(loaderContext) {
result = await super.loadFile(filename, ...args);
} catch (error) {
if (error.type !== 'File' && error.type !== 'Next') {
loaderContext.emitError(error);

return Promise.reject(error);
}

try {
result = await this.resolveFilename(filename, ...args);
} catch (e) {
loaderContext.emitError(e);
} catch (webpackResolveError) {
error.message =
`Less resolver error:\n${error.message}\n\n` +
`Webpack resolver error details:\n${webpackResolveError.details}\n\n` +
`Webpack resolver error missing:\n${webpackResolveError.missing}\n\n`;

return Promise.reject(error);
}
Expand Down
61 changes: 0 additions & 61 deletions src/formatLessError.js

This file was deleted.

28 changes: 1 addition & 27 deletions src/getLessOptions.js
Expand Up @@ -7,30 +7,9 @@ import createWebpackLessPlugin from './createWebpackLessPlugin';
*
* @param {object} loaderContext
* @param {object} loaderOptions
* @param {string} content
* @returns {Object}
*/
function getLessOptions(loaderContext, loaderOptions, content) {
function prependData(target, addedData) {
if (!addedData) {
return target;
}

return typeof addedData === 'function'
? `${addedData(loaderContext)}\n${target}`
: `${addedData}\n${target}`;
}

function appendData(target, addedData) {
if (!addedData) {
return target;
}

return typeof addedData === 'function'
? `${target}\n${addedData(loaderContext)}`
: `${target}\n${addedData}`;
}

function getLessOptions(loaderContext, loaderOptions) {
const options = clone(
loaderOptions.lessOptions
? typeof loaderOptions.lessOptions === 'function'
Expand All @@ -39,17 +18,12 @@ function getLessOptions(loaderContext, loaderOptions, content) {
: {}
);

let data = content;
data = prependData(data, loaderOptions.prependData);
data = appendData(data, loaderOptions.appendData);

const lessOptions = {
plugins: [],
relativeUrls: true,
// We need to set the filename because otherwise our WebpackFileManager will receive an undefined path for the entry
filename: loaderContext.resourcePath,
...options,
data,
};

lessOptions.plugins.push(createWebpackLessPlugin(loaderContext));
Expand Down
39 changes: 29 additions & 10 deletions src/index.js
Expand Up @@ -5,8 +5,7 @@ import validateOptions from 'schema-utils';

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

function lessLoader(source) {
const options = getOptions(this);
Expand All @@ -17,28 +16,48 @@ function lessLoader(source) {
});

const callback = this.async();
const lessOptions = getLessOptions(this, options, source);
const lessOptions = getLessOptions(this, options);

let data = source;
data = prependData(data, options.prependData);
data = appendData(data, options.appendData);

less
.render(lessOptions.data, lessOptions)
.render(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
);
callback(null, css, typeof map === 'string' ? JSON.parse(map) : map);
})
.catch((lessError) => {
if (lessError.filename) {
this.addDependency(lessError.filename);
}

callback(formatLessError(lessError));
callback(new LessError(lessError));
});

function prependData(target, addedData) {
if (!addedData) {
return target;
}

return typeof addedData === 'function'
? `${addedData(this)}\n${target}`
: `${addedData}\n${target}`;
}

function appendData(target, addedData) {
if (!addedData) {
return target;
}

return typeof addedData === 'function'
? `${target}\n${addedData(this)}`
: `${target}\n${addedData}`;
}
}

export default lessLoader;
17 changes: 0 additions & 17 deletions src/removeSourceMappingUrl.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -166,8 +166,6 @@ exports[`loader should provide a useful error message if the import could not be
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
",
"ModuleError: Module Error (from \`replaced original path\`):
Can't resolve 'not-existing.less' in '/test/fixtures'",
]
`;
Expand Down

0 comments on commit 1aff534

Please sign in to comment.