Skip to content

Commit

Permalink
perf: support drop message compiler feature flag (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Aug 29, 2023
1 parent 7c3e629 commit 257ef50
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 34 deletions.
1 change: 1 addition & 0 deletions docs/guide/advanced/optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ the each time localization is performed in an application using `$t` or `t` func
You need to configure the following feature flag with `esm-bundler` build and bundler such as vite:
- `__INTLIFY_JIT_COMPILATION__` (enable/disable message compiler for JIT style, default: `false`)
- `__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`)
:::warning NOTICE
This feature is opted out as default, because compatibility with previous version before v9.3.
Expand Down
20 changes: 18 additions & 2 deletions packages/core-base/src/compilation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { warn, format, isBoolean, isString } from '@intlify/shared'
import { warn, format, isObject, isBoolean, isString } from '@intlify/shared'
import {
baseCompile as baseCompileCore,
defaultOnError,
Expand Down Expand Up @@ -31,6 +31,11 @@ export function clearCompileCache(): void {
compileCache = Object.create(null)
}

export const isMessageAST = (val: unknown): val is ResourceNode =>
isObject(val) &&
(val.t === 0 || val.type === 0) &&
('b' in val || 'body' in val)

function baseCompile(
message: string,
options: CompileOptions = {}
Expand Down Expand Up @@ -101,7 +106,11 @@ export function compile<
message: MessageSource,
context: MessageCompilerContext
): MessageFunction<Message> {
if (isString(message)) {
if (
__FEATURE_JIT_COMPILATION__ &&
!__FEATURE_DROP_MESSAGE_COMPILER__ &&
isString(message)
) {
// check HTML message
const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
? context.warnHtmlMessage
Expand Down Expand Up @@ -131,6 +140,13 @@ export function compile<
? ((compileCache as MessageFunctions<Message>)[cacheKey] = msg)
: msg
} else {
if (__DEV__ && !isMessageAST(message)) {
warn(
`the message that is resolve with key '${context.key}') is not supported for jit compilation`
)
return (() => message) as MessageFunction<Message>
}

// AST case (passed from bundler)
const cacheKey = (message as unknown as ResourceNode).cacheKey
if (cacheKey) {
Expand Down
4 changes: 4 additions & 0 deletions packages/core-base/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ export function initFeatureFlags(): void {
if (typeof __FEATURE_JIT_COMPILATION__ !== 'boolean') {
getGlobalThis().__INTLIFY_JIT_COMPILATION__ = false
}

if (typeof __FEATURE_DROP_MESSAGE_COMPILER__ !== 'boolean') {
getGlobalThis().__INTLIFY_DROP_MESSAGE_COMPILER__ = false
}
}
5 changes: 2 additions & 3 deletions packages/core-base/src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
assign,
isObject
} from '@intlify/shared'
import { isMessageAST } from './compilation'
import { createMessageContext } from './runtime'
import {
isTranslateFallbackWarn,
Expand Down Expand Up @@ -51,12 +52,10 @@ import type {
import type { PickupKeys } from './types'

const NOOP_MESSAGE_FUNCTION = () => ''

export const isMessageFunction = <T>(val: unknown): val is MessageFunction<T> =>
isFunction(val)

export const isMessageAST = (val: unknown): val is ResourceNode =>
isObject(val) && val.type === 0 && ('body' in val || 'b' in val)

/**
* # translate
*
Expand Down
21 changes: 21 additions & 0 deletions packages/core-base/test/compilation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { baseCompile } from '@intlify/message-compiler'
import {
compileToFunction,
compile,
isMessageAST,
clearCompileCache
} from '../src/compilation'
import { createMessageContext as context } from '../src/runtime'
Expand All @@ -12,6 +13,26 @@ beforeAll(() => {
clearCompileCache()
})

describe('isMessageAST', () => {
describe('basic AST', () => {
test('should be true', () => {
expect(isMessageAST({ type: 0, body: '' })).toBe(true)
})
})

describe('minify AST', () => {
test('should be true', () => {
expect(isMessageAST({ type: 0, b: '' })).toBe(true)
})
})

describe('not message compiler AST format', () => {
test('should be false', () => {
expect(isMessageAST({ b: '' })).toBe(false)
})
})
})

describe('compileToFunction', () => {
test('basic', () => {
const msg = compileToFunction('hello {name}!', DEFAULT_CONTEXT)
Expand Down
24 changes: 1 addition & 23 deletions packages/core-base/test/translate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ vi.mock('@intlify/shared', async () => {
})

import { createCoreContext as context, NOT_REOSLVED } from '../src/context'
import { translate, isMessageAST } from '../src/translate'
import { translate } from '../src/translate'
import { CoreErrorCodes, errorMessages } from '../src/errors'
import {
registerMessageCompiler,
Expand Down Expand Up @@ -991,25 +991,3 @@ describe('AST passing', () => {
expect(translate(ctx, 'hi')).toEqual('hi kazupon !')
})
})

describe('isMessageAST', () => {
describe('basic AST', () => {
test('should be true', () => {
expect(isMessageAST({ type: 0, body: '' })).toBe(true)
})
})

describe('minify AST', () => {
test('should be true', () => {
expect(isMessageAST({ type: 0, b: '' })).toBe(true)
})
})

describe('not message compiler AST format', () => {
test('should be false', () => {
expect(isMessageAST({ b: '' })).toBe(false)
})
})
})

/* eslint-enable @typescript-eslint/no-empty-function, @typescript-eslint/no-explicit-any */
1 change: 1 addition & 0 deletions packages/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ declare let __FEATURE_PROD_INTLIFY_DEVTOOLS__: boolean
declare let __FEATURE_LEGACY_API__: boolean
declare let __FEATURE_FULL_INSTALL__: boolean
declare let __FEATURE_JIT_COMPILATION__: boolean
declare let __FEATURE_DROP_MESSAGE_COMPILER__: boolean
7 changes: 4 additions & 3 deletions packages/size-check-core/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export default defineConfig({
__RUNTIME__: true,
// is targeting Node (SSR)?
__NODE_JS__: false,
'process.env.NODE_ENV': JSON.stringify('production'),
__INTLIFY_JIT_COMPILATION__: true,
__INTLIFY_PROD_DEVTOOLS__: false
__INTLIFY_JIT_COMPILATION__: false,
__INTLIFY_DROP_MESSAGE_COMPILER__: false,
__INTLIFY_PROD_DEVTOOLS__: false,
'process.env.NODE_ENV': JSON.stringify('production')
},
resolve: {
alias: {
Expand Down
4 changes: 3 additions & 1 deletion packages/size-check-petite-vue-i18n/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default defineConfig({
__VUE_I18N_LEGACY_API__: false,
__VUE_I18N_FULL_INSTALL__: false,
__VUE_PROD_DEVTOOLS__: false,
// __INTLIFY_JIT_COMPILATION__: true,
__INTLIFY_JIT_COMPILATION__: true,
__INTLIFY_DROP_MESSAGE_COMPILER__: true,
__INTLIFY_PROD_DEVTOOLS__: false,
'process.env.NODE_ENV': JSON.stringify('production')
},
Expand All @@ -28,6 +29,7 @@ export default defineConfig({
}
},
build: {
minify: false,
rollupOptions: {
output: {
entryFileNames: `assets/[name].js`,
Expand Down
1 change: 1 addition & 0 deletions packages/size-check-vue-i18n/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default defineConfig({
__VUE_I18N_FULL_INSTALL__: false,
__VUE_PROD_DEVTOOLS__: false,
__INTLIFY_JIT_COMPILATION__: true,
__INTLIFY_DROP_MESSAGE_COMPILER__: true,
__INTLIFY_PROD_DEVTOOLS__: false,
'process.env.NODE_ENV': JSON.stringify('production')
// 'process.env.NODE_ENV': JSON.stringify('development')
Expand Down
4 changes: 4 additions & 0 deletions packages/vue-i18n-core/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export function initFeatureFlags(): void {
getGlobalThis().__INTLIFY_JIT_COMPILATION__ = false
}

if (typeof __FEATURE_DROP_MESSAGE_COMPILER__ !== 'boolean') {
getGlobalThis().__INTLIFY_DROP_MESSAGE_COMPILER__ = false
}

if (typeof __FEATURE_PROD_INTLIFY_DEVTOOLS__ !== 'boolean') {
getGlobalThis().__INTLIFY_PROD_DEVTOOLS__ = false
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vue-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ The `esm-bundler` builds now exposes global feature flags that can be overwritte
- `__VUE_I18N_FULL_INSTALL__` (enable/disable, in addition to vue-i18n APIs, components and directives all fully support installation: `true`)
- `__VUE_I18N_LEGACY_API__` (enable/disable vue-i18n legacy style APIs support, default: `true`)
- `__INTLIFY_JIT_COMPILATION__` (enable/disable message compiler for JIT style, default: `false`)
- `__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`)

> NOTE: `__INTLIFY_JIT_COMPILATION__` flag support version, 9.3+
> NOTE: `__INTLIFY_JIT_COMPILATION__`, `__INTLIFY_DROP_MESSAGE_COMPILER__` flag support version, 9.3+
The build will work without configuring these flags, however it is **strongly recommended** to properly configure them in order to get proper tree shaking in the final bundle. To configure these flags:

Expand Down
3 changes: 3 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ function createReplacePlugin(
__FEATURE_JIT_COMPILATION__: isBundlerESMBuild
? `__INTLIFY_JIT_COMPILATION__`
: `false`,
__FEATURE_DROP_MESSAGE_COMPILER__: isBundlerESMBuild
? `__INTLIFY_DROP_MESSAGE_COMPILER__`
: `false`,
...(isProduction && isBrowserBuild
? {
'emitError(': `/*#__PURE__*/ emitError(`,
Expand Down
4 changes: 3 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export default defineConfig({
__LITE__: false,
__BRIDGE__: false,
__FEATURE_FULL_INSTALL__: true,
__FEATURE_LEGACY_API__: true
__FEATURE_LEGACY_API__: true,
__FEATURE_JIT_COMPILATION__: true,
__FEATURE_DROP_MESSAGE_COMPILER__: false
},
resolve: {
alias: entries
Expand Down

0 comments on commit 257ef50

Please sign in to comment.