| 1 | const ansiHTML = require('ansi-html');
|
|---|
| 2 | const entities = require('html-entities');
|
|---|
| 3 | const theme = require('../theme.js');
|
|---|
| 4 | const utils = require('../utils.js');
|
|---|
| 5 |
|
|---|
| 6 | ansiHTML.setColors(theme);
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * @typedef {Object} CompileErrorTraceProps
|
|---|
| 10 | * @property {string} errorMessage
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * A formatter that turns Webpack compile error messages into highlighted HTML source traces.
|
|---|
| 15 | * @param {Document} document
|
|---|
| 16 | * @param {HTMLElement} root
|
|---|
| 17 | * @param {CompileErrorTraceProps} props
|
|---|
| 18 | * @returns {void}
|
|---|
| 19 | */
|
|---|
| 20 | function CompileErrorTrace(document, root, props) {
|
|---|
| 21 | const errorParts = props.errorMessage.split('\n');
|
|---|
| 22 | if (errorParts.length) {
|
|---|
| 23 | if (errorParts[0]) {
|
|---|
| 24 | errorParts[0] = utils.formatFilename(errorParts[0]);
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | const errorMessage = errorParts.splice(1, 1)[0];
|
|---|
| 28 | if (errorMessage) {
|
|---|
| 29 | // Strip filename from the error message
|
|---|
| 30 | errorParts.unshift(errorMessage.replace(/^(.*:)\s.*:(\s.*)$/, '$1$2'));
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | const stackContainer = document.createElement('pre');
|
|---|
| 35 | stackContainer.innerHTML = entities.decode(
|
|---|
| 36 | ansiHTML(entities.encode(errorParts.join('\n'), { level: 'html5', mode: 'nonAscii' })),
|
|---|
| 37 | { level: 'html5' }
|
|---|
| 38 | );
|
|---|
| 39 | stackContainer.style.fontFamily = [
|
|---|
| 40 | '"Operator Mono SSm"',
|
|---|
| 41 | '"Operator Mono"',
|
|---|
| 42 | '"Fira Code Retina"',
|
|---|
| 43 | '"Fira Code"',
|
|---|
| 44 | '"FiraCode-Retina"',
|
|---|
| 45 | '"Andale Mono"',
|
|---|
| 46 | '"Lucida Console"',
|
|---|
| 47 | 'Menlo',
|
|---|
| 48 | 'Consolas',
|
|---|
| 49 | 'Monaco',
|
|---|
| 50 | 'monospace',
|
|---|
| 51 | ].join(', ');
|
|---|
| 52 | stackContainer.style.margin = '0';
|
|---|
| 53 | stackContainer.style.whiteSpace = 'pre-wrap';
|
|---|
| 54 |
|
|---|
| 55 | root.appendChild(stackContainer);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | module.exports = CompileErrorTrace;
|
|---|