-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexternal-plugin.ts
189 lines (153 loc) · 5.35 KB
/
external-plugin.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { join, relative } from 'path'
import { stat as fsStat } from 'fs/promises'
import { createUnplugin } from 'unplugin'
import MagicString from 'magic-string'
import { createFilter, makeLegalIdentifier } from '@rollup/pluginutils'
import type { ExternalPluginOptions, InsertInfo } from '../types'
import { getSyntaxErrors } from './ftl/parse'
function getInsertInfo(source: string): InsertInfo {
let target = null
// vite-plugin-vue2
if (source.includes('__component__'))
target = '__component__'
// rollup-plugin-vue
if (source.includes('export default script'))
target = 'script'
// @vitejs/plugin-vue
if (source.includes('_sfc_main'))
target = '_sfc_main'
// vue-loader
if (source.includes('__exports__'))
target = '__exports__'
const insertPos = source.indexOf('export default')
if (insertPos === -1 || target === null)
throw new Error('Could not parse vue component. This is the issue with unplugin-fluent-vue.\nPlease report this issue to the unplugin-fluent-vue repository.')
return { insertPos, target }
}
async function fileExists(filename: string): Promise<boolean> {
try {
const stat = await fsStat(filename, { throwIfNoEntry: false } as any)
return !!stat
}
catch {
return false
}
}
function normalizePath(path: string) {
return path.replace(/\\/g, '/')
}
const isVue = createFilter(['**/*.vue'])
const isFtl = createFilter(['**/*.ftl'])
interface Dependency {
locale: string
ftlPath: string
importVariable: string
}
export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => {
const resolvedOptions = {
checkSyntax: true,
virtualModuleName: 'virtual:ftl-for-file',
getFtlPath: undefined as ((locale: string, vuePath: string) => string) | undefined,
...options,
}
if ('getFtlPath' in options) {
resolvedOptions.getFtlPath = options.getFtlPath
}
else {
resolvedOptions.getFtlPath = (locale: string, vuePath: string) => {
return join(options.ftlDir, locale, `${relative(options.baseDir, vuePath)}.ftl`)
}
}
const getTranslationsForFile = async (id: string) => {
const dependencies: Dependency[] = []
for (const locale of options.locales) {
const ftlPath = normalizePath(resolvedOptions.getFtlPath(locale, id))
const ftlExists = await fileExists(ftlPath)
if (ftlExists) {
dependencies.push({
locale,
ftlPath,
importVariable: `${makeLegalIdentifier(locale)}_ftl`,
})
}
}
return dependencies
}
return {
name: 'unplugin-fluent-vue-external',
enforce: meta.framework === 'webpack' ? 'post' : undefined,
resolveId(id, importer) {
if (id === resolvedOptions.virtualModuleName)
return `${id}?importer=${importer}`
},
async load(id) {
if (!id.startsWith(resolvedOptions.virtualModuleName))
return
const importer = id.split('?importer=')[1]
const translations = await getTranslationsForFile(importer)
for (const { ftlPath } of translations)
this.addWatchFile(ftlPath)
let code = ''
for (const { ftlPath, importVariable } of translations)
code += `import ${importVariable} from '${ftlPath}';\n`
code += `export default { ${translations
.map(({ locale, importVariable }) => `'${locale}': ${importVariable}`)
.join(', ')} }\n`
return code
},
transformInclude(id: string) {
return isVue(id) || isFtl(id)
},
async transform(source: string, id: string) {
if (isVue(id)) {
const magic = new MagicString(source, { filename: id })
const { insertPos, target } = getInsertInfo(source)
const translations = await getTranslationsForFile(id)
for (const { ftlPath } of translations)
this.addWatchFile(ftlPath)
for (const dep of translations)
magic.prepend(`import ${dep.importVariable} from '${dep.ftlPath}';\n`)
magic.appendLeft(insertPos, `${target}.fluent = ${target}.fluent || {};\n`)
for (const dep of translations)
magic.appendLeft(insertPos, `${target}.fluent['${dep.locale}'] = ${dep.importVariable}\n`)
magic.appendLeft(insertPos, `
const __HOT_API__ = import.meta.hot || import.meta.webpackHot
if (__HOT_API__) {
__HOT_API__.accept([${translations.map(dep => `'${dep.ftlPath}'`).join(', ')}], () => {
${translations.map(({ locale, importVariable }) => `${target}.fluent['${locale}'] = ${importVariable}`).join('\n')}
delete ${target}._fluent
if (typeof __VUE_HMR_RUNTIME__ !== 'undefined') {
// Vue 3
__VUE_HMR_RUNTIME__.reload(${target}.__hmrId, ${target})
} else {
// Vue 2
// There is no proper api to access HMR for component from custom block
// so use this magic
delete ${target}._Ctor
}
})
}
`)
return {
code: magic.toString(),
map: magic.generateMap({ hires: true }),
}
}
if (isFtl(id)) {
if (options.checkSyntax) {
const errorsText = getSyntaxErrors(source)
if (errorsText)
this.error(errorsText)
}
return `
import { FluentResource } from '@fluent/bundle'
export default new FluentResource(${JSON.stringify(source)})
`
}
return undefined
},
}
})
export const vitePlugin = unplugin.vite
export const rollupPlugin = unplugin.rollup
export const webpackPlugin = unplugin.webpack