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

feat(unplugin-vue-i18n): exclude unused locales from the bundle #301

Merged
merged 3 commits into from
Sep 28, 2023
Merged

feat(unplugin-vue-i18n): exclude unused locales from the bundle #301

merged 3 commits into from
Sep 28, 2023

Conversation

imslepov
Copy link
Contributor

@imslepov imslepov commented Sep 28, 2023

Motivation

When developing projects, we use Nuxt Layers to reuse common components with business logic between similar projects. For one of the projects we need only English, and for the other only German. Because the bundle of both applications includes translations of both languages, the size of unused code that is loaded in the browser also increases.

Description of changes

In this PR I added a new onlyLocales property to @intlify/unplugin-vue-i18n config. By using it you can exclude from the bundle those localizations that are not specified in the plugin settings.

For example:

// vite.config.ts
export default defineConfig({
  plugins: [
    vueI18n({
      onlyLocales: ['ja']
    })
  ]
})
<template>
  <!-- App.vue -->
  <p>{{ t('hello') }}</p>
</template>

<i18n>
{
  "en": {
    "hello": "hello, world!"
  },
  "ja": {
    "hello": "こんにちは、世界!"
  }
}
</i18n>

As a result of the build, there will be no other localizations in the bundle:

// bundle.js
export default function (Component) {
  const _Component = Component
  _Component.__i18n = _Component.__i18n || []
  _Component.__i18n.push({
    "locale": "",
    "resource": {
      "ja": {
         // body
      }
      // resources for locale "en" are not provided because "en" is not specified in vite.config.ts
    }
  })
}

Tests

  • All available tests passed successfully.
  • Added tests for new functionality.

@kazupon kazupon merged commit 06dcfac into intlify:main Sep 28, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants