Skip to content

Commit 51a009d

Browse files
authored
perf: more bundle size optimization (#1851)
1 parent 4b231ce commit 51a009d

File tree

12 files changed

+67
-94
lines changed

12 files changed

+67
-94
lines changed

packages/core-base/src/errors.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
import { incrementer } from '@intlify/shared'
21
import {
3-
CompileErrorCodes,
4-
createCompileError
2+
createCompileError,
3+
COMPILE_ERROR_CODES_EXTEND_POINT
54
} from '@intlify/message-compiler'
65

76
import type { BaseError } from '@intlify/shared'
87

98
export interface CoreError extends BaseError {}
109

11-
const code = CompileErrorCodes.__EXTEND_POINT__
12-
const inc = incrementer(code)
13-
1410
export const CoreErrorCodes = {
15-
INVALID_ARGUMENT: code, // 17
16-
INVALID_DATE_ARGUMENT: inc(), // 18
17-
INVALID_ISO_DATE_ARGUMENT: inc(), // 19
18-
NOT_SUPPORT_NON_STRING_MESSAGE: inc(), // 20
19-
NOT_SUPPORT_LOCALE_PROMISE_VALUE: inc(), // 21
20-
NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: inc(), // 22
21-
NOT_SUPPORT_LOCALE_TYPE: inc(), // 23
22-
__EXTEND_POINT__: inc() // 24
11+
INVALID_ARGUMENT: COMPILE_ERROR_CODES_EXTEND_POINT, // 17
12+
INVALID_DATE_ARGUMENT: 18,
13+
INVALID_ISO_DATE_ARGUMENT: 19,
14+
NOT_SUPPORT_NON_STRING_MESSAGE: 20,
15+
NOT_SUPPORT_LOCALE_PROMISE_VALUE: 21,
16+
NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: 22,
17+
NOT_SUPPORT_LOCALE_TYPE: 23
2318
} as const
2419

20+
export const CORE_ERROR_CODES_EXTEND_POINT = 24
21+
2522
export type CoreErrorCodes =
2623
(typeof CoreErrorCodes)[keyof typeof CoreErrorCodes]
2724

packages/core-base/src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ export * from './compilation'
1414
export * from './translate'
1515
export * from './datetime'
1616
export * from './number'
17-
export { getWarnMessage, CoreWarnCodes } from './warnings'
18-
export { CoreError, CoreErrorCodes, createCoreError } from './errors'
17+
export {
18+
getWarnMessage,
19+
CoreWarnCodes,
20+
CORE_WARN_CODES_EXTEND_POINT
21+
} from './warnings'
22+
export {
23+
CoreError,
24+
CoreErrorCodes,
25+
createCoreError,
26+
CORE_ERROR_CODES_EXTEND_POINT
27+
} from './errors'
1928
export * from './types'
2029
export * from './devtools'
2130

packages/core-base/src/warnings.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import { format, incrementer } from '@intlify/shared'
2-
3-
const code = 1
4-
const inc = incrementer(code)
1+
import { format } from '@intlify/shared'
52

63
export const CoreWarnCodes = {
7-
NOT_FOUND_KEY: code, // 1
8-
FALLBACK_TO_TRANSLATE: inc(), // 2
9-
CANNOT_FORMAT_NUMBER: inc(), // 3
10-
FALLBACK_TO_NUMBER_FORMAT: inc(), // 4
11-
CANNOT_FORMAT_DATE: inc(), // 5
12-
FALLBACK_TO_DATE_FORMAT: inc(), // 6
13-
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: inc(), // 7
14-
__EXTEND_POINT__: inc() // 8
4+
NOT_FOUND_KEY: 1,
5+
FALLBACK_TO_TRANSLATE: 2,
6+
CANNOT_FORMAT_NUMBER: 3,
7+
FALLBACK_TO_NUMBER_FORMAT: 4,
8+
CANNOT_FORMAT_DATE: 5,
9+
FALLBACK_TO_DATE_FORMAT: 6,
10+
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7
1511
} as const
1612

13+
export const CORE_WARN_CODES_EXTEND_POINT = 8
14+
1715
export type CoreWarnCodes = (typeof CoreWarnCodes)[keyof typeof CoreWarnCodes]
1816

1917
/** @internal */
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { CoreErrorCodes } from '../src/errors'
1+
import { CORE_ERROR_CODES_EXTEND_POINT } from '../src/errors'
22

33
test('CoreErrorCodes', () => {
4-
expect(CoreErrorCodes.__EXTEND_POINT__).toBe(24)
4+
expect(CORE_ERROR_CODES_EXTEND_POINT).toBe(24)
55
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { CoreWarnCodes } from '../src/warnings'
1+
import { CORE_WARN_CODES_EXTEND_POINT } from '../src/warnings'
22

33
test('CoreWarnCodes', () => {
4-
expect(CoreWarnCodes.__EXTEND_POINT__).toBe(8)
4+
expect(CORE_WARN_CODES_EXTEND_POINT).toBe(8)
55
})

packages/message-compiler/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"exports": {
5454
".": {
5555
"types": "./dist/message-compiler.d.ts",
56+
"import": "./dist/message-compiler.mjs",
5657
"browser": "./dist/message-compiler.esm-browser.js",
5758
"node": {
5859
"import": {
@@ -65,8 +66,7 @@
6566
"development": "./dist/message-compiler.cjs",
6667
"default": "./index.js"
6768
}
68-
},
69-
"import": "./dist/message-compiler.mjs"
69+
}
7070
},
7171
"./dist/*": "./dist/*",
7272
"./package.json": "./package.json"

packages/message-compiler/src/errors.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,22 @@ export const CompileErrorCodes = {
3434
EMPTY_PLACEHOLDER: 8,
3535
NOT_ALLOW_NEST_PLACEHOLDER: 9,
3636
INVALID_LINKED_FORMAT: 10,
37-
3837
// parser error codes
3938
MUST_HAVE_MESSAGES_IN_PLURAL: 11,
4039
UNEXPECTED_EMPTY_LINKED_MODIFIER: 12,
4140
UNEXPECTED_EMPTY_LINKED_KEY: 13,
4241
UNEXPECTED_LEXICAL_ANALYSIS: 14,
43-
4442
// generator error codes
4543
UNHANDLED_CODEGEN_NODE_TYPE: 15,
46-
4744
// minifier error codes
48-
UNHANDLED_MINIFIER_NODE_TYPE: 16,
49-
50-
// Special value for higher-order compilers to pick up the last code
51-
// to avoid collision of error codes. This should always be kept as the last
52-
// item.
53-
__EXTEND_POINT__: 17
45+
UNHANDLED_MINIFIER_NODE_TYPE: 16
5446
} as const
5547

48+
// Special value for higher-order compilers to pick up the last code
49+
// to avoid collision of error codes.
50+
// This should always be kept as the last item.
51+
export const COMPILE_ERROR_CODES_EXTEND_POINT = 17
52+
5653
export type CompileErrorCodes =
5754
(typeof CompileErrorCodes)[keyof typeof CompileErrorCodes]
5855

packages/shared/src/utils.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,3 @@ export function generateCodeFrame(
202202
}
203203
return res.join('\n')
204204
}
205-
206-
export function incrementer(code: number): () => number {
207-
let current = code
208-
return () => ++current
209-
}

packages/shared/test/utils.test.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
format,
3-
generateCodeFrame,
4-
makeSymbol,
5-
join,
6-
incrementer
7-
} from '../src/index'
1+
import { format, generateCodeFrame, makeSymbol, join } from '../src/index'
82

93
test('format', () => {
104
expect(format(`foo: {0}`, 'x')).toEqual('foo: x')
@@ -60,13 +54,3 @@ test('join', () => {
6054
]
6155
expect(join(longSize, ' ')).toEqual(longSize.join(' '))
6256
})
63-
64-
test('incrementer', () => {
65-
const inc1 = incrementer(1)
66-
const inc2 = incrementer(2)
67-
68-
expect(inc1()).toBe(2)
69-
expect(inc1()).toBe(3)
70-
expect(inc2()).toBe(3)
71-
expect(inc2()).toBe(4)
72-
})

packages/vue-i18n-core/src/errors.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
import { incrementer } from '@intlify/shared'
2-
import { CoreErrorCodes, createCompileError } from '@intlify/core-base'
1+
import {
2+
createCompileError,
3+
CORE_ERROR_CODES_EXTEND_POINT
4+
} from '@intlify/core-base'
35

46
import type { BaseError } from '@intlify/shared'
57

68
export interface I18nError extends BaseError {}
79

8-
const code = CoreErrorCodes.__EXTEND_POINT__
9-
const inc = incrementer(code)
10-
1110
export const I18nErrorCodes = {
1211
// composer module errors
13-
UNEXPECTED_RETURN_TYPE: code, // 24
12+
UNEXPECTED_RETURN_TYPE: CORE_ERROR_CODES_EXTEND_POINT, // 24
1413
// legacy module errors
15-
INVALID_ARGUMENT: inc(), // 25
14+
INVALID_ARGUMENT: 25,
1615
// i18n module errors
17-
MUST_BE_CALL_SETUP_TOP: inc(), // 26
18-
NOT_INSTALLED: inc(), // 27
16+
MUST_BE_CALL_SETUP_TOP: 26,
17+
NOT_INSTALLED: 27,
1918
// directive module errors
20-
REQUIRED_VALUE: inc(), // 28
21-
INVALID_VALUE: inc(), // 29
19+
REQUIRED_VALUE: 28,
20+
INVALID_VALUE: 29,
2221
// vue-devtools errors
23-
CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN: inc(), // 30
24-
NOT_INSTALLED_WITH_PROVIDE: inc(), // 31
22+
CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN: 30,
23+
NOT_INSTALLED_WITH_PROVIDE: 31,
2524
// unexpected error
26-
UNEXPECTED_ERROR: inc(), // 32
25+
UNEXPECTED_ERROR: 32,
2726
// not compatible legacy vue-i18n constructor
28-
NOT_COMPATIBLE_LEGACY_VUE_I18N: inc(), // 33
27+
NOT_COMPATIBLE_LEGACY_VUE_I18N: 33,
2928
// Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly
30-
NOT_AVAILABLE_COMPOSITION_IN_LEGACY: inc(), // 34
31-
// for enhancement
32-
__EXTEND_POINT__: inc() // 35
29+
NOT_AVAILABLE_COMPOSITION_IN_LEGACY: 34
3330
} as const
3431

3532
type I18nErrorCodes = (typeof I18nErrorCodes)[keyof typeof I18nErrorCodes]

0 commit comments

Comments
 (0)