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

fix: cleanup global injection resources #1479

Merged
merged 1 commit into from
Aug 2, 2023
Merged
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
26 changes: 24 additions & 2 deletions packages/vue-i18n-core/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,13 @@ export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
}

// global method and properties injection for Composition API
let globalReleaseHandler: ReturnType<typeof injectGlobalFields> | null =
null
if (!__legacyMode && __globalInjection) {
injectGlobalFields(app, i18n.global as Composer)
globalReleaseHandler = injectGlobalFields(
app,
i18n.global as Composer
)
}

// install built-in components and directive
Expand All @@ -614,6 +619,7 @@ export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
// release global scope
const unmountApp = app.unmount
app.unmount = () => {
globalReleaseHandler && globalReleaseHandler()
i18n.dispose()
unmountApp()
}
Expand Down Expand Up @@ -1650,7 +1656,12 @@ const globalExportMethods = !__LITE__
? ['t', 'rt', 'd', 'n', 'tm', 'te']
: ['t']

function injectGlobalFields(app: App, composer: Composer): void {
type GlobalInjectionReleaseHandler = () => void

function injectGlobalFields(
app: App,
composer: Composer
): GlobalInjectionReleaseHandler {
const i18n = Object.create(null)
globalExportProps.forEach(prop => {
const desc = Object.getOwnPropertyDescriptor(composer, prop)
Expand Down Expand Up @@ -1683,6 +1694,17 @@ function injectGlobalFields(app: App, composer: Composer): void {
}
Object.defineProperty(app.config.globalProperties, `$${method}`, desc)
})

const release = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (app as any).config.globalProperties.$i18n
globalExportMethods.forEach(method => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (app as any).config.globalProperties[`$${method}`]
})
}

return release
}

function injectGlobalFieldsForBridge(
Expand Down