File tree Expand file tree Collapse file tree 5 files changed +36
-14
lines changed
__tests__/frameworks/vite Expand file tree Collapse file tree 5 files changed +36
-14
lines changed Original file line number Diff line number Diff line change @@ -74,6 +74,29 @@ describe('Vite external', () => {
74
74
expect ( code ) . toMatchSnapshot ( )
75
75
} )
76
76
77
+ describe ( 'parseFtl' , ( ) => {
78
+ it ( 'parses ftl syntax during compilation' , async ( ) => {
79
+ // Arrange
80
+ // Act
81
+ const code = await compile ( {
82
+ plugins : [
83
+ vue3 ( {
84
+ compiler,
85
+ } ) ,
86
+ ExternalFluentPlugin ( {
87
+ baseDir : resolve ( baseDir , 'fixtures' ) ,
88
+ ftlDir : resolve ( baseDir , 'fixtures/ftl' ) ,
89
+ locales : [ 'en' , 'da' ] ,
90
+ parseFtl : true ,
91
+ } ) ,
92
+ ] ,
93
+ } , '/fixtures/components/external.vue' )
94
+
95
+ // Assert
96
+ expect ( code ) . toMatchSnapshot ( )
97
+ } )
98
+ } )
99
+
77
100
it ( 'virtual:ftl-for-file' , async ( ) => {
78
101
// Arrange
79
102
// Act
Original file line number Diff line number Diff line change @@ -132,7 +132,7 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions) => {
132
132
}
133
133
134
134
if ( isFtl ( id ) ) {
135
- if ( options . checkSyntax ) {
135
+ if ( resolvedOptions . checkSyntax ) {
136
136
const errorsText = getSyntaxErrors ( source )
137
137
if ( errorsText )
138
138
this . error ( errorsText )
@@ -158,19 +158,15 @@ export default /*#__PURE__*/ new FluentResource(${source})
158
158
this . error ( errorsText )
159
159
}
160
160
161
- const magic = new MagicString ( source , { filename : id } )
162
- if ( source . length > 0 )
163
- magic . update ( 0 , source . length , JSON . stringify ( source ) )
164
- else
165
- magic . append ( '""' )
166
- magic . prepend ( `
167
- import { FluentResource } from '@fluent/bundle'
161
+ const injectFtl = getInjectFtl ( resolvedOptions )
168
162
163
+ const magic = injectFtl `
169
164
export default function (Component) {
170
165
const target = Component.options || Component
171
166
target.fluent = target.fluent || {}
172
- target.fluent['${ query . locale } '] = new FluentResource(` )
173
- magic . append ( ')\n}' )
167
+ target.fluent['${ query . locale } '] = ${ source }
168
+ }
169
+ `
174
170
175
171
return {
176
172
code : magic . toString ( ) ,
Original file line number Diff line number Diff line change 1
- import type { SFCPluginOptions } from 'src/types'
2
-
3
1
import { FluentResource } from '@fluent/bundle'
4
2
import MagicString from 'magic-string'
5
3
@@ -9,7 +7,7 @@ function normalize(str: string) {
9
7
return str . replace ( / \r \n / g, '\n' ) . trim ( )
10
8
}
11
9
12
- export function getInjectFtl ( options : SFCPluginOptions ) : InjectFtlFn {
10
+ export function getInjectFtl ( options : { parseFtl ?: boolean } ) : InjectFtlFn {
13
11
return ( template , locale , source ) => {
14
12
if ( source == null ) {
15
13
source = locale
Original file line number Diff line number Diff line change @@ -13,7 +13,12 @@ export interface ExternalPluginOptionsFunction extends ExternalPluginOptionsBase
13
13
getFtlPath : ( locale : string , vuePath : string ) => string
14
14
}
15
15
16
- export type ExternalPluginOptions = ExternalPluginOptionsFolder | ExternalPluginOptionsFunction
16
+ export type ExternalPluginOptions = ( ExternalPluginOptionsFolder | ExternalPluginOptionsFunction ) & {
17
+ /**
18
+ * Whether to parse the ftl syntax before injecting it into component
19
+ */
20
+ parseFtl ?: boolean
21
+ }
17
22
18
23
export interface SFCPluginOptions {
19
24
/**
You can’t perform that action at this time.
0 commit comments