Skip to content
This repository was archived by the owner on Jul 4, 2021. It is now read-only.

Commit a84027d

Browse files
authored
feat: bundling optimizations (#41)
* improvement: support named & default exporting * feat: bundling optimizations
1 parent 96182e5 commit a84027d

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Vite plugin for Vue I18n
99
## :star: Features
1010
- i18n resources pre-compilation
1111
- i18n custom block
12+
- Bundling optimizations
1213

1314
## :cd: Installation
1415

@@ -24,6 +25,10 @@ $ npm i --save-dev @intlify/vite-plugin-vue-i18n
2425
$ yarn add -D @intlify/vite-plugin-vue-i18n
2526
```
2627

28+
## :warning: Notice
29+
30+
When this plugin is installed, Vue I18n can only use the Composition API, and if you want to use the Legacy API, you need to set the `compositionOnly` option to `false`.
31+
2732

2833
## :rocket: Usage
2934

@@ -122,6 +127,14 @@ ja:
122127
</i18n>
123128
```
124129
130+
### Bundling optimizations
131+
132+
vite-plugin-vue-i18n allows you to specify options in the plugin option to support bundling size optimization provided by vue-i18n.
133+
134+
The same thing can be [configured](https://vue-i18n-next.intlify.dev/advanced/optimization.html#reduce-bundle-size-with-feature-build-flags) with the `define` option, but the plugin option is more friendly. Especially if you are using typescript, you can use intelisense.
135+
136+
About details, See the below section
137+
125138
126139
## :wrench: Options
127140
@@ -141,6 +154,35 @@ ja:
141154
142155
Note `json` resources matches this option, it will be handled **before the internal json plugin of Vite, and will not be processed afterwards**, else the option doesn't match, the Vite side will handle.
143156
157+
### `runtimeOnly`
158+
159+
- **Type:** `boolean`
160+
- **Default:** `true`
161+
162+
Whether or not to use Vue I18n **runtime-only**, set in the `vue-i18n` field of Vite `alias` option.
163+
164+
If `false` is specified, Vue I18n (vue-i18n) package.json `module` field will be used.
165+
166+
For more details, See [here](https://vue-i18n-next.intlify.dev/advanced/optimization.html#improve-performance-and-reduce-bundle-size-with-runtime-build-only)
167+
168+
### `compositionOnly`
169+
170+
- **Type:** `boolean`
171+
- **Default:** `true`
172+
173+
Whether to make vue-i18n's API only composition API. **By default the legacy API is tree-shaken.**
174+
175+
For more details, See [here](https://vue-i18n-next.intlify.dev/advanced/optimization.html#reduce-bundle-size-with-feature-build-flags)
176+
177+
### `fullInstall`
178+
179+
- **Type:** `boolean`
180+
- **Default:** `true`
181+
182+
Whether to install the full set of APIs, components, etc. provided by Vue I18n. By default, all of them will be installed.
183+
184+
If `false` is specified, **buld-in components and directive will not be installed in vue and will be tree-shaken.**
185+
144186
### `forceStringify`
145187
146188
- **Type:** `boolean`

examples/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export default defineConfig({
1616
plugins: [
1717
vue(),
1818
vueI18n({
19-
include: path.resolve(__dirname, './**/locales/**')
19+
include: path.resolve(__dirname, './**/locales/**'),
20+
compositionOnly: false
2021
})
2122
]
2223
})

src/index.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
;(async () => {
2+
try {
3+
await import('vue-i18n')
4+
} catch (e) {
5+
throw new Error(
6+
'@intlify/vite-plugin-vue-i18n requires vue-i18n to be present in the dependency tree.'
7+
)
8+
}
9+
})()
10+
111
import { promises as fs } from 'fs'
212
import path from 'path'
3-
import { isEmptyObject, isString } from '@intlify/shared'
13+
import { isBoolean, isEmptyObject, isString } from '@intlify/shared'
414
import { createFilter } from '@rollup/pluginutils'
515
import { generateJSON, generateYAML } from '@intlify/cli'
616
import { debug as Debug } from 'debug'
@@ -18,11 +28,42 @@ function pluginI18n(
1828
debug('plugin options:', options)
1929

2030
const filter = createFilter(options.include)
31+
const runtimeOnly = isBoolean(options.runtimeOnly)
32+
? options.runtimeOnly
33+
: true
34+
const compositionOnly = isBoolean(options.compositionOnly)
35+
? options.compositionOnly
36+
: true
37+
const fullIinstall = isBoolean(options.fullInstall)
38+
? options.fullInstall
39+
: true
2140
let config: ResolvedConfig | null = null
2241

2342
return {
2443
name: 'vite-plugin-vue-i18n',
2544

45+
config() {
46+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
47+
const partialConfig: any = { define: {}, alias: {} }
48+
49+
if (runtimeOnly) {
50+
partialConfig.alias['vue-i18n'] =
51+
'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js'
52+
debug('set vue-i18n alias')
53+
}
54+
55+
if (compositionOnly) {
56+
partialConfig.define['__VUE_I18N_LEGACY_API__'] = false
57+
debug('set __VUE_I18N_LEGACY_API__ is `false`')
58+
}
59+
if (!fullIinstall) {
60+
partialConfig.define['__VUE_I18N_FULL_INSTALL__'] = false
61+
debug('set __VUE_I18N_FULL_INSTALL__ is `false`')
62+
}
63+
64+
return partialConfig
65+
},
66+
2667
configResolved(_config: ResolvedConfig) {
2768
// store config
2869
config = _config
@@ -52,7 +93,7 @@ function pluginI18n(
5293

5394
async transform(code: string, id: string) {
5495
const { filename, query } = parseVueRequest(id)
55-
debug('transform', id, code, JSON.stringify(query))
96+
debug('transform', id, JSON.stringify(query))
5697

5798
const parseOptions = getOptions(
5899
filename,

src/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export type VitePluginVueI18nOptions = {
22
forceStringify?: boolean
3+
runtimeOnly?: boolean
4+
compositionOnly?: boolean
5+
fullInstall?: boolean
36
include?: string | string[]
47
}

0 commit comments

Comments
 (0)