@utoo/pack-shared 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { formatIssue, handleIssues } from "./issue";
2
+ export { decodeMagicIdentifier } from "./magicIdentifier";
3
+ export { renderStyledStringToErrorAnsi } from "./styledString";
package/esm/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { formatIssue, handleIssues } from "./issue";
2
+ export { decodeMagicIdentifier } from "./magicIdentifier";
3
+ export { renderStyledStringToErrorAnsi } from "./styledString";
package/esm/issue.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ export interface Issue {
2
+ severity: string;
3
+ stage: string;
4
+ filePath: string;
5
+ title: any;
6
+ description?: any;
7
+ detail?: any;
8
+ source?: IssueSource;
9
+ documentationLink: string;
10
+ importTraces: any;
11
+ }
12
+ export interface IssueSource {
13
+ source: Source;
14
+ range?: IssueSourceRange;
15
+ }
16
+ export interface IssueSourceRange {
17
+ start: SourcePos;
18
+ end: SourcePos;
19
+ }
20
+ export interface Source {
21
+ ident: string;
22
+ content?: string;
23
+ }
24
+ export interface SourcePos {
25
+ line: number;
26
+ column: number;
27
+ }
28
+ export declare function formatIssue(issue: Issue): string;
29
+ export declare function handleIssues(issues: Issue[]): void;
package/esm/issue.js ADDED
@@ -0,0 +1,82 @@
1
+ import { codeFrameColumns } from "@babel/code-frame";
2
+ import { renderStyledStringToErrorAnsi } from "./styledString";
3
+ export function formatIssue(issue) {
4
+ const { filePath, title, description, source } = issue;
5
+ let { documentationLink } = issue;
6
+ let formattedTitle = renderStyledStringToErrorAnsi(title).replace(/\n/g, "\n ");
7
+ let formattedFilePath = filePath
8
+ .replace("[project]/", "./")
9
+ .replaceAll("/./", "/")
10
+ .replace("\\\\?\\", "");
11
+ let message = "";
12
+ if (source && source.range) {
13
+ const { start } = source.range;
14
+ message = `${formattedFilePath}:${start.line + 1}:${start.column + 1}\n${formattedTitle}`;
15
+ }
16
+ else if (formattedFilePath) {
17
+ message = `${formattedFilePath}\n${formattedTitle}`;
18
+ }
19
+ else {
20
+ message = formattedTitle;
21
+ }
22
+ message += "\n";
23
+ if ((source === null || source === void 0 ? void 0 : source.range) && source.source.content) {
24
+ const { start, end } = source.range;
25
+ message +=
26
+ codeFrameColumns(source.source.content, {
27
+ start: {
28
+ line: start.line + 1,
29
+ column: start.column + 1,
30
+ },
31
+ end: {
32
+ line: end.line + 1,
33
+ column: end.column + 1,
34
+ },
35
+ }, { forceColor: true }).trim() + "\n\n";
36
+ }
37
+ if (description) {
38
+ message += renderStyledStringToErrorAnsi(description) + "\n\n";
39
+ }
40
+ // TODO: make it possible to enable this for debugging, but not in tests.
41
+ // if (detail) {
42
+ // message += renderStyledStringToErrorAnsi(detail) + '\n\n'
43
+ // }
44
+ // TODO: Include a trace from the issue.
45
+ if (documentationLink) {
46
+ message += documentationLink + "\n\n";
47
+ }
48
+ return message;
49
+ }
50
+ export function handleIssues(issues) {
51
+ const topLevelErrors = [];
52
+ const topLevelWarnings = [];
53
+ for (const issue of issues) {
54
+ if (issue.severity === "error" || issue.severity === "fatal") {
55
+ topLevelErrors.push(formatIssue(issue));
56
+ }
57
+ else if (isRelevantWarning(issue)) {
58
+ topLevelWarnings.push(formatIssue(issue));
59
+ }
60
+ }
61
+ if (topLevelWarnings.length > 0) {
62
+ console.warn(`Utoopack build encountered ${topLevelWarnings.length} warnings:\n${topLevelWarnings.join("\n")}`);
63
+ }
64
+ if (topLevelErrors.length > 0) {
65
+ throw new Error(`Utoopack build failed with ${topLevelErrors.length} errors:\n${topLevelErrors.join("\n")}`);
66
+ }
67
+ }
68
+ function isRelevantWarning(issue) {
69
+ return issue.severity === "warning" && !isNodeModulesIssue(issue);
70
+ }
71
+ function isNodeModulesIssue(issue) {
72
+ if (issue.severity === "warning" && issue.stage === "config") {
73
+ // Override for the externalize issue
74
+ // `Package foo (serverExternalPackages or default list) can't be external`
75
+ if (renderStyledStringToErrorAnsi(issue.title).includes("can't be external")) {
76
+ return false;
77
+ }
78
+ }
79
+ return (issue.severity === "warning" &&
80
+ (issue.filePath.match(/^(?:.*[\\/])?node_modules(?:[\\/].*)?$/) !== null ||
81
+ issue.filePath.includes("@utoo/pack")));
82
+ }
@@ -0,0 +1,2 @@
1
+ export declare function decodeMagicIdentifier(identifier: string): string;
2
+ export declare const MAGIC_IDENTIFIER_REGEX: RegExp;
@@ -0,0 +1,85 @@
1
+ function decodeHex(hexStr) {
2
+ if (hexStr.trim() === "") {
3
+ throw new Error("can't decode empty hex");
4
+ }
5
+ const num = parseInt(hexStr, 16);
6
+ if (isNaN(num)) {
7
+ throw new Error(`invalid hex: \`${hexStr}\``);
8
+ }
9
+ return String.fromCodePoint(num);
10
+ }
11
+ const DECODE_REGEX = /^__TURBOPACK__([a-zA-Z0-9_$]+)__$/;
12
+ export function decodeMagicIdentifier(identifier) {
13
+ const matches = identifier.match(DECODE_REGEX);
14
+ if (!matches) {
15
+ return identifier;
16
+ }
17
+ const inner = matches[1];
18
+ let output = "";
19
+ let mode = 0 /* Mode.Text */;
20
+ let buffer = "";
21
+ for (let i = 0; i < inner.length; i++) {
22
+ const char = inner[i];
23
+ if (mode === 0 /* Mode.Text */) {
24
+ if (char === "_") {
25
+ mode = 1 /* Mode.Underscore */;
26
+ }
27
+ else if (char === "$") {
28
+ mode = 2 /* Mode.Hex */;
29
+ }
30
+ else {
31
+ output += char;
32
+ }
33
+ }
34
+ else if (mode === 1 /* Mode.Underscore */) {
35
+ if (char === "_") {
36
+ output += " ";
37
+ mode = 0 /* Mode.Text */;
38
+ }
39
+ else if (char === "$") {
40
+ output += "_";
41
+ mode = 2 /* Mode.Hex */;
42
+ }
43
+ else {
44
+ output += char;
45
+ mode = 0 /* Mode.Text */;
46
+ }
47
+ }
48
+ else if (mode === 2 /* Mode.Hex */) {
49
+ if (buffer.length === 2) {
50
+ output += decodeHex(buffer);
51
+ buffer = "";
52
+ }
53
+ if (char === "_") {
54
+ if (buffer !== "") {
55
+ throw new Error(`invalid hex: \`${buffer}\``);
56
+ }
57
+ mode = 3 /* Mode.LongHex */;
58
+ }
59
+ else if (char === "$") {
60
+ if (buffer !== "") {
61
+ throw new Error(`invalid hex: \`${buffer}\``);
62
+ }
63
+ mode = 0 /* Mode.Text */;
64
+ }
65
+ else {
66
+ buffer += char;
67
+ }
68
+ }
69
+ else if (mode === 3 /* Mode.LongHex */) {
70
+ if (char === "_") {
71
+ throw new Error(`invalid hex: \`${buffer + char}\``);
72
+ }
73
+ else if (char === "$") {
74
+ output += decodeHex(buffer);
75
+ buffer = "";
76
+ mode = 0 /* Mode.Text */;
77
+ }
78
+ else {
79
+ buffer += char;
80
+ }
81
+ }
82
+ }
83
+ return output;
84
+ }
85
+ export const MAGIC_IDENTIFIER_REGEX = /__TURBOPACK__[a-zA-Z0-9_$]+__/g;
@@ -0,0 +1,17 @@
1
+ export type StyledString = {
2
+ type: "text";
3
+ value: string;
4
+ } | {
5
+ type: "code";
6
+ value: string;
7
+ } | {
8
+ type: "strong";
9
+ value: string;
10
+ } | {
11
+ type: "stack";
12
+ value: StyledString[];
13
+ } | {
14
+ type: "line";
15
+ value: StyledString[];
16
+ };
17
+ export declare function renderStyledStringToErrorAnsi(string: StyledString): string;
@@ -0,0 +1,28 @@
1
+ import { bold, green, magenta, red } from "picocolors";
2
+ import { decodeMagicIdentifier, MAGIC_IDENTIFIER_REGEX, } from "./magicIdentifier";
3
+ export function renderStyledStringToErrorAnsi(string) {
4
+ function decodeMagicIdentifiers(str) {
5
+ return str.replaceAll(MAGIC_IDENTIFIER_REGEX, (ident) => {
6
+ try {
7
+ return magenta(`{${decodeMagicIdentifier(ident)}}`);
8
+ }
9
+ catch (e) {
10
+ return magenta(`{${ident} (decoding failed: ${e})}`);
11
+ }
12
+ });
13
+ }
14
+ switch (string.type) {
15
+ case "text":
16
+ return decodeMagicIdentifiers(string.value);
17
+ case "strong":
18
+ return bold(red(decodeMagicIdentifiers(string.value)));
19
+ case "code":
20
+ return green(decodeMagicIdentifiers(string.value));
21
+ case "line":
22
+ return string.value.map(renderStyledStringToErrorAnsi).join("");
23
+ case "stack":
24
+ return string.value.map(renderStyledStringToErrorAnsi).join("\n");
25
+ default:
26
+ throw new Error("Unknown StyledString type", string);
27
+ }
28
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@utoo/pack-shared",
3
+ "version": "0.0.1",
4
+ "module": "esm/index.js",
5
+ "types": "esm/index.d.ts",
6
+ "files": [
7
+ "esm/*"
8
+ ],
9
+ "scripts": {
10
+ "build": "rm -rf esm && tsc -p ./tsconfig.json"
11
+ },
12
+ "dependencies": {
13
+ "@babel/code-frame": "7.22.5",
14
+ "picocolors": "^1.1.1"
15
+ },
16
+ "devDependencies": {
17
+ "typescript": "^5.8.3",
18
+ "@types/babel__code-frame": "7.0.2"
19
+ },
20
+ "engines": {
21
+ "node": ">= 20"
22
+ },
23
+ "author": "xusd320",
24
+ "license": "MIT",
25
+ "repository": "git@github.com:utooland/utoo.git"
26
+ }