Skip to content

Commit 7f4b968

Browse files
committed
Shorten minified error URL
1 parent 55f8843 commit 7f4b968

7 files changed

Lines changed: 83 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx": patch
3+
---
4+
5+
Shorten minified error URL to reduce production bundle size.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ coverage
66
test/babel-tests.js
77
test/typescript/typescript-tests.js
88
dist/
9+
/docs/errors.md
910
**/.idea/*
1011
!/.idea/icon.png
1112
!/.idea/vcs.xml

packages/mobx/src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function die(error: string | keyof typeof errors, ...args: any[]): never
8787
typeof error === "number"
8888
? `[MobX] minified error nr: ${error}${
8989
args.length ? " " + args.map(String).join(",") : ""
90-
}. Find the full error at: https://github.com/mobxjs/mobx/blob/main/packages/mobx/src/errors.ts`
90+
}. See http://mobx.js.org/errors`
9191
: `[MobX] ${error}`
9292
)
9393
}

website/i18n/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
"title": "Decorators",
6565
"sidebar_label": "Decorators {🚀}"
6666
},
67+
"errors": {
68+
"title": "MobX error codes",
69+
"sidebar_label": "Error codes"
70+
},
6771
"faq/migrate-to-6": {
6872
"title": "Migrating from MobX 4/5"
6973
},

website/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"scripts": {
33
"examples": "docusaurus-examples",
4+
"prestart": "node scripts/generate-error-docs.mjs",
45
"start": "docusaurus-start",
6+
"prebuild": "node scripts/generate-error-docs.mjs",
57
"build": "docusaurus-build",
68
"publish-gh-pages": "docusaurus-publish",
79
"write-translations": "docusaurus-write-translations",
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env node
2+
3+
import { readFileSync, writeFileSync } from "node:fs"
4+
import { relative } from "node:path"
5+
import { fileURLToPath } from "node:url"
6+
7+
const repoRoot = fileURLToPath(new URL("../..", import.meta.url))
8+
const errorsPath = fileURLToPath(new URL("../../packages/mobx/src/errors.ts", import.meta.url))
9+
const outputPath = fileURLToPath(new URL("../../docs/errors.md", import.meta.url))
10+
11+
function readNiceErrors() {
12+
const source = readFileSync(errorsPath, "utf8")
13+
const match = source.match(/const niceErrors = ([\s\S]*?) as const/)
14+
15+
if (!match) {
16+
throw new Error(`Could not find niceErrors in ${errorsPath}`)
17+
}
18+
19+
const objectLiteral = match[1].replace(": PropertyKey", "")
20+
return eval(`(${objectLiteral})`)
21+
}
22+
23+
function getParamNames(fn) {
24+
const match = fn.toString().match(/^[^(]*\(([^)]*)\)/)
25+
return match ? match[1].split(",").map(name => name.trim()).filter(Boolean) : []
26+
}
27+
28+
function renderMessage(message) {
29+
if (typeof message !== "function") {
30+
return message
31+
}
32+
33+
const args = getParamNames(message).map(name => `{${name}}`)
34+
return message(...args).replace("String", args[0])
35+
}
36+
37+
function escapeMarkdownTableCell(value) {
38+
return String(value).replace(/\r?\n/g, "<br />").replace(/\|/g, "\\|")
39+
}
40+
41+
function renderDocs(errors) {
42+
const rows = Object.keys(errors)
43+
.sort((left, right) => Number(left) - Number(right))
44+
.map(code => `| ${code} | ${escapeMarkdownTableCell(renderMessage(errors[code]))} |`)
45+
.join("\n")
46+
47+
return `---
48+
title: MobX error codes
49+
sidebar_label: Error codes
50+
hide_title: true
51+
custom_edit_url: https://github.com/mobxjs/mobx/edit/main/packages/mobx/src/errors.ts
52+
---
53+
54+
<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CEBD4KQ7&placement=mobxjsorg" id="_carbonads_js"></script>
55+
56+
# MobX error codes
57+
58+
In development builds, MobX throws full error messages. Production builds replace known messages with short numeric codes to keep the published bundle small.
59+
60+
| Code | Message |
61+
| ---- | ------- |
62+
${rows}
63+
`
64+
}
65+
66+
const docs = renderDocs(readNiceErrors())
67+
writeFileSync(outputPath, docs)
68+
69+
console.log(`Generated ${relative(repoRoot, outputPath)} from ${relative(repoRoot, errorsPath)}`)

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"understanding-reactivity",
2323
"subclassing",
2424
"analyzing-reactivity",
25+
"errors",
2526
"computeds-with-args",
2627
"mobx-utils",
2728
"custom-observables",

0 commit comments

Comments
 (0)