1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path_1 = require("path");
|
4 | const diagnostics_1 = require("./diagnostics");
|
5 | const defaultCodeCategories = {
|
6 | EMPTY_BUNDLE: diagnostics_1.DiagnosticCategory.Error,
|
7 | MISSING_EXPORT: diagnostics_1.DiagnosticCategory.Error,
|
8 | NAMESPACE_CONFLICT: diagnostics_1.DiagnosticCategory.Error,
|
9 | THIS_IS_UNDEFINED: false,
|
10 | UNRESOLVED_IMPORT: diagnostics_1.DiagnosticCategory.Error,
|
11 | };
|
12 | const messageFormatter = {
|
13 | UNRESOLVED_IMPORT: (w) => `${w.source} is imported by ${w.importer}, but could not be resolved`,
|
14 | };
|
15 | function relativeId(id) {
|
16 | if (typeof process === 'undefined' ||
|
17 | !/^(?:\/|(?:[A-Za-z]:)?[\\|/])/.test(id)) {
|
18 | return id;
|
19 | }
|
20 | return path_1.relative(process.cwd(), id);
|
21 | }
|
22 | function defaultFormatter(w) {
|
23 | return w.loc && w.loc.file
|
24 | ? `${relativeId(w.loc.file)} (${w.loc.line}:${w.loc.column}) ${w.message}`
|
25 | : w.message;
|
26 | }
|
27 | const rollupWarningToDiagnostic = (codeCategories) => (warning) => {
|
28 | if (typeof warning === 'string') {
|
29 | return {
|
30 | category: diagnostics_1.DiagnosticCategory.Warning,
|
31 | messageText: warning,
|
32 | };
|
33 | }
|
34 | const { code } = warning;
|
35 | let category = diagnostics_1.DiagnosticCategory.Warning;
|
36 | if (code) {
|
37 | const codeCategory = codeCategories[code];
|
38 | if (codeCategory === false) {
|
39 | return;
|
40 | }
|
41 | if (codeCategory !== undefined)
|
42 | category = codeCategory;
|
43 | }
|
44 | let formatter = defaultFormatter;
|
45 | if (code && messageFormatter[code])
|
46 | formatter = messageFormatter[code];
|
47 | let messageText = formatter(warning);
|
48 | if (warning.frame) {
|
49 | const context = {
|
50 | messageText: warning.frame,
|
51 | category: diagnostics_1.DiagnosticCategory.Message,
|
52 | };
|
53 | messageText = [{ messageText, category }, context];
|
54 | }
|
55 | return { messageText, category };
|
56 | };
|
57 | function rollupWarningHandler({ codeCategories = {}, onDiagnostic = diagnostics_1.logDiagnosticToConsole, }) {
|
58 | const toDiagnostic = rollupWarningToDiagnostic(Object.assign(Object.assign({}, defaultCodeCategories), codeCategories));
|
59 | return (w) => {
|
60 | const diagnostic = toDiagnostic(w);
|
61 | if (!diagnostic)
|
62 | return;
|
63 | onDiagnostic(diagnostic);
|
64 | if (diagnostic.category === diagnostics_1.DiagnosticCategory.Error) {
|
65 | throw new Error('Compile failed.');
|
66 | }
|
67 | };
|
68 | }
|
69 | exports.default = rollupWarningHandler;
|