Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: more optimize bundle size #2065

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
"build:sourcemap": "pnpm build --sourcemap",
"build:type": "./scripts/build.sh",
"build:typed": "pnpm build core-base vue-i18n-core --withTypes",
"size": "tsx ./scripts/build.ts --size && tsx ./scripts/size.ts",
"size:report": "tsx ./scripts/report-size.ts",
"check-install": "tsx ./scripts/playwright.ts",
"clean": "run-p clean:*",
"clean:coverage": "rm -rf ./coverage",
"clean:dist": "rm -rf ./dist ./packages/**/dist ./docs/.vitepress/dist",
"clean:docs": "trash './docs/api/!(injection).md'",
"clean:type": "rm -rf ./temp",
"coverage": "opener coverage/index.html",
"dev": "tsx ./scripts/dev.ts",
"dev:e2e": "cross-env TZ=UTC vitest -c ./vitest.e2e.config.ts",
"dev:eslint": "npx @eslint/config-inspector",
Expand Down Expand Up @@ -73,8 +73,8 @@
"preview:size-petite-vue-i18n": "pnpm --filter @intlify/size-check-petite-vue-i18n preview",
"preview:size-vue-i18n": "pnpm --filter @intlify/size-check-vue-i18n preview",
"release": "bumpp package.json packages/**/package.json --commit \"release: v\" --push --tag",
"check-install": "tsx ./scripts/playwright.ts",
"coverage": "opener coverage/index.html",
"size": "tsx ./scripts/build.ts --size && tsx ./scripts/size.ts",
"size:report": "tsx ./scripts/report-size.ts",
"test": "run-s lint test:cover check-install test:e2e",
"test:cover": "pnpm test:unit --coverage",
"test:e2e": "cross-env TZ=UTC vitest run -c ./vitest.e2e.config.ts",
Expand Down
24 changes: 18 additions & 6 deletions packages/core-base/src/compilation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
baseCompile as baseCompileCore,
createParser,
defaultOnError,
detectHtmlTag
detectHtmlTag,
mangle,
optimize
} from '@intlify/message-compiler'
import {
create,
Expand Down Expand Up @@ -59,8 +61,17 @@ function baseCompile(
onError(err)
}

// compile with mesasge-compiler
return { ...baseCompileCore(message, options), detectError }
// parse source codes
const parser = createParser(options)
const ast = parser.parse(message)

// optimize ASTs
options.optimize && optimize(ast)

// minimize ASTs
options.mangle && mangle(ast)

return { ast, detectError, code: '' }
}

/* #__NO_SIDE_EFFECTS__ */
Expand Down Expand Up @@ -94,11 +105,12 @@ export function compile<
return cached
}

// compile with JIT mode
// compile message
const { ast, detectError } = baseCompile(message, {
...context,
location: __DEV__,
jit: true
mangle: !__DEV__,
optimize: !__DEV__
})

// compose message function from AST
Expand Down
2 changes: 1 addition & 1 deletion packages/core-base/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function resolveValue<Message = string>(
node as Node,
PROPS_VALUE
) as MessageType<Message>
if (resolved) {
if (resolved != null) {
Copy link
Member Author

@kazupon kazupon Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In unit tests for regression, I found bug for Literal case, such as ''.
I've fixed it.

return resolved
} else {
throw createUnhandleNodeError(type)
Expand Down
3 changes: 3 additions & 0 deletions packages/message-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export function baseCompile(
const parser = createParser(assignedOptions)
const ast = parser.parse(source)

// TODO:
// With the introduction of Jit compilation, code generation is no longer necessary. This function may no longer be needed since tree-shaking is not possible.

if (!jit) {
// transform ASTs
transform(ast, assignedOptions)
Expand Down
8 changes: 5 additions & 3 deletions packages/message-compiler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export * from './compiler'
export * from './errors'
export * from './helpers'
export * from './location'
export * from './mangler'
export * from './nodes'
export * from './optimizer'
export * from './options'
export * from './errors'
export * from './helpers'
export * from './parser'
export * from './compiler'
2 changes: 1 addition & 1 deletion packages/message-compiler/src/mangler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
TextNode
} from './nodes'

export const ERROR_DOMAIN = 'minifier'
const ERROR_DOMAIN = 'minifier'

/* eslint-disable @typescript-eslint/no-explicit-any */

Expand Down
2 changes: 1 addition & 1 deletion packages/message-compiler/src/optimizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NodeTypes } from './nodes'
import { join } from '@intlify/shared'
import { NodeTypes } from './nodes'

import type { MessageNode, ResourceNode } from './nodes'

Expand Down
Loading