WXL
5 天以前 871522ed7e06fd9c62a87c178d7f5c88d7853a20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const generateWithPlugin = require('@vue/cli-test-utils/generateWithPlugin')
 
test('javascript', async () => {
  const projectName = 'vue-i18n-gen-js'
  const { files } = await generateWithPlugin([
    {
      id: '@vue/cli-service',
      apply: () => {},
      options: { projectName }
    },
    {
      id: 'i18n',
      apply: require('../../generator'),
      options: { localeDir: 'locales', locale: 'en' }
    }
  ])
 
  // check files
  const i18n = files['src/i18n.js']
  expect(i18n).toMatch(
    `const locales = require.context('./locales', true, /[A-Za-z0-9-_,\\s]+\\.json$/i)`
  )
  const locale = files['src/locales/en.json']
  expect(locale).toMatch(`{\n  "message": "hello i18n !!"\n}`)
  const pack = files['package.json']
  expect(pack).toMatch(`"vue-i18n": "^8.26.3"`)
  expect(pack).not.toMatch(`"@intlify/vue-i18n-loader": "^1.1.0"`)
})
 
test('typescript', async () => {
  const projectName = 'vue-i18n-gen-ts'
  const { files } = await generateWithPlugin([
    {
      id: '@vue/cli-service',
      apply: () => {},
      options: { projectName }
    },
    {
      id: '@vue/cli-plugin-typescript',
      apply: () => {},
      options: { projectName }
    },
    {
      id: 'i18n',
      apply: require('../../generator'),
      options: { locale: 'ja', localeDir: 'loc', enableInSFC: true }
    }
  ])
 
  // check files
  const i18n = files['src/i18n.ts']
  expect(i18n).toMatch(
    `const locales = require.context('./loc', true, /[A-Za-z0-9-_,\\s]+\\.json$/i)`
  )
  const locale = files['src/loc/ja.json']
  expect(locale).toMatch(`{\n  "message": "hello i18n !!"\n}`)
  const sfc = files['src/components/HelloI18n.vue']
  expect(sfc).toMatch(`export default Vue.extend({`)
  const pack = files['package.json']
  expect(pack).toMatch(`"vue-i18n": "^8.26.3"`)
  expect(pack).toMatch(`"@intlify/vue-i18n-loader": "^1.1.0"`)
})
 
test('bridge', async () => {
  const projectName = 'vue-i18n-gen-ts-bridge'
  const { files } = await generateWithPlugin([
    {
      id: '@vue/cli-service',
      apply: () => {},
      options: { projectName }
    },
    {
      id: '@vue/cli-plugin-typescript',
      apply: () => {},
      options: { projectName }
    },
    {
      id: 'i18n',
      apply: require('../../generator'),
      options: {
        locale: 'ja',
        localeDir: 'loc',
        enableInSFC: true,
        enableBridge: true
      }
    }
  ])
 
  // check files
  const i18n = files['src/i18n.ts']
  expect(i18n).toMatch(
    `const locales = require.context('./loc', true, /[A-Za-z0-9-_,\\s]+\\.json$/i)`
  )
  const locale = files['src/loc/ja.json']
  expect(locale).toMatch(`{\n  "message": "hello i18n !!"\n}`)
  const sfc = files['src/components/HelloI18n.vue']
  expect(sfc).toMatch(`export default Vue.extend({`)
  const pack = files['package.json']
  expect(pack).toMatch(`"vue-i18n": "^8.26.3"`)
  expect(pack).toMatch(`"vue-i18n-bridge": "^9.2.0-beta.10"`)
  expect(pack).toMatch(`"@intlify/vue-i18n-loader": "^3.2.0"`)
})