Skip to content

Commit 979d267

Browse files
authored
Shorten minified error URL to reduce production bundle size (mobxjs#4677)
* Shorten minified error URL * simpler check * simpler code * wording
1 parent 55f8843 commit 979d267

8 files changed

Lines changed: 73 additions & 3 deletions

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const niceErrors = {
1+
export const niceErrors = {
22
0: `Invalid value for configuration 'enforceActions', expected 'never', 'always' or 'observed'`,
33
1(annotationType, key: PropertyKey) {
44
return `Cannot apply '${annotationType}' to '${key.toString()}': Field not found.`
@@ -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
}

packages/mobx/src/internal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ With this file that will still happen,
66
but at least in this file we can magically reorder the imports with trial and error until the build succeeds again.
77
*/
88
export * from "./utils/global"
9-
export * from "./errors"
9+
export { die } from "./errors"
1010
export * from "./utils/utils"
1111
export * from "./api/decorators"
1212
export * from "./core/atom"

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.js",
45
"start": "docusaurus-start",
6+
"prebuild": "node scripts/generate-error-docs.js",
57
"build": "docusaurus-build",
68
"publish-gh-pages": "docusaurus-publish",
79
"write-translations": "docusaurus-write-translations",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env node
2+
3+
const { writeFileSync } = require("fs")
4+
const { relative, resolve } = require("path")
5+
6+
const repoRoot = resolve(__dirname, "../..")
7+
const errorsPath = resolve(repoRoot, "packages/mobx/src/errors.ts")
8+
const outputPath = resolve(repoRoot, "docs/errors.md")
9+
10+
globalThis.__DEV__ = true
11+
const { niceErrors } = require(errorsPath)
12+
13+
function formatMarkdownCell(message) {
14+
let value = message
15+
16+
if (typeof message === "function") {
17+
// Render dynamic errors with visible placeholders instead of real runtime values
18+
const match = message.toString().match(/^[^(]*\(([^)]*)\)/)
19+
const params = match ? match[1] : ""
20+
const names = params.split(",").map(name => name.trim()).filter(Boolean)
21+
const args = names.map(name => `{${name}}`)
22+
value = message(...args).replace("String", args[0])
23+
}
24+
25+
// Keep the generated table valid Markdown
26+
return String(value).replace(/\r?\n/g, "<br />").replace(/\|/g, "\\|")
27+
}
28+
29+
function renderDocs(errors) {
30+
const rows = Object.keys(errors)
31+
.sort((left, right) => Number(left) - Number(right))
32+
.map(code => `| ${code} | ${formatMarkdownCell(errors[code])} |`)
33+
.join("\n")
34+
35+
return `---
36+
title: MobX error codes
37+
sidebar_label: Error codes
38+
hide_title: true
39+
custom_edit_url: https://github.com/mobxjs/mobx/edit/main/packages/mobx/src/errors.ts
40+
---
41+
42+
<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CEBD4KQ7&placement=mobxjsorg" id="_carbonads_js"></script>
43+
44+
# MobX error codes
45+
46+
In development builds, MobX throws full error messages. Production builds replace known messages with short numeric codes to keep the published bundle smaller.
47+
48+
| Code | Message |
49+
| ---- | ------- |
50+
${rows}
51+
`
52+
}
53+
54+
const docs = renderDocs(niceErrors)
55+
writeFileSync(outputPath, docs)
56+
57+
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)