import { IPluginAPI } from 'build-scripts' ; import path from 'path' ; import { I18nConfig } from './types' ; import { LOCALE_COOKIE_KEY } from './constants' ; export default async function ( { onGetWebpackConfig , getValue , applyMethod } , i18nConfig ) { checkI18nConfig ( i18nConfig ) ; const { i18nRouting = true } = i18nConfig ; const iceTemp = getValue < string > ( 'TEMP_PATH' ) ; const i18nTempDir = path . join ( iceTemp , 'plugins' , 'i18n' ) ; applyMethod ( 'modifyRenderData' , ( originRenderData ) => { return { ... originRenderData , i18n : { ... i18nConfig } } ; } ) ; applyMethod ( 'addPluginTemplate' , path . join ( __dirname , 'templates' ) , { LOCALE_COOKIE_KEY , i18nConfig , i18nRouting } ) ; applyMethod ( 'addPluginTemplate' , { template : path . join ( __dirname , '../src/utils' ) , targetDir : 'i18n/utils' , } ) ; onGetWebpackConfig ( ( config ) => { config . resolve . alias . set ( '$ice/i18n' , path . join ( i18nTempDir , 'index.tsx' ) ) ; } ) ; applyMethod ( 'addExport' , { source : './plugins/i18n' , importSource : '$$ice/plugins/i18n' , exportMembers : [ 'useLocale' , 'getAllLocales' , 'getDefaultLocale' , 'getLocale' ] } ) ; } function checkI18nConfig ( i18nConfig ) { const { locales , defaultLocale } = i18nConfig ; if ( ! locales ) { console . error ( `[build-plugin-ice-i18n] locales should be array but received ${ typeof locales }` ) ; process . exit ( 1 ) ; } if ( ! defaultLocale ) { console . error ( `[build-plugin-ice-i18n] defaultLocale should be string but received ${ typeof defaultLocale }` ) ; process . exit ( 1 ) ; } }