Skip to content

Commit af8db09

Browse files
authored
Vue 3 support (#140)
* Update packages * Migrate to vue compiler sfc * Update parse types * Fix locale type * Update demo * Upgrade dependecies
1 parent e51d052 commit af8db09

File tree

11 files changed

+9061
-124
lines changed

11 files changed

+9061
-124
lines changed

demo/package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
"i18n:report": "vue-cli-service i18n:report --src './src/**/*.?(js|vue)' --locales './src/locales/**/*.json'"
1010
},
1111
"dependencies": {
12-
"core-js": "^3.0.0",
13-
"vue": "^2.6.10",
14-
"vue-i18n": "^8.14.0"
12+
"core-js": "^3.6.5",
13+
"vue": "^3.0.11",
14+
"vue-i18n": "^9.1.4"
1515
},
1616
"devDependencies": {
17-
"@kazupon/vue-i18n-loader": "^0.5.0",
18-
"@vue/cli-plugin-babel": "^4.0.0",
19-
"@vue/cli-plugin-eslint": "^4.0.0",
20-
"@vue/cli-service": "^4.0.0",
17+
"@intlify/vue-i18n-loader": "^2.1.0",
18+
"@vue/cli-plugin-babel": "^4.5.12",
19+
"@vue/cli-plugin-eslint": "^4.5.12",
20+
"@vue/cli-service": "^4.5.12",
21+
"@vue/compiler-sfc": "^3.0.11",
2122
"babel-eslint": "^10.0.1",
2223
"eslint": "^6.0.0",
2324
"eslint-plugin-vue": "^6.0.0",
24-
"vue-cli-plugin-i18n": "^1.0.0",
25-
"vue-template-compiler": "^2.6.10"
25+
"vue-cli-plugin-i18n": "^2.1.1"
2626
},
2727
"eslintConfig": {
2828
"root": true,

demo/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h1>{{ $t("title") }}</h1>
44
<form>
55
<label for="locale">{{ $t('lang') }}:</label>
6-
<select v-model="$parent.$i18n.locale">
6+
<select v-model="$i18n.locale">
77
<option value="en">en</option>
88
<option value="ja">ja</option>
99
</select>

demo/src/i18n.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import Vue from 'vue'
2-
import VueI18n from 'vue-i18n'
3-
4-
Vue.use(VueI18n)
1+
import { createI18n } from 'vue-i18n'
52

63
function loadLocaleMessages () {
74
const locales = require.context('./locales', true, /[A-Za-z0-9-_,\s]+\.json$/i)
@@ -16,7 +13,7 @@ function loadLocaleMessages () {
1613
return messages
1714
}
1815

19-
export default new VueI18n({
16+
export default createI18n({
2017
locale: process.env.VUE_APP_I18N_LOCALE || 'en',
2118
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'ja',
2219
silentFallbackWarn: true,

demo/src/main.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import Vue from 'vue'
1+
import { createApp } from 'vue'
22
import App from './App.vue'
33
import i18n from './i18n'
44

5-
Vue.config.productionTip = false
5+
const app = createApp(App)
66

7-
new Vue({
8-
i18n,
9-
render: h => h(App)
10-
}).$mount('#app')
7+
app.use(i18n)
8+
app.mount('#app')

demo/yarn.lock

Lines changed: 8865 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
}
2626
},
2727
"dependencies": {
28-
"@vue/component-compiler-utils": "^3.0.0",
2928
"debug": "^4.1.1",
3029
"deep-diff": "^1.0.2",
3130
"deepmerge": "^4.2.2",
@@ -34,7 +33,6 @@
3433
"js-yaml": "^3.13.1",
3534
"json-diff": "^0.5.4",
3635
"json5": "^2.1.0",
37-
"vue-template-compiler": "^2.6.10",
3836
"yargs": "^15.1.0"
3937
},
4038
"devDependencies": {
@@ -52,6 +50,7 @@
5250
"@sucrase/jest-plugin": "^2.1.0",
5351
"@typescript-eslint/eslint-plugin": "^3.0.0",
5452
"@typescript-eslint/parser": "^3.0.0",
53+
"@vue/compiler-sfc": "^3.0.11",
5554
"eslint": "^7.4.0",
5655
"eslint-plugin-vue-libs": "^4.0.0",
5756
"jest": "^26.0.0",
@@ -63,8 +62,7 @@
6362
"tsd": "^0.13.0",
6463
"typescript": "^3.9.5",
6564
"typescript-eslint-language-service": "^3.0.0",
66-
"vue": "^2.6.11",
67-
"vue-template-compiler": "^2.6.11"
65+
"vue": "^3.0.11"
6866
},
6967
"engines": {
7068
"node": ">= 12"
@@ -84,8 +82,8 @@
8482
"license": "MIT",
8583
"main": "lib/index.js",
8684
"peerDependencies": {
87-
"vue": "^2.6",
88-
"vue-template-compiler": "^2.6"
85+
"@vue/compiler-sfc": "^3.0.11",
86+
"vue": "^3.0.11"
8987
},
9088
"repository": {
9189
"type": "git",

src/infuser.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable-next-line */
2-
/// <reference path="../types/shims-vue-template-compiler.d.ts"/>
3-
import { SFCBlock, SFCDescriptor } from 'vue-template-compiler'
2+
/// <reference path="../types/shims-vue-compiler-sfc.d.ts"/>
3+
import { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
44
import { Locale, MetaLocaleMessage, SFCI18nBlock, SFCFileInfo, FormatOptions } from '../types'
55

66
import { escape, reflectSFCDescriptor, parseContent, stringifyContent } from './utils'
@@ -24,7 +24,7 @@ function generate (meta: MetaLocaleMessage, descriptor: SFCDescriptor, options?:
2424
debug('target i18n blocks\n', i18nBlocks)
2525

2626
const blocks: SFCBlock[] = getBlocks(descriptor)
27-
blocks.forEach(b => debug(`block: type=${b.type}, start=${b.start}, end=${b.end}`))
27+
blocks.forEach(b => debug(`block: type=${b.type}, start=${b.loc.start.offset}, end=${b.loc.end.offset}`))
2828

2929
const { raw } = descriptor
3030
const content = buildContent(i18nBlocks, raw, blocks, options)
@@ -39,7 +39,9 @@ function getBlocks (descriptor: SFCDescriptor): SFCBlock[] {
3939
const blocks: SFCBlock[] = [...styles, ...customBlocks]
4040
template && blocks.push(template as SFCBlock)
4141
script && blocks.push(script as SFCBlock)
42-
blocks.sort((a, b) => { return (a.start as number) - (b.start as number) })
42+
blocks.sort((a, b) => {
43+
return a.loc.start.offset - b.loc.start.offset
44+
})
4345
return blocks
4446
}
4547

@@ -52,7 +54,7 @@ function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock
5254
if (block.type === 'i18n') {
5355
let lang = block.attrs.lang
5456
lang = (!lang || typeof lang !== 'string') ? 'json' : lang
55-
const locale: Locale | undefined = block.attrs.locale
57+
const locale: Locale | true = block.attrs.locale
5658
const i18nBlock = i18nBlocks[i18nBlockCounter]
5759
debug(`meta.lang = ${i18nBlock.lang}, block.lang = ${lang}, meta.locale = ${i18nBlock.locale}, block.locale = ${locale}`)
5860

@@ -68,14 +70,14 @@ function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock
6870
messages = parseContent(block.content, lang)
6971
}
7072

71-
contents = contents.concat(raw.slice(offset, block.start))
73+
contents = contents.concat(raw.slice(offset, block.loc.start.offset))
7274
const serialized = `\n${stringifyContent(messages, lang, options)}`
7375
contents = contents.concat(serialized)
74-
offset = block.end as number
76+
offset = block.loc.end.offset
7577
i18nBlockCounter++
7678
} else {
77-
contents = contents.concat(raw.slice(offset, block.end))
78-
offset = block.end as number
79+
contents = contents.concat(raw.slice(offset, block.loc.end.offset))
80+
offset = block.loc.end.offset
7981
}
8082
return contents
8183
}, contents)

src/squeezer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SFCBlock } from 'vue-template-compiler'
1+
import { SFCBlock } from '@vue/compiler-sfc'
22
import { MetaLocaleMessage, I18nLang, SFCFileInfo, SFCI18nBlock } from '../types'
33

44
import { reflectSFCDescriptor, parseContent } from './utils'
@@ -9,7 +9,9 @@ const debug = Debug('vue-i18n-locale-message:squeezer')
99
export default function squeeze (basePath: string, files: SFCFileInfo[]): MetaLocaleMessage {
1010
const descriptors = reflectSFCDescriptor(basePath, files)
1111
return descriptors.reduce((meta, descriptor) => {
12-
descriptor.customBlocks.sort((a, b) => { return (a.start as number) - (b.start as number) })
12+
descriptor.customBlocks.sort((a, b) => {
13+
return a.loc.start.offset - b.loc.start.offset
14+
})
1315
const i18nBlocks = squeezeFromCustomBlocks(descriptor.customBlocks)
1416
debug('squeezeFromCustomBlocks: i18nBlocks', JSON.stringify(i18nBlocks, null, 2))
1517
meta.components[descriptor.contentPath] = i18nBlocks

src/utils.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// import types
22
import { Arguments } from 'yargs'
3-
import { SFCDescriptor } from 'vue-template-compiler'
4-
import { VueTemplateCompiler } from '@vue/component-compiler-utils/dist/types'
3+
import { SFCDescriptor } from '@vue/compiler-sfc'
54
import {
65
SFCFileInfo,
76
Locale,
@@ -19,8 +18,7 @@ import {
1918
} from '../types'
2019

2120
// import modules
22-
import { parse } from '@vue/component-compiler-utils'
23-
import * as compiler from 'vue-template-compiler'
21+
import { parse } from '@vue/compiler-sfc'
2422
import fs from 'fs'
2523
import glob from 'glob'
2624
import path from 'path'
@@ -68,18 +66,13 @@ export function isLocaleMessageDictionary (message: LocaleMessage): message is L
6866

6967
export function reflectSFCDescriptor (basePath: string, components: SFCFileInfo[]): SFCDescriptor[] {
7068
return components.map(target => {
71-
const { template, script, styles, customBlocks } = parse({
72-
source: target.content,
73-
filename: target.path,
74-
compiler: compiler as VueTemplateCompiler
75-
}) as SFCDescriptor
69+
const descriptor = parse(target.content, {
70+
filename: target.path
71+
}).descriptor as SFCDescriptor
7672
return {
7773
...parsePath(basePath, target.path),
78-
raw: target.content,
79-
customBlocks,
80-
template,
81-
script,
82-
styles
74+
...descriptor,
75+
raw: target.content
8376
}
8477
})
8578
}

types/shims-vue-template-compiler.d.ts renamed to types/shims-vue-compiler-sfc.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { SFCDescriptor } from 'vue-template-compiler'
1+
import { SFCDescriptor } from '@vue/compiler-sfc'
22

33
// extend for vue-i18n-locale-message
4-
declare module 'vue-template-compiler/types/index' {
4+
declare module '@vue/compiler-sfc' {
55
export interface SFCDescriptor {
66
raw: string
77
contentPath: string

0 commit comments

Comments
 (0)