|
1 | 1 | # Optimization
|
2 | 2 |
|
3 | 3 |
|
4 |
| -## Improve performance and reduce bundle size with runtime build only |
| 4 | +## Performance |
5 | 5 |
|
6 | 6 | As described in "[installation](../../installation##from-cdn-or-without-a-bundler)" section, Vue I18n offer the following two built ES modules for Bundler.
|
7 | 7 |
|
8 | 8 | - message compiler + runtime: **`vue-i18n.esm-bundler.js`**
|
9 | 9 | - runtime only: **`vue-i18n.runtime.esm-bundler.js`**
|
10 | 10 |
|
11 |
| -For bundler, it’s configured to bundle `vue-i18n.esm-bundler.js` by default. If you want to reduce the bundle size further, you can configure the bundler to use `vue-i18n.runtime.esm-bundler.js`, which is runtime only. |
12 |
| - |
13 |
| -The use of this ES Module means that **all locale messages have to pre-compile to Message functions**. |
| 11 | +For bundler, it’s configured to bundle `vue-i18n.esm-bundler.js` with [`@intlify/bundle-tools`](https://github.com/intlify/bundle-tools#intlifybundle-tools) as default. If you want to reduce the bundle size further, you can configure the bundler to use `vue-i18n.runtime.esm-bundler.js`, which is runtime only. |
14 | 12 |
|
15 | 13 | :::danger NOTE
|
16 | 14 | IF [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) is enabled, `vue-i18n.esm-bundler.js` would not work with compiler due to `eval` statements. These statements violate the `default-src 'self'` header. Instead you need to use `vue-i18n.runtime.esm-bundler.js`.
|
17 | 15 | :::
|
18 | 16 |
|
19 |
| -### vite |
| 17 | +The use of this ES Module means that **all locale messages have to pre-compile to Message functions**. what this means it improves performance because vue-i18n just only execute Message functions, so no compilation. |
| 18 | + |
| 19 | +Also, the message compiler is not bundled, therefore **bundle size can be reduced** |
| 20 | + |
| 21 | +## How to configure |
| 22 | + |
| 23 | +We can configure these modules with module path using the module resolve alias feature (e.g. `resolve.alias` vite and webpack) of some bundler, but It takes time and effort. |
| 24 | +Intlify project provides plugins/loaders for some bundlers, for simplicity |
| 25 | + |
| 26 | +### `@intlify/unplugin-vue-i18n` |
20 | 27 |
|
21 |
| -In vite, use `alias` option as below: |
| 28 | +[`unplugin`](https://github.com/unjs/unplugin) is an unified plugin system for bundle tool such as vite, webpack, rollup, esbuild and etc. |
| 29 | + |
| 30 | +Intlify project is providing [`@intlify/unplugin-vue-i18n`](https://github.com/intlify/bundle-tools/tree/main/packages/unplugin-vue-i18n) for vite and webpack. |
| 31 | + |
| 32 | +If you do a production build, Vue I18n will automatically bundle the runtime only module |
| 33 | + |
| 34 | +#### Install plugin |
| 35 | + |
| 36 | +```sh |
| 37 | +npm install --save-dev @intlify/unplugin-vue-i18n |
| 38 | +``` |
| 39 | + |
| 40 | +#### Configure plugin for vite |
22 | 41 |
|
23 | 42 | ```js
|
24 |
| -import path from 'path' |
| 43 | +// vite.config.ts |
25 | 44 | import { defineConfig } from 'vite'
|
26 |
| -import vue from '@vitejs/plugin-vue' |
27 |
| -import vueI18n from '@intlify/vite-plugin-vue-i18n' |
| 45 | +import { resolve, dirname } from 'node:path' |
| 46 | +import { fileURLToPath } from 'url' |
| 47 | +import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite' |
28 | 48 |
|
29 | 49 | export default defineConfig({
|
30 |
| - // ... |
31 |
| - alias: { |
32 |
| - 'vue-i18n': 'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js' |
33 |
| - }, |
| 50 | + /* ... */ |
34 | 51 | plugins: [
|
35 |
| - vue(), |
36 |
| - vueI18n({ |
37 |
| - include: path.resolve(__dirname, './path/to/src/locales/**') |
38 |
| - }) |
| 52 | + /* ... */ |
| 53 | + VueI18nPlugin({ |
| 54 | + /* options */ |
| 55 | + // locale messages resourece pre-compile option |
| 56 | + include: resolve(dirname(fileURLToPath(import.meta.url)), './path/to/src/locales/**'), |
| 57 | + }), |
39 | 58 | ],
|
40 |
| - // ... |
41 | 59 | })
|
42 | 60 | ```
|
43 | 61 |
|
44 |
| -:::tip NOTE |
45 |
| -If you are using Vite, you can do the same thing by specifying the option in the [plugin provided officially](https://github.com/intlify/bundle-tools/tree/main/packages/vite-plugin-vue-i18n). |
| 62 | +#### Configure plugin for webpack |
| 63 | +
|
| 64 | +```js |
| 65 | +// webpack.config.js |
| 66 | +const paht = require('path') |
| 67 | +const VueI18nPlugin = require('@intlify/unplugin-vue-i18n/webpack') |
| 68 | + |
| 69 | +module.exports = { |
| 70 | + /* ... */ |
| 71 | + plugins: [ |
| 72 | + /* ... */ |
| 73 | + VueI18nPlugin({ |
| 74 | + /* options */ |
| 75 | + // locale messages resourece pre-compile option |
| 76 | + include: path.resolve(__dirname, './path/to/src/locales/**'), |
| 77 | + }) |
| 78 | + ] |
| 79 | +} |
| 80 | +``` |
| 81 | +
|
| 82 | +#### More configuration |
| 83 | +
|
| 84 | +About optoins and features, see the deital [page](https://github.com/intlify/bundle-tools/tree/main/packages/unplugin-vue-i18n#intlifyunplugin-vue-i18n) |
| 85 | +
|
| 86 | +### `@intlify/vite-plugin-vue-i18n` |
| 87 | +
|
| 88 | +[`vite`](https://vitejs.dev/) is next generation frontend tooling. |
| 89 | +
|
| 90 | +Intlify project is providing [`@intlify/vite-plugin-vue-i18n`](https://github.com/intlify/bundle-tools/tree/main/packages/vite-plugin-vue-i18n) |
| 91 | +
|
| 92 | +If you do a production build, Vue I18n will automatically bundle the runtime only module |
| 93 | +
|
| 94 | +:::warning NOTICE |
| 95 | +This plugin will be deprecated in the near future, because we can replace `@intlify/unplugin-vue-i18n`. |
46 | 96 | :::
|
47 | 97 |
|
48 |
| -### webpack |
| 98 | +#### Install plugin |
| 99 | +
|
| 100 | +```sh |
| 101 | +npm install --save-dev @intlify/vite-plugin-vue-i18n |
| 102 | +``` |
49 | 103 |
|
50 |
| -In webpack, use `resolve.alias` as below: |
| 104 | +#### Configure |
51 | 105 |
|
52 | 106 | ```js
|
53 |
| -module.exports = { |
54 |
| - // ... |
55 |
| - resolve: { |
56 |
| - alias: { |
57 |
| - 'vue-i18n': 'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js' |
58 |
| - } |
59 |
| - }, |
60 |
| - // ... |
61 |
| -} |
| 107 | +// vite.config.ts |
| 108 | +import { defineConfig } from 'vite' |
| 109 | +import { resolve, dirname } from 'node:path' |
| 110 | +import { fileURLToPath } from 'url' |
| 111 | +import vueI18n from '@intlify/vite-plugin-vue-i18n' |
| 112 | + |
| 113 | +export default defineConfig({ |
| 114 | + /* ... */ |
| 115 | + plugins: [ |
| 116 | + /* ... */ |
| 117 | + vueI18n({ |
| 118 | + /* options */ |
| 119 | + // locale messages resourece pre-compile option |
| 120 | + include: resolve(dirname(fileURLToPath(import.meta.url)), './path/to/src/locales/**'), |
| 121 | + }), |
| 122 | + ] |
| 123 | +}) |
62 | 124 | ```
|
63 | 125 |
|
64 |
| -:::tip NOTE |
65 |
| -For more information about pre-compiling locale messages, see [`@intlify/vue-i18n-loader`](https://github.com/intlify/vue-i18n-loader) |
| 126 | +#### More configuration |
| 127 | +
|
| 128 | +About optoins and features, see the deital [page](https://github.com/intlify/bundle-tools/tree/main/packages/vite-plugin-vue-i18n) |
| 129 | +
|
| 130 | +### `@intlify/vue-i18n-loader` |
| 131 | +
|
| 132 | +[webpack](https://webpack.js.org/) is a static module bundler for modern JavaScript applications. |
| 133 | +
|
| 134 | +Intlify project is providing [`@intlify/vue-i18n-loader`](https://github.com/intlify/bundle-tools/tree/main/packages/vue-i18n-loader) |
| 135 | +
|
| 136 | +:::danger NOTICE |
| 137 | +This plugin will be deprecated in the near future, because we can replace `@intlify/unplugin-vue-i18n`. |
66 | 138 | :::
|
67 | 139 |
|
68 |
| -### rollup |
| 140 | +#### Install loader |
69 | 141 |
|
70 |
| -In rollup, use [`@rollup/plugin-alias`](https://github.com/rollup/plugins/tree/master/packages/alias) as below: |
| 142 | +```sh |
| 143 | +npm install --save-dev @intlify/vue-i18n-loader |
| 144 | +``` |
| 145 | +
|
| 146 | +#### Configure |
71 | 147 |
|
72 | 148 | ```js
|
73 |
| -import path from 'path' |
74 |
| -import alias from '@rollup/plugin-alias' |
| 149 | +// webpack.config.js |
| 150 | +const paht = require('path') |
75 | 151 |
|
76 | 152 | module.exports = {
|
77 |
| - // ... |
78 |
| - plugins: [ |
79 |
| - alias({ |
80 |
| - entries: { |
81 |
| - 'vue-i18n': path.resolve(__dirname, './node_modules/vue-i18n/dist/vue-i18n.runtime.esm-bundler.js') |
82 |
| - } |
83 |
| - }) |
84 |
| - ], |
85 |
| - // ... |
| 153 | + /* ... */ |
| 154 | + resolve: { |
| 155 | + alias: { |
| 156 | + 'vue-i18n': 'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js' |
| 157 | + } |
| 158 | + }, |
| 159 | + /* ... */ |
| 160 | + module: { |
| 161 | + rules: [ |
| 162 | + // ... |
| 163 | + { |
| 164 | + test: /\.(json5?|ya?ml)$/, // target json, json5, yaml and yml files |
| 165 | + type: 'javascript/auto', |
| 166 | + loader: '@intlify/vue-i18n-loader', |
| 167 | + include: [ // Use `Rule.include` to specify the files of locale messages to be pre-compiled |
| 168 | + path.resolve(__dirname, 'src/locales') |
| 169 | + ] |
| 170 | + }, |
| 171 | + // ... |
| 172 | + ] |
| 173 | + } |
86 | 174 | }
|
87 | 175 | ```
|
88 | 176 |
|
| 177 | +#### More configuration |
| 178 | +
|
| 179 | +About optoins and features, see the deital [page](https://github.com/intlify/bundle-tools/tree/main/packages/vue-i18n-loader) |
| 180 | +
|
89 | 181 | ### Quasar CLI
|
90 | 182 |
|
91 | 183 | No need to do anything. [Quasar CLI](https://quasar.dev) takes care of the optimizations for you.
|
|
0 commit comments