Skip to content

Commit bbda7d3

Browse files
authored
Fix path to relative ftl file imports (#49)
* Fix path to ftl file * Improve HMR
1 parent c4b6663 commit bbda7d3

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed
Binary file not shown.

scripts/build-examples.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function buildExamples () {
1414

1515
for (const example of examples) {
1616
console.log(`building ${example.folder}...`)
17-
await execa('pnpm', ['add', `file:../../`], { stdio: 'inherit', cwd: example.folder })
17+
await execa('pnpm', ['add', `link:../../`], { stdio: 'inherit', cwd: example.folder })
1818
await execa('pnpm', ['i'], { stdio: 'inherit', cwd: example.folder })
1919
await execa('pnpm', ['build'], { stdio: 'inherit', cwd: example.folder })
2020
}

src/plugins/external-plugin.ts

+38-23
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,46 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
7676
}
7777
}
7878

79+
const insertFtlImports = (magic: MagicString, translations: Dependency[]) => {
80+
for (const dep of translations)
81+
magic.prepend(`import ${dep.importVariable} from '${dep.relativeFtlPath}';\n`)
82+
}
83+
84+
const insertHotCode = (magic: MagicString, translations: Dependency[], target: string, insertPos: number) => {
85+
const __HOT_API__ = meta.framework === 'webpack' ? 'import.meta.webpackHot' : 'import.meta.hot'
86+
87+
magic.appendLeft(insertPos, `
88+
if (${__HOT_API__}) {
89+
${__HOT_API__}.accept([${translations.map(dep => `'${dep.relativeFtlPath}'`).join(', ')}], (mods) => {
90+
${translations.map(({ locale, importVariable }) => `${target}.fluent['${locale}'] = ${importVariable}`).join('\n')}
91+
92+
if (mods) {
93+
${translations.map(({ locale }, index) => `if (mods['${index}']) ${target}.fluent['${locale}'] = mods['${index}'].default`).join('\n')}
94+
}
95+
96+
delete ${target}._fluent
97+
if (typeof __VUE_HMR_RUNTIME__ !== 'undefined') {
98+
// Vue 3
99+
__VUE_HMR_RUNTIME__.reload(${target}.__hmrId, ${target})
100+
} else {
101+
// Vue 2
102+
// There is no proper api to access HMR for component from custom block
103+
// so use this magic
104+
delete ${target}._Ctor
105+
}
106+
})
107+
}
108+
`)
109+
}
110+
79111
const getTranslationsForFile = async (id: string) => {
80112
const dependencies: Dependency[] = []
81113
for (const locale of options.locales) {
82114
const ftlPath = normalizePath(resolvedOptions.getFtlPath(locale, id))
83115
const ftlExists = await fileExists(ftlPath)
84-
const relativeFtlPath = normalizePath(relative(dirname(id), ftlPath))
116+
let relativeFtlPath = normalizePath(relative(dirname(id), ftlPath))
117+
if (!relativeFtlPath.startsWith('.'))
118+
relativeFtlPath = `./${relativeFtlPath}`
85119

86120
if (ftlExists) {
87121
dependencies.push({
@@ -141,32 +175,13 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
141175
for (const { ftlPath } of translations)
142176
this.addWatchFile(ftlPath)
143177

144-
for (const dep of translations)
145-
magic.prepend(`import ${dep.importVariable} from '${dep.relativeFtlPath}';\n`)
178+
insertFtlImports(magic, translations)
179+
146180
magic.appendLeft(insertPos, `${target}.fluent = ${target}.fluent || {};\n`)
147181
for (const dep of translations)
148182
magic.appendLeft(insertPos, `${target}.fluent['${dep.locale}'] = ${dep.importVariable}\n`)
149183

150-
const __HOT_API__ = meta.framework === 'webpack' ? 'import.meta.webpackHot' : 'import.meta.hot'
151-
152-
magic.appendLeft(insertPos, `
153-
if (${__HOT_API__}) {
154-
${__HOT_API__}.accept([${translations.map(dep => `'${dep.relativeFtlPath}'`).join(', ')}], () => {
155-
${translations.map(({ locale, importVariable }) => `${target}.fluent['${locale}'] = ${importVariable}`).join('\n')}
156-
157-
delete ${target}._fluent
158-
if (typeof __VUE_HMR_RUNTIME__ !== 'undefined') {
159-
// Vue 3
160-
__VUE_HMR_RUNTIME__.reload(${target}.__hmrId, ${target})
161-
} else {
162-
// Vue 2
163-
// There is no proper api to access HMR for component from custom block
164-
// so use this magic
165-
delete ${target}._Ctor
166-
}
167-
})
168-
}
169-
`)
184+
insertHotCode(magic, translations, target, insertPos)
170185

171186
return {
172187
code: magic.toString(),

0 commit comments

Comments
 (0)