Skip to content

Commit 2544d06

Browse files
authored
Add testing of example projects (#10)
1 parent e301525 commit 2544d06

22 files changed

+313
-216
lines changed

.github/workflows/test.yml

+21
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,24 @@ jobs:
5050

5151
- name: Lint
5252
run: pnpm lint
53+
54+
test-examples:
55+
runs-on: ubuntu-latest
56+
name: Test build of example projects
57+
steps:
58+
- name: Checkout unplugin-fluent-vue
59+
uses: actions/[email protected]
60+
- name: Checkout examples
61+
uses: actions/[email protected]
62+
with:
63+
repository: fluent-vue/examples
64+
path: examples
65+
- name: Setup Node.js
66+
uses: actions/[email protected]
67+
- uses: pnpm/[email protected]
68+
with:
69+
version: 7.x.x
70+
run_install: true
71+
72+
- name: Test
73+
run: node scripts/build-examples.mjs

__tests__/fixtures/blockType.vue

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<template>
2+
{{ $t('test-key') }}
3+
</template>
4+
5+
<i18n locale="en">
6+
test-key = test-value
7+
</i18n>

__tests__/frameworks/vite/.npmrc

-1
This file was deleted.

__tests__/frameworks/vite/src/__snapshots__/external.spec.ts.snap renamed to __tests__/frameworks/vite/__snapshots__/external.spec.ts.snap

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
// Vitest Snapshot v1
22

33
exports[`Vite external > works 1`] = `
4-
"import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/components/external.vue\\");import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
4+
"=== /fixtures/components/external.vue ===
5+
import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/components/external.vue\\");import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
56
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
67
const _sfc_main = {}
7-
import { toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"/@id/vue\\"
8+
const _toDisplayString = window['Vue'].toDisplayString
9+
const _createElementVNode = window['Vue'].createElementVNode
10+
const _openBlock = window['Vue'].openBlock
11+
const _createElementBlock = window['Vue'].createElementBlock
12+
813
914
function _sfc_render(_ctx, _cache) {
1015
return (_openBlock(), _createElementBlock(\\"div\\", null, [
@@ -48,11 +53,24 @@ _sfc_main.fluent['da'] = da_ftl
4853
}
4954
})
5055
}
51-
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render],['__file',\\"/fixtures/components/external.vue\\"]])"
56+
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render],['__file',\\"/fixtures/components/external.vue\\"]])
57+
58+
=== /fixtures/ftl/da/components/external.vue.ftl ===
59+
60+
import { FluentResource } from '.vite-plugin-externals/@fluent/bundle.js'
61+
export default new FluentResource(\\"\\")
62+
63+
64+
=== /fixtures/ftl/en/components/external.vue.ftl ===
65+
66+
import { FluentResource } from '.vite-plugin-externals/@fluent/bundle.js'
67+
export default new FluentResource(\\"# Simple things are simple.\\\\nhello-user = Hello, {$userName}!\\\\n\\\\n# Complex things are possible.\\\\nshared-photos =\\\\n {$userName} {$photoCount ->\\\\n [one] added one photo\\\\n *[other] added {$photoCount} new photo\\\\n }to {$userGender ->\\\\n [male] his stream\\\\n [female] her stream\\\\n *[other] their stream\\\\n }.\\\\n\\")
68+
"
5269
`;
5370
5471
exports[`Vite external > works with script setup 1`] = `
55-
"import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/components/external.setup.vue\\");import en_ftl from '/fixtures/ftl/en/components/external.setup.vue.ftl?import';
72+
"=== /fixtures/components/external.setup.vue ===
73+
import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/components/external.setup.vue\\");import en_ftl from '/fixtures/ftl/en/components/external.setup.vue.ftl?import';
5674
const _sfc_main = {
5775
__name: 'external.setup',
5876
setup(__props, { expose }) {
@@ -66,7 +84,11 @@ return __returned__
6684
}
6785
6886
}
69-
import { toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"/@id/vue\\"
87+
const _toDisplayString = window['Vue'].toDisplayString
88+
const _createElementVNode = window['Vue'].createElementVNode
89+
const _openBlock = window['Vue'].openBlock
90+
const _createElementBlock = window['Vue'].createElementBlock
91+
7092
7193
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
7294
return (_openBlock(), _createElementBlock(\\"div\\", null, [
@@ -108,5 +130,11 @@ if (__HOT_API__) {
108130
}
109131
})
110132
}
111-
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render],['__file',\\"/fixtures/components/external.setup.vue\\"]])"
133+
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render],['__file',\\"/fixtures/components/external.setup.vue\\"]])
134+
135+
=== /fixtures/ftl/en/components/external.setup.vue.ftl ===
136+
137+
import { FluentResource } from '.vite-plugin-externals/@fluent/bundle.js'
138+
export default new FluentResource(\\"# Simple things are simple.\\\\nhello-user = Hello, {$userName}!\\\\n\\\\n# Complex things are possible.\\\\nshared-photos =\\\\n {$userName} {$photoCount ->\\\\n [one] added one photo\\\\n *[other] added {$photoCount} new photo\\\\n } to {$userGender ->\\\\n [male] his stream\\\\n [female] her stream\\\\n *[other] their stream\\\\n }.\\\\n\\")
139+
"
112140
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`Vite SFC > generates custom block code 1`] = `
4+
"=== /fixtures/test.vue ===
5+
import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/test.vue\\");const _sfc_main = {}
6+
const _toDisplayString = window['Vue'].toDisplayString
7+
const _createElementVNode = window['Vue'].createElementVNode
8+
const _openBlock = window['Vue'].openBlock
9+
const _createElementBlock = window['Vue'].createElementBlock
10+
11+
12+
function _sfc_render(_ctx, _cache) {
13+
return (_openBlock(), _createElementBlock(\\"div\\", null, [
14+
_createElementVNode(\\"div\\", null, _toDisplayString(_ctx.$t('hello-user', { userName: _ctx.userName })), 1 /* TEXT */),
15+
_createElementVNode(\\"div\\", null, _toDisplayString(_ctx.$t('shared-photos', { userName: _ctx.userName, photoCount: _ctx.photoCount, userGender: _ctx.userGender })), 1 /* TEXT */)
16+
]))
17+
}
18+
19+
import block0 from \\"/fixtures/test.vue?vue&type=fluent&index=0&locale=en&lang.fluent\\"
20+
if (typeof block0 === 'function') block0(_sfc_main)
21+
22+
_sfc_main.__hmrId = \\"7e4b934c\\"
23+
typeof __VUE_HMR_RUNTIME__ !== 'undefined' && __VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)
24+
import.meta.hot.accept(mod => {
25+
if (!mod) return
26+
const { default: updated, _rerender_only } = mod
27+
if (_rerender_only) {
28+
__VUE_HMR_RUNTIME__.rerender(updated.__hmrId, updated.render)
29+
} else {
30+
__VUE_HMR_RUNTIME__.reload(updated.__hmrId, updated)
31+
}
32+
})
33+
import _export_sfc from '/@id/__x00__plugin-vue:export-helper'
34+
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render],['__file',\\"/fixtures/test.vue\\"]])
35+
36+
=== /fixtures/test.vue?vue&type=fluent&index=0&locale=en&lang.fluent ===
37+
38+
const FluentResource = window['FluentBundle'].FluentResource
39+
40+
41+
export default function (Component) {
42+
const target = Component.options || Component
43+
target.fluent = target.fluent || {}
44+
target.fluent['en'] = new FluentResource(\\"# Simple things are simple.\\\\nhello-user = Hello, {$userName}!\\\\n\\\\n# Complex things are possible.\\\\nshared-photos =\\\\n {$userName} {$photoCount ->\\\\n [one] added one photo\\\\n *[other] added {$photoCount} new photos\\\\n } to {$userGender ->\\\\n [male] his stream\\\\n [female] her stream\\\\n *[other] their stream\\\\n }.\\")
45+
}
46+
"
47+
`;
48+
49+
exports[`Vite SFC > supports custom blockType 1`] = `
50+
"=== /fixtures/blockType.vue ===
51+
import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/blockType.vue\\");const _sfc_main = {}
52+
const _toDisplayString = window['Vue'].toDisplayString
53+
54+
55+
function _sfc_render(_ctx, _cache) {
56+
return _toDisplayString(_ctx.$t('test-key'))
57+
}
58+
59+
import block0 from \\"/fixtures/blockType.vue?vue&type=i18n&index=0&locale=en&lang.i18n\\"
60+
if (typeof block0 === 'function') block0(_sfc_main)
61+
62+
_sfc_main.__hmrId = \\"8c542073\\"
63+
typeof __VUE_HMR_RUNTIME__ !== 'undefined' && __VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)
64+
import.meta.hot.accept(mod => {
65+
if (!mod) return
66+
const { default: updated, _rerender_only } = mod
67+
if (_rerender_only) {
68+
__VUE_HMR_RUNTIME__.rerender(updated.__hmrId, updated.render)
69+
} else {
70+
__VUE_HMR_RUNTIME__.reload(updated.__hmrId, updated)
71+
}
72+
})
73+
import _export_sfc from '/@id/__x00__plugin-vue:export-helper'
74+
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render],['__file',\\"/fixtures/blockType.vue\\"]])
75+
76+
=== /fixtures/blockType.vue?vue&type=i18n&index=0&locale=en&lang.i18n ===
77+
78+
const FluentResource = window['FluentBundle'].FluentResource
79+
80+
81+
export default function (Component) {
82+
const target = Component.options || Component
83+
target.fluent = target.fluent || {}
84+
target.fluent['en'] = new FluentResource(\\"test-key = test-value\\")
85+
}
86+
"
87+
`;

__tests__/frameworks/vite/src/external.spec.ts renamed to __tests__/frameworks/vite/external.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { describe, expect, it } from 'vitest'
44
import vue3base from '@vitejs/plugin-vue'
55
import compiler from '@vue/compiler-sfc'
66

7-
import { ExternalFluentPlugin } from '../../../../src/vite'
7+
import { ExternalFluentPlugin } from '../../../src/vite'
88
import { compile } from './util'
99

1010
const vue3 = () => vue3base({
1111
compiler,
1212
})
1313

14-
const baseDir = resolve(__dirname, '../../..')
14+
const baseDir = resolve(__dirname, '../..')
1515

1616
describe('Vite external', () => {
1717
it('works', async () => {

__tests__/frameworks/vite/package.json

-10
This file was deleted.

__tests__/frameworks/vite/src/sfc.spec.ts renamed to __tests__/frameworks/vite/sfc.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { describe, expect, it } from 'vitest'
22

33
import vue3base from '@vitejs/plugin-vue'
4-
import compiler from '@vue/compiler-sfc'
54

6-
import { SFCFluentPlugin } from '../../../../src/vite'
5+
import compiler from '@vue/compiler-sfc'
6+
import { SFCFluentPlugin } from '../../../src/vite'
77
import { compile } from './util'
88

99
const vue3 = () => vue3base({
@@ -32,7 +32,7 @@ describe('Vite SFC', () => {
3232
plugins: [
3333
vue3(),
3434
SFCFluentPlugin({
35-
blockType: 'custom',
35+
blockType: 'i18n',
3636
}),
3737
],
3838
}, '/fixtures/blockType.vue')

__tests__/frameworks/vite/src/__snapshots__/sfc.spec.ts.snap

-32
This file was deleted.

__tests__/frameworks/vite/src/util.ts

-33
This file was deleted.

__tests__/frameworks/vite/util.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { resolve, sep } from 'path'
2+
3+
import type { InlineConfig, ModuleNode } from 'vite'
4+
import { createServer } from 'vite'
5+
6+
import { viteExternalsPlugin } from 'vite-plugin-externals'
7+
8+
const baseDir = resolve(__dirname, '../..')
9+
10+
export async function compile(options: InlineConfig, file: string): Promise<string | undefined> {
11+
const vite = await createServer({
12+
root: baseDir,
13+
...options,
14+
plugins: [
15+
...options.plugins,
16+
viteExternalsPlugin({
17+
'vue': 'Vue',
18+
'@fluent/bundle': 'FluentBundle',
19+
}),
20+
],
21+
})
22+
23+
await vite.transformRequest(file)
24+
25+
const module = await vite.moduleGraph.getModuleByUrl(file)
26+
27+
const getAllModules = (module: ModuleNode): ModuleNode[] => [module].concat([...module.importedModules.values()].flatMap(getAllModules))
28+
29+
const modules = await Promise.all(getAllModules(module)
30+
.map(async module => ({
31+
transform: await vite.transformRequest(module.url),
32+
module,
33+
})))
34+
35+
const code = modules
36+
.filter(module => module.transform)
37+
.filter(module => !module.module.url.includes('node_modules'))
38+
.map(module => `=== ${module.module.url} ===\n${module.transform.code}`).join('\n\n')
39+
40+
// normalize paths
41+
return code
42+
?.replaceAll(baseDir.replaceAll(sep, '/'), '')
43+
.replace(/\/@(fs|id).*?node_modules\//g, '')
44+
.replaceAll('\\r\\n', '\\n')
45+
}

__tests__/frameworks/webpack/.npmrc

-1
This file was deleted.

__tests__/frameworks/webpack/package.json

-11
This file was deleted.

__tests__/frameworks/webpack/src/util.ts renamed to __tests__/frameworks/webpack/util.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { Volume, createFsFromVolume } from 'memfs'
44

55
import { VueLoaderPlugin } from 'vue-loader'
66

7-
import type { SFCPluginOptions } from '../../../../src/webpack'
8-
import { SFCFluentPlugin } from '../../../../src/webpack'
7+
import { SFCFluentPlugin } from '../../../src/webpack'
8+
import type { SFCPluginOptions } from '../../../src/webpack'
99

1010
export async function compile(fixture: string, options: Partial<SFCPluginOptions> = {}, hot = false): Promise<webpack.Stats> {
1111
const compilation = webpack({
12-
context: path.resolve(__dirname, '../../..'),
12+
context: path.resolve(__dirname, '../..'),
1313
entry: `./${fixture}`,
1414
externals: {
1515
'vue': 'Vue',

0 commit comments

Comments
 (0)