@intlify/unplugin-vue-i18n 0.1.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +221 -7
- package/lib/index.cjs +78 -1
- package/lib/index.mjs +79 -2
- package/lib/types.d.ts +3 -0
- package/messages.d.ts +5 -0
- package/package.json +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
|
|
2
|
+
## @intlify/unplugin-vue-i18n@0.3.0 (2022-07-07)
|
|
3
|
+
|
|
4
|
+
#### :star: Features
|
|
5
|
+
* [#136](https://github.com/intlify/bundle-tools/pull/136) feat(unplugin-vue-i18n): support fullinstall option ([@kazupon](https://github.com/kazupon))
|
|
6
|
+
* [#135](https://github.com/intlify/bundle-tools/pull/135) feat(unplugin-vue-i18n): support composition only option ([@kazupon](https://github.com/kazupon))
|
|
7
|
+
* [#134](https://github.com/intlify/bundle-tools/pull/134) feat(unplugin-vue-i18n): support runtimeOnly option ([@kazupon](https://github.com/kazupon))
|
|
8
|
+
|
|
9
|
+
#### :pencil: Documentation
|
|
10
|
+
* [#133](https://github.com/intlify/bundle-tools/pull/133) docs: consistent plugin name and add Nuxt ([@Atinux](https://github.com/Atinux))
|
|
11
|
+
|
|
12
|
+
#### Committers: 2
|
|
13
|
+
- Sébastien Chopin ([@Atinux](https://github.com/Atinux))
|
|
14
|
+
- kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## @intlify/unplugin-vue-i18n@0.2.1 (2022-05-07)
|
|
18
|
+
|
|
19
|
+
#### :bug: Bug Fixes
|
|
20
|
+
* [#118](https://github.com/intlify/bundle-tools/pull/118) fix(unplugin-vue-i18n): code transforming strictly ([@kazupon](https://github.com/kazupon))
|
|
21
|
+
|
|
22
|
+
#### Committers: 1
|
|
23
|
+
- kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
## @intlify/unplugin-vue-i18n@0.2.0 (2022-05-06)
|
|
27
|
+
|
|
28
|
+
#### :star: Features
|
|
29
|
+
* [#117](https://github.com/intlify/bundle-tools/pull/117) feat: support virtual resource importing ([@kazupon](https://github.com/kazupon))
|
|
30
|
+
|
|
31
|
+
#### Committers: 1
|
|
32
|
+
- kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
|
|
33
|
+
|
|
34
|
+
|
|
2
35
|
## @intlify/unplugin-vue-i18n@0.1.2 (2022-05-06)
|
|
3
36
|
|
|
4
37
|
#### :bug: Bug Fixes
|
package/README.md
CHANGED
|
@@ -28,11 +28,11 @@ npm i @intlify/unplugin-vue-i18n
|
|
|
28
28
|
|
|
29
29
|
```ts
|
|
30
30
|
// vite.config.ts
|
|
31
|
-
import
|
|
31
|
+
import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
|
|
32
32
|
|
|
33
33
|
export default defineConfig({
|
|
34
34
|
plugins: [
|
|
35
|
-
|
|
35
|
+
VueI18nPlugin({ /* options */ }),
|
|
36
36
|
],
|
|
37
37
|
})
|
|
38
38
|
```
|
|
@@ -43,7 +43,7 @@ export default defineConfig({
|
|
|
43
43
|
<summary>Webpack</summary><br>
|
|
44
44
|
|
|
45
45
|
```ts
|
|
46
|
-
const VueI18nPlugin = require('unplugin-vue-i18n/webpack')
|
|
46
|
+
const VueI18nPlugin = require('@intlify/unplugin-vue-i18n/webpack')
|
|
47
47
|
|
|
48
48
|
// webpack.config.js
|
|
49
49
|
module.exports = {
|
|
@@ -56,6 +56,187 @@ module.exports = {
|
|
|
56
56
|
|
|
57
57
|
<br></details>
|
|
58
58
|
|
|
59
|
+
<details>
|
|
60
|
+
<summary>Nuxt</summary><br>
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
// nuxt.config.ts
|
|
64
|
+
import { defineNuxtConfig } from 'nuxt'
|
|
65
|
+
import VueI18nPlugin from '@intlify/unplugin-vue-i18n'
|
|
66
|
+
|
|
67
|
+
export default defineNuxtConfig({
|
|
68
|
+
vite: {
|
|
69
|
+
plugins: [
|
|
70
|
+
VueI18nPlugin.vite({ /* options */ }),
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
// When using Webpack
|
|
74
|
+
// builder: '@nuxt/webpack-builder',
|
|
75
|
+
webpack: {
|
|
76
|
+
plugins: [
|
|
77
|
+
VueI18nPlugin.webpack({ /* options */ }),
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
<br></details>
|
|
84
|
+
|
|
85
|
+
## 🚀 Usage
|
|
86
|
+
|
|
87
|
+
### i18n resources pre-compilation
|
|
88
|
+
|
|
89
|
+
Since `vue-i18n@v9.x`, the locale messages are handled with message compiler, which converts them to javascript functions after compiling. After compiling, message compiler converts them into javascript functions, which can improve the performance of the application.
|
|
90
|
+
|
|
91
|
+
However, with the message compiler, the javascript function conversion will not work in some environments (e.g. CSP). For this reason, `vue-i18n@v9.x` and later offer a full version that includes compiler and runtime, and a runtime only version.
|
|
92
|
+
|
|
93
|
+
If you are using the runtime version, you will need to compile before importing locale messages by managing them in a file such as `.json`.
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
### i18n custom block
|
|
97
|
+
|
|
98
|
+
The below example that `examples/vite/src/App.vue` have `i18n` custom block:
|
|
99
|
+
|
|
100
|
+
```vue
|
|
101
|
+
<template>
|
|
102
|
+
<form>
|
|
103
|
+
<label>{{ t('language') }}</label>
|
|
104
|
+
<select v-model="locale">
|
|
105
|
+
<option value="en">en</option>
|
|
106
|
+
<option value="ja">ja</option>
|
|
107
|
+
</select>
|
|
108
|
+
</form>
|
|
109
|
+
<p>{{ t('hello') }}</p>
|
|
110
|
+
<Banana />
|
|
111
|
+
</template>
|
|
112
|
+
|
|
113
|
+
<script>
|
|
114
|
+
import { useI18n } from 'vue-i18n'
|
|
115
|
+
import Banana from './Banana.vue'
|
|
116
|
+
|
|
117
|
+
export default {
|
|
118
|
+
name: 'App',
|
|
119
|
+
components: {
|
|
120
|
+
Banana
|
|
121
|
+
},
|
|
122
|
+
setup() {
|
|
123
|
+
const { t, locale } = useI18n({
|
|
124
|
+
inheritLocale: true,
|
|
125
|
+
useScope: 'local'
|
|
126
|
+
})
|
|
127
|
+
return { t, locale }
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
</script>
|
|
131
|
+
|
|
132
|
+
<i18n>
|
|
133
|
+
{
|
|
134
|
+
"en": {
|
|
135
|
+
"language": "Language",
|
|
136
|
+
"hello": "hello, world!"
|
|
137
|
+
},
|
|
138
|
+
"ja": {
|
|
139
|
+
"language": "言語",
|
|
140
|
+
"hello": "こんにちは、世界!"
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
</i18n>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Locale Messages formatting
|
|
147
|
+
|
|
148
|
+
You can be used by specifying the following format in the `lang` attribute:
|
|
149
|
+
|
|
150
|
+
- json (default)
|
|
151
|
+
- yaml
|
|
152
|
+
- yml
|
|
153
|
+
- json5
|
|
154
|
+
|
|
155
|
+
example `yaml` format:
|
|
156
|
+
|
|
157
|
+
```vue
|
|
158
|
+
<i18n lang="yaml">
|
|
159
|
+
en:
|
|
160
|
+
hello: 'Hello World!'
|
|
161
|
+
ja:
|
|
162
|
+
hello: 'こんにちは、世界!'
|
|
163
|
+
</i18n>
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Static bundle importing
|
|
167
|
+
|
|
168
|
+
unplugin-vue-i18n allows you to statically bundle i18n resources such as `json` or `yaml` specified by the [`include` option](#include) of the plugin described below as locale messages with the `import` syntax.
|
|
169
|
+
|
|
170
|
+
In this case, only one i18n resource can be statically bundled at a time with `import` syntax, so the these code will be redundant for multiple locales.
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
import { createApp } from 'vue'
|
|
174
|
+
import { createI18n } from 'vue-i18n'
|
|
175
|
+
/*
|
|
176
|
+
* The i18n resources in the path specified in the plugin `include` option can be read
|
|
177
|
+
* as vue-i18n optimized locale messages using the import syntax
|
|
178
|
+
*/
|
|
179
|
+
import en from './src/locales/en.json'
|
|
180
|
+
import ja from './src/locales/ja.yaml'
|
|
181
|
+
import fr from './src/locales/fr.json5'
|
|
182
|
+
|
|
183
|
+
const i18n = createI18n({
|
|
184
|
+
locale: 'en',
|
|
185
|
+
messages: {
|
|
186
|
+
en,
|
|
187
|
+
ja,
|
|
188
|
+
fr
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
const app = createApp()
|
|
193
|
+
app.use(i18n).mount('#app')
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
unplugin-vue-i18n can use the bundler virtual mechanism to import all locales at once, using the special identifier `@intlify/unplugin-vue-i18n/messages`, as the bellow:
|
|
197
|
+
|
|
198
|
+
```js
|
|
199
|
+
import { createApp } from 'vue'
|
|
200
|
+
import { createI18n } from 'vue-i18n'
|
|
201
|
+
/*
|
|
202
|
+
* All i18n resources specified in the plugin `include` option can be loaded
|
|
203
|
+
* at once using the import syntax
|
|
204
|
+
*/
|
|
205
|
+
import messages from '@intlify/unplugin-vue-i18n/messages'
|
|
206
|
+
|
|
207
|
+
const i18n = createI18n({
|
|
208
|
+
locale: 'en',
|
|
209
|
+
messages
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
const app = createApp()
|
|
213
|
+
app.use(i18n).mount('#app')
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Types
|
|
217
|
+
|
|
218
|
+
If you want type definition of `@intlify/unplugin-vue-i18n/messages`, add `unplugin-vue-i18n/messages` to `compilerOptions.types` of your tsconfig:
|
|
219
|
+
|
|
220
|
+
```json
|
|
221
|
+
{
|
|
222
|
+
"compilerOptions": {
|
|
223
|
+
"types": ["@intlify/unplugin-vue-i18n/messages"]
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
## 📦 Automatic bundling
|
|
230
|
+
### For Vue I18n
|
|
231
|
+
As noted [here](https://vue-i18n.intlify.dev/installation.html#explanation-of-different-builds), NPM provides many different builds of Vue I18n.
|
|
232
|
+
|
|
233
|
+
This plugin will automatically select and bundle Vue I18n build according to the following behavior:
|
|
234
|
+
|
|
235
|
+
- development: `vue-i18n.esm-bundler.js`
|
|
236
|
+
- production: `vue-i18n.runtime.esm-bundler.js`
|
|
237
|
+
|
|
238
|
+
About details, See the [here](https://vue-i18n.intlify.dev/guide/advanced/optimization.html#improve-performance-and-reduce-bundle-size-with-runtime-build-only)
|
|
239
|
+
|
|
59
240
|
|
|
60
241
|
## 🔧 Options
|
|
61
242
|
|
|
@@ -73,7 +254,43 @@ module.exports = {
|
|
|
73
254
|
- yml
|
|
74
255
|
```
|
|
75
256
|
|
|
76
|
-
Note `json` resources matches this option, it will be handled **before the internal json plugin of
|
|
257
|
+
Note `json` resources matches this option, it will be handled **before the internal json plugin of bundler, and will not be processed afterwards**, else the option doesn't match, the bundler side will handle.
|
|
258
|
+
|
|
259
|
+
### `runtimeOnly`
|
|
260
|
+
|
|
261
|
+
- **Type:** `boolean`
|
|
262
|
+
- **Default:** `true`
|
|
263
|
+
|
|
264
|
+
Whether or not to automatically use Vue I18n **runtime-only** in production build, set `vue-i18n.runtime.esm-bundler.js` in the `vue-i18n` field of bundler config, the below:
|
|
265
|
+
|
|
266
|
+
```
|
|
267
|
+
- vite config: `resolve.alias`
|
|
268
|
+
- webpack config: `resolve.alias`
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
If `false` is specified, Vue I18n (vue-i18n) package.json `module` field will be used.
|
|
272
|
+
|
|
273
|
+
For more details, See [here](#-automatic-bundling)
|
|
274
|
+
|
|
275
|
+
### `compositionOnly`
|
|
276
|
+
|
|
277
|
+
- **Type:** `boolean`
|
|
278
|
+
- **Default:** `true`
|
|
279
|
+
|
|
280
|
+
Whether to make vue-i18n API only composition API. **By default the legacy API is tree-shaken.**
|
|
281
|
+
|
|
282
|
+
For more details, See [here](https://vue-i18n.intlify.dev/guide/advanced/optimization.html#reduce-bundle-size-with-feature-build-flags)
|
|
283
|
+
|
|
284
|
+
### `fullInstall`
|
|
285
|
+
|
|
286
|
+
- **Type:** `boolean`
|
|
287
|
+
- **Default:** `true`
|
|
288
|
+
|
|
289
|
+
Whether to install the full set of APIs, components, etc. provided by Vue I18n. By default, all of them will be installed.
|
|
290
|
+
|
|
291
|
+
If `false` is specified, **buld-in components and directive will not be installed in vue and will be tree-shaken.**
|
|
292
|
+
|
|
293
|
+
For more details, See [here](https://vue-i18n.intlify.dev/guide/advanced/optimization.html#reduce-bundle-size-with-feature-build-flags)
|
|
77
294
|
|
|
78
295
|
### `forceStringify`
|
|
79
296
|
|
|
@@ -206,10 +423,7 @@ module.exports = {
|
|
|
206
423
|
|
|
207
424
|
|
|
208
425
|
## ✅ TODO
|
|
209
|
-
- [ ] Virtual resource importing
|
|
210
426
|
- [ ] Bundling optimizations
|
|
211
|
-
- [ ] i18n custom block birdge mode (for vite)
|
|
212
|
-
- [ ] Support nuxt
|
|
213
427
|
|
|
214
428
|
## 📜 Changelog
|
|
215
429
|
|
package/lib/index.cjs
CHANGED
|
@@ -116,12 +116,46 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
|
|
|
116
116
|
const useClassComponent = !!options.useClassComponent;
|
|
117
117
|
const bridge = !!options.bridge;
|
|
118
118
|
debug("bridge", bridge);
|
|
119
|
+
const runtimeOnly = shared.isBoolean(options.runtimeOnly) ? options.runtimeOnly : true;
|
|
120
|
+
debug("runtimeOnly", runtimeOnly);
|
|
121
|
+
const compositionOnly = shared.isBoolean(options.compositionOnly) ? options.compositionOnly : true;
|
|
122
|
+
debug("compositionOnly", compositionOnly);
|
|
123
|
+
const fullInstall = shared.isBoolean(options.fullInstall) ? options.fullInstall : true;
|
|
124
|
+
debug("fullInstall", fullInstall);
|
|
119
125
|
let isProduction = false;
|
|
120
126
|
let sourceMap = false;
|
|
121
127
|
return {
|
|
122
128
|
name: "unplugin-vue-i18n",
|
|
123
129
|
enforce: meta.framework === "vite" ? "pre" : "post",
|
|
130
|
+
transformInclude(id) {
|
|
131
|
+
debug("transformInclude", id);
|
|
132
|
+
if (meta.framework === "vite") {
|
|
133
|
+
return true;
|
|
134
|
+
} else {
|
|
135
|
+
const { filename } = parseVueRequest(id);
|
|
136
|
+
return filename.endsWith("vue") || filename.endsWith(INTLIFY_BUNDLE_IMPORT_ID) || filename.endsWith(INTLIFY_BUNDLE_IMPORT_DEPRECTED_ID) ? true : /\.(json5?|ya?ml)$/.test(id) && filter(id);
|
|
137
|
+
}
|
|
138
|
+
},
|
|
124
139
|
vite: {
|
|
140
|
+
config(config, { command }) {
|
|
141
|
+
normalizeConfigResolveAlias(config.resolve, meta.framework);
|
|
142
|
+
if (command === "build" && runtimeOnly) {
|
|
143
|
+
if (shared.isArray(config.resolve.alias)) {
|
|
144
|
+
config.resolve.alias.push({
|
|
145
|
+
find: "vue-i18n",
|
|
146
|
+
replacement: `vue-i18n/dist/vue-i18n.runtime.esm-bundler.js`
|
|
147
|
+
});
|
|
148
|
+
} else if (shared.isObject(config.resolve.alias)) {
|
|
149
|
+
config.resolve.alias["vue-i18n"] = `vue-i18n/dist/vue-i18n.runtime.esm-bundler.js`;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
config.define = config.define || {};
|
|
153
|
+
config.define["__VUE_I18N_LEGACY_API__"] = !compositionOnly;
|
|
154
|
+
debug(`set __VUE_I18N_LEGACY_API__ is '${config.define["__VUE_I18N_LEGACY_API__"]}'`);
|
|
155
|
+
config.define["__VUE_I18N_FULL_INSTALL__"] = fullInstall;
|
|
156
|
+
debug(`set __VUE_I18N_FULL_INSTALL__ is '${config.define["__VUE_I18N_FULL_INSTALL__"]}'`);
|
|
157
|
+
config.define["__VUE_I18N_PROD_DEVTOOLS__"] = false;
|
|
158
|
+
},
|
|
125
159
|
configResolved(config) {
|
|
126
160
|
isProduction = config.isProduction;
|
|
127
161
|
sourceMap = config.command === "build" ? !!config.build.sourcemap : false;
|
|
@@ -147,6 +181,23 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
|
|
|
147
181
|
isProduction = compiler.options.mode !== "development";
|
|
148
182
|
sourceMap = !!compiler.options.devtool;
|
|
149
183
|
debug(`webpack: isProduction = ${isProduction}, sourceMap = ${sourceMap}`);
|
|
184
|
+
if (isProduction && runtimeOnly) {
|
|
185
|
+
normalizeConfigResolveAlias(compiler.options.resolve, meta.framework);
|
|
186
|
+
compiler.options.resolve.alias["vue-i18n"] = `vue-i18n/dist/vue-i18n.runtime.esm-bundler.js`;
|
|
187
|
+
}
|
|
188
|
+
loadWebpack().then((webpack) => {
|
|
189
|
+
if (webpack) {
|
|
190
|
+
compiler.options.plugins.push(new webpack.DefinePlugin({
|
|
191
|
+
__VUE_I18N_LEGACY_API__: JSON.stringify(compositionOnly),
|
|
192
|
+
__VUE_I18N_FULL_INSTALL__: JSON.stringify(fullInstall),
|
|
193
|
+
__INTLIFY_PROD_DEVTOOLS__: "false"
|
|
194
|
+
}));
|
|
195
|
+
debug(`set __VUE_I18N_LEGACY_API__ is '${compositionOnly}'`);
|
|
196
|
+
debug(`set __VUE_I18N_FULL_INSTALL__ is '${fullInstall}'`);
|
|
197
|
+
} else {
|
|
198
|
+
debug("ignore vue-i18n feature flags with webpack.DefinePlugin");
|
|
199
|
+
}
|
|
200
|
+
});
|
|
150
201
|
if (compiler.options.module) {
|
|
151
202
|
compiler.options.module.rules.push({
|
|
152
203
|
test: /\.(json5?|ya?ml)$/,
|
|
@@ -189,7 +240,6 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
|
|
|
189
240
|
async transform(code, id) {
|
|
190
241
|
const { filename, query } = parseVueRequest(id);
|
|
191
242
|
debug("transform", id, JSON.stringify(query), filename);
|
|
192
|
-
debug("transform (code)", code);
|
|
193
243
|
let langInfo = defaultSFCLang;
|
|
194
244
|
let inSourceMap;
|
|
195
245
|
if (!query.vue) {
|
|
@@ -251,6 +301,33 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
|
|
|
251
301
|
}
|
|
252
302
|
};
|
|
253
303
|
});
|
|
304
|
+
function normalizeConfigResolveAlias(resolve, framework) {
|
|
305
|
+
if (resolve && resolve.alias) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (!resolve) {
|
|
309
|
+
if (framework === "vite") {
|
|
310
|
+
resolve = { alias: [] };
|
|
311
|
+
} else if (framework === "webpack") {
|
|
312
|
+
resolve = { alias: {} };
|
|
313
|
+
}
|
|
314
|
+
} else if (!resolve.alias) {
|
|
315
|
+
if (framework === "vite") {
|
|
316
|
+
resolve.alias = [];
|
|
317
|
+
} else if (framework === "webpack") {
|
|
318
|
+
resolve.alias = {};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
async function loadWebpack() {
|
|
323
|
+
let webpack = null;
|
|
324
|
+
try {
|
|
325
|
+
webpack = await import('webpack').then((m) => m.default || m);
|
|
326
|
+
} catch (e) {
|
|
327
|
+
console.warn(`[@intlify/unplugin-vue-i18n] webpack not found, please install webpack.`);
|
|
328
|
+
}
|
|
329
|
+
return webpack;
|
|
330
|
+
}
|
|
254
331
|
async function generateBundleResources(resources, query, isProduction, {
|
|
255
332
|
forceStringify = false,
|
|
256
333
|
isGlobal = false,
|
package/lib/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { createUnplugin } from 'unplugin';
|
|
|
2
2
|
import { normalize, parse } from 'pathe';
|
|
3
3
|
import createDebug from 'debug';
|
|
4
4
|
import fg from 'fast-glob';
|
|
5
|
-
import { isString, isArray, isEmptyObject, isNumber, assign } from '@intlify/shared';
|
|
5
|
+
import { isString, isArray, isBoolean, isObject, isEmptyObject, isNumber, assign } from '@intlify/shared';
|
|
6
6
|
import { createFilter } from '@rollup/pluginutils';
|
|
7
7
|
import { generateJSON, generateYAML } from '@intlify/bundle-utils';
|
|
8
8
|
import { parse as parse$1 } from '@vue/compiler-sfc';
|
|
@@ -105,12 +105,46 @@ const unplugin = createUnplugin((options = {}, meta) => {
|
|
|
105
105
|
const useClassComponent = !!options.useClassComponent;
|
|
106
106
|
const bridge = !!options.bridge;
|
|
107
107
|
debug("bridge", bridge);
|
|
108
|
+
const runtimeOnly = isBoolean(options.runtimeOnly) ? options.runtimeOnly : true;
|
|
109
|
+
debug("runtimeOnly", runtimeOnly);
|
|
110
|
+
const compositionOnly = isBoolean(options.compositionOnly) ? options.compositionOnly : true;
|
|
111
|
+
debug("compositionOnly", compositionOnly);
|
|
112
|
+
const fullInstall = isBoolean(options.fullInstall) ? options.fullInstall : true;
|
|
113
|
+
debug("fullInstall", fullInstall);
|
|
108
114
|
let isProduction = false;
|
|
109
115
|
let sourceMap = false;
|
|
110
116
|
return {
|
|
111
117
|
name: "unplugin-vue-i18n",
|
|
112
118
|
enforce: meta.framework === "vite" ? "pre" : "post",
|
|
119
|
+
transformInclude(id) {
|
|
120
|
+
debug("transformInclude", id);
|
|
121
|
+
if (meta.framework === "vite") {
|
|
122
|
+
return true;
|
|
123
|
+
} else {
|
|
124
|
+
const { filename } = parseVueRequest(id);
|
|
125
|
+
return filename.endsWith("vue") || filename.endsWith(INTLIFY_BUNDLE_IMPORT_ID) || filename.endsWith(INTLIFY_BUNDLE_IMPORT_DEPRECTED_ID) ? true : /\.(json5?|ya?ml)$/.test(id) && filter(id);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
113
128
|
vite: {
|
|
129
|
+
config(config, { command }) {
|
|
130
|
+
normalizeConfigResolveAlias(config.resolve, meta.framework);
|
|
131
|
+
if (command === "build" && runtimeOnly) {
|
|
132
|
+
if (isArray(config.resolve.alias)) {
|
|
133
|
+
config.resolve.alias.push({
|
|
134
|
+
find: "vue-i18n",
|
|
135
|
+
replacement: `vue-i18n/dist/vue-i18n.runtime.esm-bundler.js`
|
|
136
|
+
});
|
|
137
|
+
} else if (isObject(config.resolve.alias)) {
|
|
138
|
+
config.resolve.alias["vue-i18n"] = `vue-i18n/dist/vue-i18n.runtime.esm-bundler.js`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
config.define = config.define || {};
|
|
142
|
+
config.define["__VUE_I18N_LEGACY_API__"] = !compositionOnly;
|
|
143
|
+
debug(`set __VUE_I18N_LEGACY_API__ is '${config.define["__VUE_I18N_LEGACY_API__"]}'`);
|
|
144
|
+
config.define["__VUE_I18N_FULL_INSTALL__"] = fullInstall;
|
|
145
|
+
debug(`set __VUE_I18N_FULL_INSTALL__ is '${config.define["__VUE_I18N_FULL_INSTALL__"]}'`);
|
|
146
|
+
config.define["__VUE_I18N_PROD_DEVTOOLS__"] = false;
|
|
147
|
+
},
|
|
114
148
|
configResolved(config) {
|
|
115
149
|
isProduction = config.isProduction;
|
|
116
150
|
sourceMap = config.command === "build" ? !!config.build.sourcemap : false;
|
|
@@ -136,6 +170,23 @@ const unplugin = createUnplugin((options = {}, meta) => {
|
|
|
136
170
|
isProduction = compiler.options.mode !== "development";
|
|
137
171
|
sourceMap = !!compiler.options.devtool;
|
|
138
172
|
debug(`webpack: isProduction = ${isProduction}, sourceMap = ${sourceMap}`);
|
|
173
|
+
if (isProduction && runtimeOnly) {
|
|
174
|
+
normalizeConfigResolveAlias(compiler.options.resolve, meta.framework);
|
|
175
|
+
compiler.options.resolve.alias["vue-i18n"] = `vue-i18n/dist/vue-i18n.runtime.esm-bundler.js`;
|
|
176
|
+
}
|
|
177
|
+
loadWebpack().then((webpack) => {
|
|
178
|
+
if (webpack) {
|
|
179
|
+
compiler.options.plugins.push(new webpack.DefinePlugin({
|
|
180
|
+
__VUE_I18N_LEGACY_API__: JSON.stringify(compositionOnly),
|
|
181
|
+
__VUE_I18N_FULL_INSTALL__: JSON.stringify(fullInstall),
|
|
182
|
+
__INTLIFY_PROD_DEVTOOLS__: "false"
|
|
183
|
+
}));
|
|
184
|
+
debug(`set __VUE_I18N_LEGACY_API__ is '${compositionOnly}'`);
|
|
185
|
+
debug(`set __VUE_I18N_FULL_INSTALL__ is '${fullInstall}'`);
|
|
186
|
+
} else {
|
|
187
|
+
debug("ignore vue-i18n feature flags with webpack.DefinePlugin");
|
|
188
|
+
}
|
|
189
|
+
});
|
|
139
190
|
if (compiler.options.module) {
|
|
140
191
|
compiler.options.module.rules.push({
|
|
141
192
|
test: /\.(json5?|ya?ml)$/,
|
|
@@ -178,7 +229,6 @@ const unplugin = createUnplugin((options = {}, meta) => {
|
|
|
178
229
|
async transform(code, id) {
|
|
179
230
|
const { filename, query } = parseVueRequest(id);
|
|
180
231
|
debug("transform", id, JSON.stringify(query), filename);
|
|
181
|
-
debug("transform (code)", code);
|
|
182
232
|
let langInfo = defaultSFCLang;
|
|
183
233
|
let inSourceMap;
|
|
184
234
|
if (!query.vue) {
|
|
@@ -240,6 +290,33 @@ const unplugin = createUnplugin((options = {}, meta) => {
|
|
|
240
290
|
}
|
|
241
291
|
};
|
|
242
292
|
});
|
|
293
|
+
function normalizeConfigResolveAlias(resolve, framework) {
|
|
294
|
+
if (resolve && resolve.alias) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (!resolve) {
|
|
298
|
+
if (framework === "vite") {
|
|
299
|
+
resolve = { alias: [] };
|
|
300
|
+
} else if (framework === "webpack") {
|
|
301
|
+
resolve = { alias: {} };
|
|
302
|
+
}
|
|
303
|
+
} else if (!resolve.alias) {
|
|
304
|
+
if (framework === "vite") {
|
|
305
|
+
resolve.alias = [];
|
|
306
|
+
} else if (framework === "webpack") {
|
|
307
|
+
resolve.alias = {};
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
async function loadWebpack() {
|
|
312
|
+
let webpack = null;
|
|
313
|
+
try {
|
|
314
|
+
webpack = await import('webpack').then((m) => m.default || m);
|
|
315
|
+
} catch (e) {
|
|
316
|
+
console.warn(`[@intlify/unplugin-vue-i18n] webpack not found, please install webpack.`);
|
|
317
|
+
}
|
|
318
|
+
return webpack;
|
|
319
|
+
}
|
|
243
320
|
async function generateBundleResources(resources, query, isProduction, {
|
|
244
321
|
forceStringify = false,
|
|
245
322
|
isGlobal = false,
|
package/lib/types.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
declare type SFCLangFormat = 'json' | 'json5' | 'yml' | 'yaml';
|
|
2
2
|
interface PluginOptions {
|
|
3
3
|
include?: string | string[];
|
|
4
|
+
runtimeOnly?: boolean;
|
|
5
|
+
compositionOnly?: boolean;
|
|
6
|
+
fullInstall?: boolean;
|
|
4
7
|
forceStringify?: boolean;
|
|
5
8
|
defaultSFCLang?: SFCLangFormat;
|
|
6
9
|
globalSFCScope?: boolean;
|
package/messages.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlify/unplugin-vue-i18n",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "unplugin for Vue I18n",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
@@ -79,6 +79,9 @@
|
|
|
79
79
|
"./types": {
|
|
80
80
|
"types": "./types.d.ts"
|
|
81
81
|
},
|
|
82
|
+
"./messages": {
|
|
83
|
+
"types": "./messages.d.ts"
|
|
84
|
+
},
|
|
82
85
|
"./lib/*": "./lib/*",
|
|
83
86
|
"./package.json": "./package.json"
|
|
84
87
|
},
|