@@ -16,7 +16,7 @@ import { loadEsmModule } from '../../utils/load-esm';
16
16
/**
17
17
* The options passed to the inliner for each file request
18
18
*/
19
- interface InlineRequest {
19
+ interface InlineFileRequest {
20
20
/**
21
21
* The filename that should be processed. The data for the file is provided to the Worker
22
22
* during Worker initialization.
@@ -34,6 +34,31 @@ interface InlineRequest {
34
34
translation ?: Record < string , unknown > ;
35
35
}
36
36
37
+ /**
38
+ * The options passed to the inliner for each code request
39
+ */
40
+ interface InlineCodeRequest {
41
+ /**
42
+ * The code that should be processed.
43
+ */
44
+ code : string ;
45
+
46
+ /**
47
+ * The filename to use in error and warning messages for the provided code.
48
+ */
49
+ filename : string ;
50
+
51
+ /**
52
+ * The locale specifier that should be used during the inlining process of the file.
53
+ */
54
+ locale : string ;
55
+
56
+ /**
57
+ * The translation messages for the locale that should be used during the inlining process of the file.
58
+ */
59
+ translation ?: Record < string , unknown > ;
60
+ }
61
+
37
62
// Extract the application files and common options used for inline requests from the Worker context
38
63
// TODO: Evaluate overall performance difference of passing translations here as well
39
64
const { files, missingTranslation, shouldOptimize } = ( workerData || { } ) as {
@@ -47,9 +72,9 @@ const { files, missingTranslation, shouldOptimize } = (workerData || {}) as {
47
72
* This function is the main entry for the Worker's action that is called by the worker pool.
48
73
*
49
74
* @param request An InlineRequest object representing the options for inlining
50
- * @returns An array containing the inlined file and optional map content.
75
+ * @returns An object containing the inlined file and optional map content.
51
76
*/
52
- export default async function inlineLocale ( request : InlineRequest ) {
77
+ export default async function inlineFile ( request : InlineFileRequest ) {
53
78
const data = files . get ( request . filename ) ;
54
79
55
80
assert ( data !== undefined , `Invalid inline request for file '${ request . filename } '.` ) ;
@@ -70,6 +95,22 @@ export default async function inlineLocale(request: InlineRequest) {
70
95
} ;
71
96
}
72
97
98
+ /**
99
+ * Inlines the provided locale and translation into JavaScript code that contains `$localize` usage.
100
+ * This function is a secondary entry primarily for use with component HMR update modules.
101
+ *
102
+ * @param request An InlineRequest object representing the options for inlining
103
+ * @returns An object containing the inlined code.
104
+ */
105
+ export async function inlineCode ( request : InlineCodeRequest ) {
106
+ const result = await transformWithBabel ( request . code , undefined , request ) ;
107
+
108
+ return {
109
+ output : result . code ,
110
+ messages : result . diagnostics . messages ,
111
+ } ;
112
+ }
113
+
73
114
/**
74
115
* A Type representing the localize tools module.
75
116
*/
@@ -138,7 +179,7 @@ async function createI18nPlugins(locale: string, translation: Record<string, unk
138
179
async function transformWithBabel (
139
180
code : string ,
140
181
map : SourceMapInput | undefined ,
141
- options : InlineRequest ,
182
+ options : InlineFileRequest ,
142
183
) {
143
184
let ast ;
144
185
try {
0 commit comments