Skip to content

Commit bd0fff1

Browse files
refactor: avoid using babel-code-frame (webpack-contrib#799)
1 parent c598c79 commit bd0fff1

File tree

6 files changed

+256
-423
lines changed

6 files changed

+256
-423
lines changed

lib/CssSyntaxError.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = class CssSyntaxError extends Error {
2+
constructor(error) {
3+
super(error);
4+
5+
const { reason, line, column } = error;
6+
7+
this.name = 'CssSyntaxError';
8+
9+
// Based on https://github.com/postcss/postcss/blob/master/lib/css-syntax-error.es6#L132
10+
// We don't need `plugin` and `file` properties.
11+
this.message = `${this.name}\n\n`;
12+
13+
if (typeof line !== 'undefined') {
14+
this.message += `(${line}:${column}) `;
15+
}
16+
17+
this.message += `${reason}`;
18+
19+
const code = error.showSourceCode();
20+
21+
if (code) {
22+
this.message += `\n\n${code}\n`;
23+
}
24+
25+
// We don't need stack https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
26+
this.stack = false;
27+
}
28+
};

lib/processCss.js

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
const formatCodeFrame = require('babel-code-frame');
65
const Tokenizer = require('css-selector-tokenizer');
76
const postcss = require('postcss');
87
const loaderUtils = require('loader-utils');
@@ -14,6 +13,7 @@ const modulesScope = require('postcss-modules-scope');
1413
const modulesValues = require('postcss-modules-values');
1514
const valueParser = require('postcss-value-parser');
1615

16+
const CssSyntaxError = require('./CssSyntaxError');
1717
const getLocalIdent = require('./getLocalIdent');
1818

1919
const parserPlugin = postcss.plugin('css-loader-parser', (options) => (css) => {
@@ -227,46 +227,9 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
227227
urlItemRegExp: /___CSS_LOADER_URL___([0-9]+)___/,
228228
});
229229
})
230-
.catch((err) => {
231-
if (err.name === 'CssSyntaxError') {
232-
const wrappedError = new CSSLoaderError(
233-
'Syntax Error',
234-
err.reason,
235-
err.line != null && err.column != null
236-
? { line: err.line, column: err.column }
237-
: null,
238-
err.input.source
239-
);
240-
callback(wrappedError);
241-
} else {
242-
callback(err);
243-
}
230+
.catch((error) => {
231+
callback(
232+
error.name === 'CssSyntaxError' ? new CssSyntaxError(error) : error
233+
);
244234
});
245235
};
246-
247-
function formatMessage(message, loc, source) {
248-
let formatted = message;
249-
if (loc) {
250-
formatted = `${formatted} (${loc.line}:${loc.column})`;
251-
}
252-
if (loc && source) {
253-
formatted = `${formatted}\n\n${formatCodeFrame(
254-
source,
255-
loc.line,
256-
loc.column
257-
)}\n`;
258-
}
259-
return formatted;
260-
}
261-
262-
function CSSLoaderError(name, message, loc, source, error) {
263-
Error.call(this);
264-
Error.captureStackTrace(this, CSSLoaderError);
265-
this.name = name;
266-
this.error = error;
267-
this.message = formatMessage(message, loc, source);
268-
this.hideStack = true;
269-
}
270-
271-
CSSLoaderError.prototype = Object.create(Error.prototype);
272-
CSSLoaderError.prototype.constructor = CSSLoaderError;

0 commit comments

Comments
 (0)