Skip to content

Commit ecbfe95

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

7 files changed

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