This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,776 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
console.log('hello, @intlify/vite-plugin-vue-i18n !') | ||
import { friendlyJSONstringify } from 'vue-i18n' | ||
import yaml from 'js-yaml' | ||
import JSON5 from 'json5' | ||
|
||
import { debug as Debug } from 'debug' | ||
const debug = Debug('vite-plugin-vue-i18n') | ||
|
||
type Query = Record<string, string> | ||
|
||
export default function i18n(source: string, query: Query) { | ||
debug('vueSFCTransform: query', JSON.stringify(query)) | ||
|
||
return new Promise<string>(resolve => { | ||
const code = `export default Comp => { | ||
Comp.__i18n = Comp.__i18n || [] | ||
Comp.__i18n.push(${stringify(parse(source.trim(), query), query)}) | ||
}`.trim() | ||
resolve(code) | ||
}) | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function stringify(data: any, query: Query): string { | ||
const { locale } = query | ||
if (locale) { | ||
return friendlyJSONstringify( | ||
Object.assign({}, { [locale as string]: data }) | ||
) | ||
} else { | ||
return friendlyJSONstringify(data) | ||
} | ||
} | ||
|
||
function parse(source: string, query: Query): string { | ||
const value = source.trim() | ||
const { lang } = query | ||
switch (lang) { | ||
case 'yaml': | ||
case 'yml': | ||
return yaml.safeLoad(value) | ||
case 'json5': | ||
return JSON5.parse(value) | ||
default: | ||
return JSON.parse(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<i18n> | ||
{ | ||
"en": { | ||
"hello": "hello world!" | ||
} | ||
} | ||
</i18n> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Component from '~target' | ||
import * as exports from '~target' | ||
|
||
if (typeof window !== 'undefined') { | ||
window.module = Component | ||
window.exports = exports | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<i18n src="./message.json"> | ||
</i18n> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<i18n lang="json5"> | ||
{ | ||
"en": { | ||
// comments | ||
"hello": "hello world!" | ||
} | ||
} | ||
</i18n> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<i18n locale="en" src="./locale.json"> | ||
</i18n> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<i18n> | ||
{ | ||
"en": { | ||
"hello": "hello world!" | ||
} | ||
} | ||
</i18n> | ||
|
||
<i18n locale="ja"> | ||
{ | ||
"hello": "こんにちは、世界!" | ||
} | ||
</i18n> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"hello": "hello world!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<i18n locale="ja"> | ||
{ | ||
"hello": "こんにちは、世界!" | ||
} | ||
</i18n> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"en": { | ||
"hello": "hello world!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<i18n> | ||
{ | ||
"en": { | ||
"hello": "hello world!" | ||
} | ||
} | ||
</i18n> | ||
|
||
<i18n> | ||
{ | ||
"ja": { | ||
"hello": "こんにちは、世界!" | ||
} | ||
} | ||
</i18n> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<i18n> | ||
{ | ||
"en": { | ||
"hello": "hello\ngreat\t\"world\"" | ||
} | ||
} | ||
</i18n> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<i18n lang="yaml"> | ||
en: | ||
hello: "hello world!" | ||
</i18n> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,110 @@ | ||
test('hello', () => { | ||
expect(1 + 1).toBe(2) | ||
import { bundleAndRun } from './utils' | ||
|
||
test('basic', async () => { | ||
const { module } = await bundleAndRun('basic.vue') | ||
expect(module.__i18n).toMatchObject([ | ||
{ | ||
en: { | ||
hello: 'hello world!' | ||
} | ||
} | ||
]) | ||
}) | ||
|
||
test('yaml', async () => { | ||
const { module } = await bundleAndRun('yaml.vue') | ||
expect(module.__i18n).toMatchObject([ | ||
{ | ||
en: { | ||
hello: 'hello world!' | ||
} | ||
} | ||
]) | ||
}) | ||
|
||
test('json5', async () => { | ||
const { module } = await bundleAndRun('json5.vue') | ||
expect(module.__i18n).toMatchObject([ | ||
{ | ||
en: { | ||
hello: 'hello world!' | ||
} | ||
} | ||
]) | ||
}) | ||
|
||
test('import', async () => { | ||
const { module } = await bundleAndRun('import.vue') | ||
expect(module.__i18n).toMatchObject([ | ||
{ | ||
en: { | ||
hello: 'hello world!' | ||
} | ||
} | ||
]) | ||
}) | ||
|
||
test('multiple', async () => { | ||
const { module } = await bundleAndRun('multiple.vue') | ||
expect(module.__i18n).toMatchObject([ | ||
{ | ||
en: { | ||
hello: 'hello world!' | ||
} | ||
}, | ||
{ | ||
ja: { | ||
hello: 'こんにちは、世界!' | ||
} | ||
} | ||
]) | ||
}) | ||
|
||
test('locale', async () => { | ||
const { module } = await bundleAndRun('locale.vue') | ||
expect(module.__i18n).toMatchObject([ | ||
{ | ||
ja: { | ||
hello: 'こんにちは、世界!' | ||
} | ||
} | ||
]) | ||
}) | ||
|
||
test('locale attr and basic', async () => { | ||
const { module } = await bundleAndRun('locale-mix.vue') | ||
expect(module.__i18n).toMatchObject([ | ||
{ | ||
en: { | ||
hello: 'hello world!' | ||
} | ||
}, | ||
{ | ||
ja: { | ||
hello: 'こんにちは、世界!' | ||
} | ||
} | ||
]) | ||
}) | ||
|
||
test('locale attr and import', async () => { | ||
const { module } = await bundleAndRun('locale-import.vue') | ||
expect(module.__i18n).toMatchObject([ | ||
{ | ||
en: { | ||
hello: 'hello world!' | ||
} | ||
} | ||
]) | ||
}) | ||
|
||
test('special characters', async () => { | ||
const { module } = await bundleAndRun('special-char.vue') | ||
expect(module.__i18n).toMatchObject([ | ||
{ | ||
en: { | ||
hello: 'hello\ngreat\t"world"' | ||
} | ||
} | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import path from 'path' | ||
import alias from '@rollup/plugin-alias' | ||
import { build } from 'vite' | ||
import { JSDOM, VirtualConsole } from 'jsdom' | ||
import i18n from '../src/index' | ||
|
||
async function bundle(fixture: string, options: Record<string, unknown> = {}) { | ||
const { assets } = await build({ | ||
emitAssets: false, | ||
emitIndex: false, | ||
write: false, | ||
minify: false, | ||
silent: true, | ||
rollupInputOptions: { | ||
input: path.resolve(__dirname, './fixtures/entry.ts'), | ||
plugins: [ | ||
alias({ | ||
entries: { | ||
'~target': path.resolve(__dirname, './fixtures', fixture) | ||
} | ||
}) | ||
] | ||
}, | ||
vueCustomBlockTransforms: { | ||
i18n | ||
} | ||
}) | ||
return { code: assets[0].code } | ||
} | ||
|
||
export async function bundleAndRun( | ||
fixture: string, | ||
options: Record<string, unknown> = {} | ||
) { | ||
const { code } = await bundle(fixture, options) | ||
|
||
let dom: JSDOM | null = null | ||
let jsdomError | ||
try { | ||
dom = new JSDOM(`<!DOCTYPE html><html><head></head><body></body></html>`, { | ||
runScripts: 'outside-only', | ||
virtualConsole: new VirtualConsole() | ||
}) | ||
dom.window.eval(code) | ||
} catch (e) { | ||
console.error(`JSDOM error:\n${e.stack}`) | ||
jsdomError = e | ||
} | ||
|
||
if (!dom) { | ||
return Promise.reject(new Error('Cannot assigned JSDOM instance')) | ||
} | ||
|
||
const { window } = dom | ||
const { module, exports } = window | ||
|
||
return Promise.resolve({ | ||
window, | ||
module, | ||
exports, | ||
jsdomError | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.