11import { red , blue , green , bold } from 'colors' ;
2- import { existsSync , readFileSync , writeFileSync } from 'fs' ;
2+ import { existsSync , readdirSync , readFileSync , statSync , writeFileSync } from 'fs' ;
33import { load } from 'js-yaml' ;
44import { join } from 'path' ;
55
@@ -9,6 +9,7 @@ import { DefaultAppConfig } from './default-app-config';
99import { ServerConfig } from './server-config.interface' ;
1010import { mergeConfig } from './config.util' ;
1111import { isNotEmpty } from '../app/shared/empty.util' ;
12+ import { LanguageHashesConfig } from './languageHashes-config.interface' ;
1213
1314const CONFIG_PATH = join ( process . cwd ( ) , 'config' ) ;
1415
@@ -231,6 +232,29 @@ export const buildAppConfig = (destConfigPath?: string): AppConfig => {
231232 // apply build defined production
232233 appConfig . production = env === 'production' ;
233234
235+ const useDynamicLanguageHashes = isNotEmpty ( ENV ( 'DYNAMIC_LANGUAGE_HASHES' , true ) ) ? ENV ( 'DYNAMIC_LANGUAGE_HASHES' , true ) : false ;
236+ if ( appConfig . production && useDynamicLanguageHashes ) {
237+ const i18nAssetsDir = 'dist/server/assets/i18n/' ;
238+ const i18nFiles = readdirSync ( i18nAssetsDir ) ;
239+ //sort all the i18nFiles by mtime descending
240+ i18nFiles . sort ( function ( a , b ) {
241+ return statSync ( i18nAssetsDir + b ) . mtime . getTime ( ) - statSync ( i18nAssetsDir + a ) . mtime . getTime ( ) ;
242+ } ) ;
243+
244+ i18nFiles . forEach ( file => {
245+ const match = file . match ( / ^ ( .+ ) \. ( .+ ) \. j s o n $ / ) ;
246+ // only add language hash to config if no config exists for current language (ensuring the latest language file
247+ // is used)
248+ if ( match && appConfig . languageHashes . filter ( ( ( languageHashConfig ) => languageHashConfig . lang === match [ 1 ] ) ) . length === 0 ) {
249+ const languageConfig : LanguageHashesConfig = {
250+ lang : match [ 1 ] ,
251+ md5 : match [ 2 ]
252+ } ;
253+ appConfig . languageHashes . push ( languageConfig ) ;
254+ }
255+ } ) ;
256+ }
257+
234258 // build base URLs
235259 buildBaseUrl ( appConfig . ui ) ;
236260 buildBaseUrl ( appConfig . rest ) ;
0 commit comments