Skip to content
This repository was archived by the owner on Sep 12, 2023. It is now read-only.

Commit 73321bc

Browse files
authored
feat: pre-compilation (#60)
1 parent 8697c9c commit 73321bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3592
-2559
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
'@typescript-eslint/explicit-function-return-type': 'off',
3232
'@typescript-eslint/member-delimiter-style': 'off',
3333
'@typescript-eslint/no-use-before-define': 'off',
34-
'@typescript-eslint/explicit-module-boundary-types': 'off'
34+
'@typescript-eslint/explicit-module-boundary-types': 'off',
35+
'@typescript-eslint/no-non-null-assertion': 'off'
3536
}
3637
}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ coverage
22
node_modules
33
examples/legacy/index.js
44
examples/global/index.js
5-
examples/compsable/index.js
5+
examples/composition/index.js
66
.DS_Store
77
lib
88
*.log

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
lib
22
coverage
33
tsconfig.json
4+
examples/composition/index.js
5+
examples/global/index.js
6+
examples/legacy/index.js

README.md

Lines changed: 88 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![Test](https://github.com/intlify/rollup-plugin-vue-i18n/workflows/Test/badge.svg)
44
[![npm](https://img.shields.io/npm/v/@intlify/rollup-plugin-vue-i18n.svg)](https://www.npmjs.com/package/@intlify/rollup-plugin-vue-i18n)
55

6-
vue-i18n rollup plugin for custom blocks
6+
vue-i18n rollup plugin for i18n resource pre-compilation and custom blocks
77

88
**NOTE:** :warning: This `next` branch is development branch for Vue 3! The stable version is now in [`master`](https://github.com/intlify/rollup-plugin-vue-i18n/tree/master) branch!
99

@@ -35,7 +35,55 @@ $ yarn add -D @intlify/rollup-plugin-vue-i18n@next
3535

3636
## :rocket: Usages
3737

38-
the below example that `examples/composable/App.vue` have `i18n` custom block:
38+
### i18n resource pre-compilation
39+
40+
Since [email protected], 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.
41+
42+
However, with the message compiler, the javascript function conversion will not work in some environments (e.g. CSP). For this reason, [email protected] and later offer a full version that includes compiler and runtime, and a runtime only version.
43+
44+
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`.
45+
46+
#### Rollup Config
47+
48+
The below rollup configi example:
49+
50+
```js
51+
import VuePlugin from 'rollup-plugin-vue'
52+
import VueI18nPlugin from 'rollup-plugin-vue-i18n'
53+
import resolve from '@rollup/plugin-node-resolve'
54+
import commonjs from '@rollup/plugin-commonjs'
55+
import path from 'path'
56+
57+
export default [
58+
{
59+
input: path.resolve(__dirname, `./path/to/src/main.js`),
60+
output: {
61+
file: path.resolve(__dirname, `./path/to/dist/index.js`),
62+
format: 'cjs'
63+
},
64+
plugins: [
65+
// set `customBlocks` opton to `rollup-plugin-vue`
66+
VuePlugin({ customBlocks: ['i18n'] }),
67+
// set `rollup-plugin-vue-i18n` after **`rollup-plugin-vue`**
68+
VueI18nPlugin({
69+
// `include` option for i18n resources bundling
70+
include: path.resolve(__dirname, `./path/to/src/locales/**`)
71+
}),
72+
resolve(),
73+
commonjs()
74+
]
75+
}
76+
]
77+
```
78+
79+
#### Notes on using with other i18n resource loading plugins
80+
81+
If you use the plugin like `@rollup/plugin-json`, make sure that the i18n resource to be precompiled with `rollup-plugin-vue-i18n` is not loaded. you need to filter with the plugin options.
82+
83+
84+
### `i18n` custom block
85+
86+
the below example that `examples/composition/App.vue` have `i18n` custom block:
3987

4088
```vue
4189
<template>
@@ -47,18 +95,23 @@ the below example that `examples/composable/App.vue` have `i18n` custom block:
4795
</select>
4896
</form>
4997
<p>{{ t('hello') }}</p>
98+
<Banana />
5099
</template>
51100
52101
<script>
53102
import { useI18n } from 'vue-i18n'
103+
import Banana from './Banana.vue'
54104
55105
export default {
56106
name: 'App',
107+
components: {
108+
Banana
109+
},
57110
setup() {
58111
const { t, locale } = useI18n({
59-
inheritLocale: true
112+
inheritLocale: true,
113+
useScope: 'local'
60114
})
61-
62115
return { t, locale }
63116
}
64117
}
@@ -79,37 +132,6 @@ export default {
79132
80133
```
81134

82-
If you would like to bundle as common library with rollup, you can configure the following for ES Module:
83-
84-
```js
85-
import vue from 'rollup-plugin-vue'
86-
import replace from '@rollup/plugin-replace'
87-
import resolve from '@rollup/plugin-node-resolve'
88-
import commonjs from '@rollup/plugin-commonjs'
89-
import i18n from '@intlify/rollup-plugin-vue-i18n'
90-
import path from 'path'
91-
92-
export default [
93-
{
94-
input: path.resolve(__dirname, `./examples/composable/main.js`),
95-
output: {
96-
file: path.resolve(__dirname, `./examples/composable/index.js`),
97-
format: 'cjs'
98-
},
99-
plugins: [
100-
commonjs(),
101-
resolve(),
102-
replace({
103-
'process.env.NODE_ENV': JSON.stringify('production')
104-
}),
105-
i18n(),
106-
vue({ customBlocks: ['i18n'] })
107-
]
108-
}
109-
]
110-
111-
```
112-
113135
### Locale Messages formatting
114136

115137
You can be used by specifying the following format in the `lang` attribute:
@@ -129,6 +151,37 @@ ja:
129151
</i18n>
130152
```
131153

154+
### `forceStringify` options
155+
156+
Whether pre-compile number and boolean values as message functions that return the string value, default `false`
157+
158+
```ts
159+
import VuePlugin from 'rollup-plugin-vue'
160+
import VueI18nPlugin from 'rollup-plugin-vue-i18n'
161+
import resolve from '@rollup/plugin-node-resolve'
162+
import commonjs from '@rollup/plugin-commonjs'
163+
import path from 'path'
164+
165+
export default [
166+
{
167+
input: path.resolve(__dirname, `./src/main.js`),
168+
output: {
169+
file: path.resolve(__dirname, `./dist/index.js`),
170+
format: 'cjs'
171+
},
172+
plugins: [
173+
VuePlugin({ customBlocks: ['i18n'] }),
174+
VueI18nPlugin({
175+
include: path.resolve(__dirname, `./path/to/src/locales/**`)
176+
// `forceStringify` option
177+
forceStringify: true
178+
}),
179+
resolve(),
180+
commonjs()
181+
]
182+
}
183+
]
184+
```
132185

133186
## :scroll: Changelog
134187
Details changes for each release are documented in the [CHANGELOG.md](https://github.com/intlify/rollup-plugin-vue-i18n/blob/master/CHANGELOG.md).

e2e/example.test.js

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,64 @@
1-
;['composable', 'legacy', 'globe'].forEach(pattern => {
2-
describe(`${pattern}`, () => {
3-
beforeAll(async () => {
4-
await page.goto(`http://localhost:8080/${pattern}/`)
5-
})
6-
7-
test('initial rendering', async () => {
8-
await expect(page).toMatch('言語')
9-
await expect(page).toMatch('こんにちは、世界!')
10-
})
11-
12-
test('change locale', async () => {
13-
await page.select('#app select', 'en')
14-
await expect(page).toMatch('Language')
15-
await expect(page).toMatch('hello, world!')
16-
})
1+
describe('composition', () => {
2+
beforeAll(async () => {
3+
await page.goto(`http://localhost:8080/composition/`)
4+
})
5+
6+
test('initial rendering', async () => {
7+
await expect(page).toMatch('言語')
8+
await expect(page).toMatch('こんにちは、世界!')
9+
await expect(page).toMatch('バナナが欲しい?')
10+
})
11+
12+
test('change locale', async () => {
13+
await page.select('#app select', 'en')
14+
await expect(page).toMatch('Language')
15+
await expect(page).toMatch('hello, world!')
16+
})
17+
18+
test('change select', async () => {
19+
await page.select('#fruits select', '2')
20+
await expect(page).toMatch('バナナ 2 個')
21+
})
22+
})
23+
24+
describe('legacy', () => {
25+
beforeAll(async () => {
26+
await page.goto(`http://localhost:8080/legacy/`)
27+
})
28+
29+
test('initial rendering', async () => {
30+
await expect(page).toMatch('言語')
31+
await expect(page).toMatch('こんにちは、世界!')
32+
await expect(page).toMatch('バナナが欲しい?')
33+
})
34+
35+
test('change select', async () => {
36+
await page.select('#fruits select', '2')
37+
await expect(page).toMatch('バナナ 2 個')
38+
})
39+
40+
test('change locale', async () => {
41+
await page.select('#app select', 'en')
42+
await expect(page).toMatch('Language')
43+
await expect(page).toMatch('hello, world!')
44+
await expect(page).toMatch('Do you want banana?')
45+
await expect(page).toMatch('2 bananas')
46+
})
47+
})
48+
49+
describe('global', () => {
50+
beforeAll(async () => {
51+
await page.goto(`http://localhost:8080/global/`)
52+
})
53+
54+
test('initial rendering', async () => {
55+
await expect(page).toMatch('言語')
56+
await expect(page).toMatch('こんにちは、世界!')
57+
})
58+
59+
test('change locale', async () => {
60+
await page.select('#app select', 'en')
61+
await expect(page).toMatch('Language')
62+
await expect(page).toMatch('hello, world!')
1763
})
1864
})

examples/composable/App.vue renamed to examples/composition/App.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
</select>
88
</form>
99
<p>{{ t('hello') }}</p>
10+
<Banana />
1011
</template>
1112

1213
<script>
1314
import { useI18n } from 'vue-i18n'
15+
import Banana from './Banana.vue'
16+
1417
export default {
1518
name: 'App',
19+
components: {
20+
Banana
21+
},
1622
setup() {
1723
const { t, locale } = useI18n({
18-
inheritLocale: true
24+
inheritLocale: true,
25+
useScope: 'local'
1926
})
20-
2127
return { t, locale }
2228
}
2329
}

examples/composition/Banana.vue

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<form id="fruits">
3+
<label>{{ t('select') }}</label>
4+
<select v-model.number="select">
5+
<option value="0">0</option>
6+
<option value="1">1</option>
7+
<option value="2">2</option>
8+
<option value="3">3</option>
9+
</select>
10+
</form>
11+
<p>{{ t('fruits.banana', select, { n: select }) }}</p>
12+
</template>
13+
14+
<script>
15+
import { ref } from 'vue'
16+
import { useI18n } from 'vue-i18n'
17+
18+
export default {
19+
name: 'Banana',
20+
setup() {
21+
const { t } = useI18n({ useScope: 'global' })
22+
const select = ref(0)
23+
return { t, select }
24+
}
25+
}
26+
</script>
File renamed without changes.

0 commit comments

Comments
 (0)