@@ -42,6 +42,8 @@ export interface AugmentIndexHtmlOptions {
42
42
loadOutputFile : LoadOutputFileFunctionType ;
43
43
/** Used to sort the inseration of files in the HTML file */
44
44
entrypoints : string [ ] ;
45
+ /** Used to set the document default locale */
46
+ lang ?: string ;
45
47
}
46
48
47
49
export interface FileInfo {
@@ -56,6 +58,7 @@ export interface FileInfo {
56
58
* after processing several configurations in order to build different sets of
57
59
* bundles for differential serving.
58
60
*/
61
+ // tslint:disable-next-line: no-big-function
59
62
export async function augmentIndexHtml ( params : AugmentIndexHtmlOptions ) : Promise < string > {
60
63
const { loadOutputFile, files, noModuleFiles = [ ] , moduleFiles = [ ] , entrypoints } = params ;
61
64
@@ -91,8 +94,10 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
91
94
const document = parse5 . parse ( params . inputContent , { treeAdapter, locationInfo : true } ) ;
92
95
let headElement ;
93
96
let bodyElement ;
97
+ let htmlElement ;
94
98
for ( const docChild of document . childNodes ) {
95
99
if ( docChild . tagName === 'html' ) {
100
+ htmlElement = docChild ;
96
101
for ( const htmlChild of docChild . childNodes ) {
97
102
if ( htmlChild . tagName === 'head' ) {
98
103
headElement = htmlChild ;
@@ -241,6 +246,33 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
241
246
242
247
indexSource . insert ( styleInsertionPoint , parse5 . serialize ( styleElements , { treeAdapter } ) ) ;
243
248
249
+ // Adjust document locale if specified
250
+ if ( typeof params . lang == 'string' ) {
251
+
252
+ const htmlFragment = treeAdapter . createDocumentFragment ( ) ;
253
+
254
+ let langAttribute ;
255
+ for ( const attribute of htmlElement . attrs ) {
256
+ if ( attribute . name === 'lang' ) {
257
+ langAttribute = attribute ;
258
+ }
259
+ }
260
+ if ( langAttribute ) {
261
+ langAttribute . value = params . lang ;
262
+ } else {
263
+ htmlElement . attrs . push ( { name : 'lang' , value : params . lang } ) ;
264
+ }
265
+ // we want only openning tag
266
+ htmlElement . childNodes = [ ] ;
267
+
268
+ treeAdapter . appendChild ( htmlFragment , htmlElement ) ;
269
+ indexSource . replace (
270
+ htmlElement . __location . startTag . startOffset ,
271
+ htmlElement . __location . startTag . endOffset - 1 ,
272
+ parse5 . serialize ( htmlFragment , { treeAdapter } ) . replace ( '</html>' , '' ) ,
273
+ ) ;
274
+ }
275
+
244
276
return indexSource . source ( ) ;
245
277
}
246
278
0 commit comments