Skip to content

Commit 257ef50

Browse files
authored
perf: support drop message compiler feature flag (#1510)
1 parent 7c3e629 commit 257ef50

File tree

14 files changed

+68
-34
lines changed

14 files changed

+68
-34
lines changed

docs/guide/advanced/optimization.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ the each time localization is performed in an application using `$t` or `t` func
227227
You need to configure the following feature flag with `esm-bundler` build and bundler such as vite:
228228
229229
- `__INTLIFY_JIT_COMPILATION__` (enable/disable message compiler for JIT style, default: `false`)
230+
- `__INTLIFY_DROP_MESSAGE_COMPILER__` (enable/disable whether to tree-shake message compiler when we will be bundling, this flag works when `__INTLIFY_JIT_COMPILATION__` is enabled. default: `false`)
230231
231232
:::warning NOTICE
232233
This feature is opted out as default, because compatibility with previous version before v9.3.

packages/core-base/src/compilation.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { warn, format, isBoolean, isString } from '@intlify/shared'
1+
import { warn, format, isObject, isBoolean, isString } from '@intlify/shared'
22
import {
33
baseCompile as baseCompileCore,
44
defaultOnError,
@@ -31,6 +31,11 @@ export function clearCompileCache(): void {
3131
compileCache = Object.create(null)
3232
}
3333

34+
export const isMessageAST = (val: unknown): val is ResourceNode =>
35+
isObject(val) &&
36+
(val.t === 0 || val.type === 0) &&
37+
('b' in val || 'body' in val)
38+
3439
function baseCompile(
3540
message: string,
3641
options: CompileOptions = {}
@@ -101,7 +106,11 @@ export function compile<
101106
message: MessageSource,
102107
context: MessageCompilerContext
103108
): MessageFunction<Message> {
104-
if (isString(message)) {
109+
if (
110+
__FEATURE_JIT_COMPILATION__ &&
111+
!__FEATURE_DROP_MESSAGE_COMPILER__ &&
112+
isString(message)
113+
) {
105114
// check HTML message
106115
const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
107116
? context.warnHtmlMessage
@@ -131,6 +140,13 @@ export function compile<
131140
? ((compileCache as MessageFunctions<Message>)[cacheKey] = msg)
132141
: msg
133142
} else {
143+
if (__DEV__ && !isMessageAST(message)) {
144+
warn(
145+
`the message that is resolve with key '${context.key}') is not supported for jit compilation`
146+
)
147+
return (() => message) as MessageFunction<Message>
148+
}
149+
134150
// AST case (passed from bundler)
135151
const cacheKey = (message as unknown as ResourceNode).cacheKey
136152
if (cacheKey) {

packages/core-base/src/misc.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ export function initFeatureFlags(): void {
1212
if (typeof __FEATURE_JIT_COMPILATION__ !== 'boolean') {
1313
getGlobalThis().__INTLIFY_JIT_COMPILATION__ = false
1414
}
15+
16+
if (typeof __FEATURE_DROP_MESSAGE_COMPILER__ !== 'boolean') {
17+
getGlobalThis().__INTLIFY_DROP_MESSAGE_COMPILER__ = false
18+
}
1519
}

packages/core-base/src/translate.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
assign,
1717
isObject
1818
} from '@intlify/shared'
19+
import { isMessageAST } from './compilation'
1920
import { createMessageContext } from './runtime'
2021
import {
2122
isTranslateFallbackWarn,
@@ -51,12 +52,10 @@ import type {
5152
import type { PickupKeys } from './types'
5253

5354
const NOOP_MESSAGE_FUNCTION = () => ''
55+
5456
export const isMessageFunction = <T>(val: unknown): val is MessageFunction<T> =>
5557
isFunction(val)
5658

57-
export const isMessageAST = (val: unknown): val is ResourceNode =>
58-
isObject(val) && val.type === 0 && ('body' in val || 'b' in val)
59-
6059
/**
6160
* # translate
6261
*

packages/core-base/test/compilation.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { baseCompile } from '@intlify/message-compiler'
22
import {
33
compileToFunction,
44
compile,
5+
isMessageAST,
56
clearCompileCache
67
} from '../src/compilation'
78
import { createMessageContext as context } from '../src/runtime'
@@ -12,6 +13,26 @@ beforeAll(() => {
1213
clearCompileCache()
1314
})
1415

16+
describe('isMessageAST', () => {
17+
describe('basic AST', () => {
18+
test('should be true', () => {
19+
expect(isMessageAST({ type: 0, body: '' })).toBe(true)
20+
})
21+
})
22+
23+
describe('minify AST', () => {
24+
test('should be true', () => {
25+
expect(isMessageAST({ type: 0, b: '' })).toBe(true)
26+
})
27+
})
28+
29+
describe('not message compiler AST format', () => {
30+
test('should be false', () => {
31+
expect(isMessageAST({ b: '' })).toBe(false)
32+
})
33+
})
34+
})
35+
1536
describe('compileToFunction', () => {
1637
test('basic', () => {
1738
const msg = compileToFunction('hello {name}!', DEFAULT_CONTEXT)

packages/core-base/test/translate.test.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ vi.mock('@intlify/shared', async () => {
1313
})
1414

1515
import { createCoreContext as context, NOT_REOSLVED } from '../src/context'
16-
import { translate, isMessageAST } from '../src/translate'
16+
import { translate } from '../src/translate'
1717
import { CoreErrorCodes, errorMessages } from '../src/errors'
1818
import {
1919
registerMessageCompiler,
@@ -991,25 +991,3 @@ describe('AST passing', () => {
991991
expect(translate(ctx, 'hi')).toEqual('hi kazupon !')
992992
})
993993
})
994-
995-
describe('isMessageAST', () => {
996-
describe('basic AST', () => {
997-
test('should be true', () => {
998-
expect(isMessageAST({ type: 0, body: '' })).toBe(true)
999-
})
1000-
})
1001-
1002-
describe('minify AST', () => {
1003-
test('should be true', () => {
1004-
expect(isMessageAST({ type: 0, b: '' })).toBe(true)
1005-
})
1006-
})
1007-
1008-
describe('not message compiler AST format', () => {
1009-
test('should be false', () => {
1010-
expect(isMessageAST({ b: '' })).toBe(false)
1011-
})
1012-
})
1013-
})
1014-
1015-
/* eslint-enable @typescript-eslint/no-empty-function, @typescript-eslint/no-explicit-any */

packages/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ declare let __FEATURE_PROD_INTLIFY_DEVTOOLS__: boolean
1818
declare let __FEATURE_LEGACY_API__: boolean
1919
declare let __FEATURE_FULL_INSTALL__: boolean
2020
declare let __FEATURE_JIT_COMPILATION__: boolean
21+
declare let __FEATURE_DROP_MESSAGE_COMPILER__: boolean

packages/size-check-core/vite.config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ export default defineConfig({
1313
__RUNTIME__: true,
1414
// is targeting Node (SSR)?
1515
__NODE_JS__: false,
16-
'process.env.NODE_ENV': JSON.stringify('production'),
17-
__INTLIFY_JIT_COMPILATION__: true,
18-
__INTLIFY_PROD_DEVTOOLS__: false
16+
__INTLIFY_JIT_COMPILATION__: false,
17+
__INTLIFY_DROP_MESSAGE_COMPILER__: false,
18+
__INTLIFY_PROD_DEVTOOLS__: false,
19+
'process.env.NODE_ENV': JSON.stringify('production')
1920
},
2021
resolve: {
2122
alias: {

packages/size-check-petite-vue-i18n/vite.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export default defineConfig({
1818
__VUE_I18N_LEGACY_API__: false,
1919
__VUE_I18N_FULL_INSTALL__: false,
2020
__VUE_PROD_DEVTOOLS__: false,
21-
// __INTLIFY_JIT_COMPILATION__: true,
21+
__INTLIFY_JIT_COMPILATION__: true,
22+
__INTLIFY_DROP_MESSAGE_COMPILER__: true,
2223
__INTLIFY_PROD_DEVTOOLS__: false,
2324
'process.env.NODE_ENV': JSON.stringify('production')
2425
},
@@ -28,6 +29,7 @@ export default defineConfig({
2829
}
2930
},
3031
build: {
32+
minify: false,
3133
rollupOptions: {
3234
output: {
3335
entryFileNames: `assets/[name].js`,

packages/size-check-vue-i18n/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default defineConfig({
1919
__VUE_I18N_FULL_INSTALL__: false,
2020
__VUE_PROD_DEVTOOLS__: false,
2121
__INTLIFY_JIT_COMPILATION__: true,
22+
__INTLIFY_DROP_MESSAGE_COMPILER__: true,
2223
__INTLIFY_PROD_DEVTOOLS__: false,
2324
'process.env.NODE_ENV': JSON.stringify('production')
2425
// 'process.env.NODE_ENV': JSON.stringify('development')

0 commit comments

Comments
 (0)