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

Commit 3a1ef1a

Browse files
committed
Allow to mix custom blocks and ftl files
1 parent 14404d2 commit 3a1ef1a

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

__tests__/__snapshots__/external.spec.ts.snap

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Vitest Snapshot v1
22

33
exports[`external ftl file support > works with vue 2 1`] = `
4-
"import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/components/external.vue\\");import.meta.env = {\\"BASE_URL\\":\\"/\\",\\"MODE\\":\\"development\\",\\"DEV\\":true,\\"PROD\\":false,\\"SSR\\":false};import { FluentResource } from '/@id/@fluent/bundle';
4+
"import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/components/external.vue\\");import.meta.env = {\\"BASE_URL\\":\\"/\\",\\"MODE\\":\\"development\\",\\"DEV\\":true,\\"PROD\\":false,\\"SSR\\":false};import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
55
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
6-
import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
76
const __vue2_script = {}
87
import { render as __vue2_render, staticRenderFns as __vue2_staticRenderFns } from \\"/fixtures/components/external.vue?vue&type=template&lang.js\\"
98
const __cssModules = {}
@@ -27,7 +26,9 @@ function __vue2_injectStyles (context) {
2726
}
2827
}
2928
30-
__component__.options.fluent = { 'en': en_ftl, 'da': da_ftl };
29+
__component__.options.fluent = __component__.options.fluent || {};
30+
__component__.options.fluent['en'] = en_ftl
31+
__component__.options.fluent['da'] = da_ftl
3132
__component__.options.__file = \\"fixtures/components/external.vue\\"
3233
/* hot reload */
3334
import __VUE_HMR_RUNTIME__ from \\"/@id/__x00__/vite/vueHotReload\\"
@@ -48,9 +49,8 @@ export default /*#__PURE__*/(function () { return __component__.exports })()"
4849
`;
4950

5051
exports[`external ftl file support > works with vue 3 1`] = `
51-
"import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/components/external.vue\\");import { FluentResource } from '/@id/@fluent/bundle';
52+
"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';
5253
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
53-
import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
5454
const _sfc_main = {}
5555
import { toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"/@id/vue\\"
5656
@@ -62,7 +62,9 @@ function _sfc_render(_ctx, _cache) {
6262
}
6363
6464
65-
_sfc_main.fluent = { 'en': en_ftl, 'da': da_ftl };
65+
_sfc_main.fluent = _sfc_main.fluent || {};
66+
_sfc_main.fluent['en'] = en_ftl
67+
_sfc_main.fluent['da'] = da_ftl
6668
_sfc_main.__hmrId = \\"4c0553ad\\"
6769
typeof __VUE_HMR_RUNTIME__ !== 'undefined' && __VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)
6870
import.meta.hot.accept(({ default: updated, _rerender_only }) => {
@@ -77,23 +79,22 @@ export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render],['__f
7779
`;
7880

7981
exports[`external ftl file support > works with vue 3 rollup plugin 1`] = `
80-
"import { FluentResource } from '/@id/@fluent/bundle';
82+
"import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
8183
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
82-
import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
8384
const script = {}
8485
import { render } from \\"/fixtures/components/external.vue?vue&type=template&id=8d0ca2ca&lang.js\\"
8586
8687
8788
script.render = render
88-
script.fluent = { 'en': en_ftl, 'da': da_ftl };
89+
script.fluent = script.fluent || {};
90+
script.fluent['en'] = en_ftl
91+
script.fluent['da'] = da_ftl
8992
script.__file = \\"__tests__/fixtures/components/external.vue\\"
9093
export default script"
9194
`;
9295

9396
exports[`external ftl file support > works with vue 3 script setup 1`] = `
94-
"import { createHotContext as __vite__createHotContext } from \\"/@vite/client\\";import.meta.hot = __vite__createHotContext(\\"/fixtures/components/external.setup.vue\\");import { FluentResource } from '/@id/@fluent/bundle';
95-
import en_ftl from '/fixtures/ftl/en/components/external.setup.vue.ftl?import';
96-
import da_ftl from '/fixtures/ftl/da/components/external.setup.vue.ftl?import';
97+
"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';
9798
const _sfc_main = {
9899
setup(__props, { expose }) {
99100
expose();
@@ -116,7 +117,8 @@ function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
116117
}
117118
118119
119-
_sfc_main.fluent = { 'en': en_ftl, 'da': da_ftl };
120+
_sfc_main.fluent = _sfc_main.fluent || {};
121+
_sfc_main.fluent['en'] = en_ftl
120122
_sfc_main.__hmrId = \\"f4d31108\\"
121123
typeof __VUE_HMR_RUNTIME__ !== 'undefined' && __VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)
122124
import.meta.hot.accept(({ default: updated, _rerender_only }) => {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rollup-plugin-fluent-vue",
3-
"version": "3.1.0-3",
3+
"version": "3.1.0-4",
44
"description": "Rollup and Vite plugin for custom block support for fluent-vue",
55
"keywords": [
66
"localization",

src/index.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ function getInsertInfo(source: string): InsertInfo {
5757
return { insertPos, target }
5858
}
5959

60+
function cleanLocale(str: string) {
61+
return str.replace('-', '_')
62+
}
63+
64+
async function fileExists(filename: string): Promise<boolean> {
65+
try {
66+
const stat = await fs.stat(filename, { throwIfNoEntry: false } as any)
67+
return !!stat
68+
}
69+
catch {
70+
return false
71+
}
72+
}
73+
6074
export default function fluentPlugin({ blockType = 'fluent', external }: PluginOptions = {}): Plugin {
6175
return {
6276
name: 'rollup-plugin-fluent-vue',
@@ -88,11 +102,24 @@ export default new FluentResource(${JSON.stringify(ftl)})
88102

89103
const magic = new MagicString(code, { filename: id })
90104

91-
magic.prepend(`${external.locales.map(locale => `import ${locale}_ftl from '${normalizePath(join(external.ftlDir, locale, relativePath))}.ftl'`).join(';\n')};\n`)
92-
magic.prepend('import { FluentResource } from \'@fluent/bundle\';\n')
105+
const existingTranslations = []
106+
for (const locale of external.locales) {
107+
const ftlPath = normalizePath(join(external.ftlDir, locale, `${relativePath}.ftl`))
108+
this.addWatchFile(ftlPath)
109+
110+
const ftlExists = await fileExists(ftlPath)
111+
112+
if (ftlExists) {
113+
existingTranslations.push(locale)
114+
magic.prepend(`import ${cleanLocale(locale)}_ftl from '${ftlPath}';\n`)
115+
}
116+
}
93117

94118
const { insertPos, target } = getInsertInfo(code)
95-
magic.appendLeft(insertPos, `${target}.fluent = { ${external.locales.map(locale => `'${locale}': ${locale}_ftl`).join(', ')} };\n`)
119+
120+
magic.appendLeft(insertPos, `${target}.fluent = ${target}.fluent || {};\n`)
121+
for (const locale of existingTranslations)
122+
magic.appendLeft(insertPos, `${target}.fluent['${cleanLocale(locale)}'] = ${cleanLocale(locale)}_ftl\n`)
96123

97124
return {
98125
code: magic.toString(),

0 commit comments

Comments
 (0)