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

Commit 821e483

Browse files
authored
feat: core implementation (#1)
* feat: core implementation * fix lint errors * add codecov badge * add more tests * remove unneccesary modules * tweak tests * add examples * update docs
1 parent d009510 commit 821e483

File tree

14 files changed

+1615
-47
lines changed

14 files changed

+1615
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
coverage
22
node_modules
3+
example/components.esm.js
34
.DS_Store
45
lib
56
*.log

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
coverage
66
__snapshots__
77
test
8+
example/components.esm.js
89
.vscode

README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,129 @@
11
# :globe_with_meridians: rollup-plugin-vue-i18n
22

33
[![CircleCI](https://circleci.com/gh/intlify/rollup-plugin-vue-i18n.svg?style=svg)](https://circleci.com/gh/intlify/rollup-plugin-vue-i18n)
4+
[![npm](https://img.shields.io/npm/v/@intlify/rollup-plugin-vue-i18n.svg)](https://www.npmjs.com/package/@intlify/rollup-plugin-vue-i18n)
5+
[![codecov](https://codecov.io/gh/intlify/rollup-plugin-vue-i18n/branch/master/graph/badge.svg)](https://codecov.io/gh/intlify/rollup-plugin-vue-i18n)
46

57
vue-i18n rollup plugin for custom blocks
68

9+
10+
## :exclamation: Requirement
11+
12+
You need to install the follwoing:
13+
14+
15+
16+
If you use rollup-plugin-vue, We recommend you should read the [docs](https://rollup-plugin-vue.vuejs.org/)
17+
18+
19+
## :cd: Installation
20+
21+
npm:
22+
```bash
23+
$ npm i --save-dev @rollup/plugin-json
24+
$ npm i --save-dev @rollup/plugin-yaml # if you use locale messages with YAML format
25+
$ npm i --save-dev @intlify/rollup-plugin-vue-i18n
26+
```
27+
28+
yarn:
29+
```bash
30+
$ yarn add -D @rollup/plugin-json
31+
$ yarn add -D @rollup/plugin-yaml # if you use locale messages with YAML format
32+
$ yarn add -D @intlify/rollup-plugin-vue-i18n
33+
```
34+
35+
## :rocket: Usages
36+
37+
the below example that `example/Hello.vue` have `i18n` custom block:
38+
39+
```vue
40+
<template>
41+
<p>{{ $t('hello') }}</p>
42+
</template>
43+
44+
<script>
45+
export default {
46+
name: 'Hello'
47+
}
48+
</script>
49+
50+
<i18n>
51+
{
52+
"en": {
53+
"hello": "Hello World!"
54+
},
55+
"ja": {
56+
"hello": "こんにちは、世界!"
57+
}
58+
}
59+
</i18n>
60+
```
61+
62+
If you would like to bundle as common library with rollup, you can configure the following for ES Module:
63+
64+
```js
65+
const vue = require('rollup-plugin-vue')
66+
const yaml = require('@rollup/plugin-yaml')
67+
const json = require('@rollup/plugin-json')
68+
const { default: i18n } = require('../lib/index')
69+
70+
export default {
71+
input: './example/index.js',
72+
output: {
73+
format: 'esm',
74+
file: './example/components.esm.js'
75+
},
76+
external: [
77+
// Externalize so that the output code is readable.
78+
'vue',
79+
'vue-runtime-helpers',
80+
'vue-i18n'
81+
],
82+
plugins: [
83+
yaml(),
84+
json(),
85+
i18n(),
86+
vue({
87+
customBlocks: ['i18n']
88+
})
89+
]
90+
}
91+
```
92+
93+
### Locale Messages formatting
94+
95+
You can be used by specifying the following format in the `lang` attribute:
96+
97+
- json (default)
98+
- yaml
99+
100+
example `yaml` foramt:
101+
102+
```vue
103+
<i18n lang="yaml">
104+
en:
105+
hello: "Hello World!"
106+
ja:
107+
hello: "こんにちは、世界!"
108+
</i18n>
109+
```
110+
111+
112+
## :warning: Limitations
113+
Currently, There are the following limitations:
114+
115+
- Not support `json5` format
116+
- Not support `locale` attributes
117+
118+
119+
## :scroll: Changelog
120+
Details changes for each release are documented in the [CHANGELOG.md](https://github.com/intlify/rollup-plugin-vue-i18n/blob/master/CHANGELOG.md).
121+
122+
123+
## :exclamation: Issues
124+
Please make sure to read the [Issue Reporting Checklist](https://github.com/inlitify/rollup-plugin-vue-i18n/blob/master/.github/CONTRIBUTING.md#issue-reporting-guidelines) before opening an issue. Issues not conforming to the guidelines may be closed immediately.
125+
126+
7127
## :copyright: License
8128

9129
[MIT](http://opensource.org/licenses/MIT)

example/Hello.vue

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<p>{{ $t('hello') }}</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'Hello'
8+
}
9+
</script>
10+
11+
<i18n>
12+
{
13+
"en": {
14+
"hello": "Hello World!"
15+
},
16+
"ja": {
17+
"hello": "こんにちは、世界!"
18+
}
19+
}
20+
</i18n>

example/HelloYaml.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template>
2+
<p>{{ $t('hello') }}</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'HelloYaml'
8+
}
9+
</script>
10+
11+
<i18n lang="yaml">
12+
en:
13+
hello: "Hello World!"
14+
ja:
15+
hello: "こんにちは、世界!"
16+
</i18n>

example/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Hello from './Hello.vue'
2+
import HelloYaml from './HelloYaml.vue'
3+
4+
export {
5+
Hello,
6+
HelloYaml
7+
}

example/rollup.config.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const vue = require('rollup-plugin-vue')
2+
const yaml = require('@rollup/plugin-yaml')
3+
const json = require('@rollup/plugin-json')
4+
const { default: i18n } = require('../lib/index')
5+
6+
export default {
7+
input: './example/index.js',
8+
output: {
9+
format: 'esm',
10+
file: './example/components.esm.js'
11+
},
12+
external: [
13+
// Externalize so that the output code is readable.
14+
'vue',
15+
'vue-runtime-helpers',
16+
'vue-i18n'
17+
],
18+
plugins: [
19+
yaml(),
20+
json(),
21+
i18n(),
22+
vue({
23+
customBlocks: ['i18n']
24+
})
25+
]
26+
}

index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,31 @@
2424
}
2525
},
2626
"devDependencies": {
27+
"@rollup/plugin-json": "^4.0.1",
28+
"@rollup/plugin-yaml": "^2.0.0",
29+
"@types/debug": "^4.1.5",
2730
"@types/jest": "^24.0.25",
2831
"@types/node": "^13.1.4",
2932
"@typescript-eslint/eslint-plugin": "^2.14.0",
3033
"@typescript-eslint/parser": "^2.14.0",
3134
"@typescript-eslint/typescript-estree": "^2.14.0",
35+
"debug": "^4.1.1",
3236
"eslint": "^6.8.0",
3337
"eslint-plugin-vue-libs": "^4.0.0",
3438
"jest": "^24.9.0",
3539
"jest-watch-typeahead": "^0.4.2",
3640
"lerna-changelog": "^1.0.0",
3741
"opener": "^1.5.1",
38-
"shipjs": "0.13.1",
42+
"rollup": "^1.28.0",
43+
"rollup-plugin-vue": "^5.1.4",
44+
"shipjs": "^0.13.1",
3945
"ts-jest": "^24.2.0",
4046
"typescript": "^3.7.4",
41-
"typescript-eslint-language-service": "^2.0.3"
47+
"typescript-eslint-language-service": "^2.0.3",
48+
"vue-template-compiler": "^2.6.11"
49+
},
50+
"peerDependencies": {
51+
"vue-template-compiler": "^2.6"
4252
},
4353
"engines": {
4454
"node": ">= 10"
@@ -59,8 +69,9 @@
5969
},
6070
"scripts": {
6171
"build": "tsc -p .",
72+
"build:example": "npm run build && rollup -c ./example/rollup.config.js",
6273
"build:watch": "tsc -p . --watch",
63-
"clean": "rm -rf ./coverage",
74+
"clean": "rm -rf ./coverage && rm -rf ./example/app.js",
6475
"coverage": "opener coverage/lcov-report/index.html",
6576
"lint": "eslint ./src ./test --ext .ts",
6677
"release:prepare": "shipjs prepare",

src/index.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1-
export default function (a: number, b: number) {
2-
return a + b
1+
import { Plugin } from 'rollup'
2+
3+
import { debug as Debug } from 'debug'
4+
const debug = Debug('rollup-plugin-vue-i18n')
5+
6+
export default function i18n (): Plugin {
7+
return {
8+
name: 'rollup-plugin-vue-i18n',
9+
transform (source: string, id: string) {
10+
debug('transform id', id)
11+
if (/rollup-plugin-vue=i18n/i.test(id)) {
12+
return {
13+
code:
14+
`${source.replace(/export default/, 'const __i18n =')}` +
15+
`export default function i18n(Component) {\n` +
16+
` const options = typeof Component === 'function' ? Component.options : Component\n` +
17+
` options.__i18n = options.__i18n || []\n` +
18+
` options.__i18n.push(JSON.stringify(__i18n))\n` +
19+
`}`,
20+
map: {
21+
mappings: ''
22+
}
23+
}
24+
}
25+
}
26+
}
327
}

0 commit comments

Comments
 (0)