diff --git a/libs/native-federation-core/src/lib/core/bundle-exposed-and-mappings.ts b/libs/native-federation-core/src/lib/core/bundle-exposed-and-mappings.ts index e0dd20f1..20d7a469 100644 --- a/libs/native-federation-core/src/lib/core/bundle-exposed-and-mappings.ts +++ b/libs/native-federation-core/src/lib/core/bundle-exposed-and-mappings.ts @@ -9,6 +9,7 @@ import { } from '../utils/build-result-map'; import { logger } from '../utils/logger'; import { normalize } from '../utils/normalize'; +import { tryCopyFileToLocales } from '../utils/try-copy-file-to-locales'; export interface ArtefactInfo { mappings: SharedInfo[]; @@ -55,9 +56,11 @@ export async function bundleExposedAndMappings( const sharedResult: Array = []; for (const item of shared) { + const outBaseName = lookupInResultMap(resultMap, item.outName); + const outFile = path.join(fedOptions.workspaceRoot, fedOptions.outputPath, outBaseName); sharedResult.push({ packageName: item.key, - outFileName: lookupInResultMap(resultMap, item.outName), + outFileName: outBaseName, requiredVersion: '', singleton: true, strictVersion: false, @@ -68,14 +71,17 @@ export async function bundleExposedAndMappings( entryPoint: normalize(path.normalize(item.fileName)), }, }); + tryCopyFileToLocales(outFile, fedOptions); } const exposedResult: Array = []; for (const item of exposes) { + const outBaseName = lookupInResultMap(resultMap, item.outName); + const outFile = path.join(fedOptions.workspaceRoot, fedOptions.outputPath, outBaseName); exposedResult.push({ key: item.key, - outFileName: lookupInResultMap(resultMap, item.outName), + outFileName: outBaseName, dev: !fedOptions.dev ? undefined : { @@ -84,6 +90,7 @@ export async function bundleExposedAndMappings( ), }, }); + tryCopyFileToLocales(outFile, fedOptions); } return { mappings: sharedResult, exposes: exposedResult }; diff --git a/libs/native-federation-core/src/lib/core/bundle-shared.ts b/libs/native-federation-core/src/lib/core/bundle-shared.ts index 20dc0657..fe1d5815 100644 --- a/libs/native-federation-core/src/lib/core/bundle-shared.ts +++ b/libs/native-federation-core/src/lib/core/bundle-shared.ts @@ -8,6 +8,7 @@ import { FederationOptions } from './federation-options'; import { copySrcMapIfExists } from '../utils/copy-src-map-if-exists'; import { logger } from '../utils/logger'; import { normalize } from '../utils/normalize'; +import { tryCopyFileToLocales } from '../utils/try-copy-file-to-locales'; export async function bundleShared( config: NormalizedFederationConfig, @@ -77,6 +78,7 @@ export async function bundleShared( const outFileName = path.basename(fileName); const cachedFile = path.join(cachePath, outFileName); + tryCopyFileToLocales(cachedFile, fedOptions); copyFileIfExists(cachedFile, fileName); copySrcMapIfExists(cachedFile, fileName); } diff --git a/libs/native-federation-core/src/lib/core/federation-options.ts b/libs/native-federation-core/src/lib/core/federation-options.ts index 7c5cb51b..8d64d821 100644 --- a/libs/native-federation-core/src/lib/core/federation-options.ts +++ b/libs/native-federation-core/src/lib/core/federation-options.ts @@ -7,4 +7,5 @@ export interface FederationOptions { dev?: boolean; watch?: boolean; packageJson?: string; + locales?: string[]; } diff --git a/libs/native-federation-core/src/lib/core/write-federation-info.ts b/libs/native-federation-core/src/lib/core/write-federation-info.ts index 12bc5520..18c595ea 100644 --- a/libs/native-federation-core/src/lib/core/write-federation-info.ts +++ b/libs/native-federation-core/src/lib/core/write-federation-info.ts @@ -7,10 +7,22 @@ export function writeFederationInfo( federationInfo: FederationInfo, fedOptions: FederationOptions ) { - const metaDataPath = path.join( - fedOptions.workspaceRoot, - fedOptions.outputPath, - 'remoteEntry.json' - ); - fs.writeFileSync(metaDataPath, JSON.stringify(federationInfo, null, 2)); + if (!fedOptions.locales?.length) { + const metaDataPath = path.join( + fedOptions.workspaceRoot, + fedOptions.outputPath, + 'remoteEntry.json' + ); + fs.writeFileSync(metaDataPath, JSON.stringify(federationInfo, null, 2)); + } else { + for (const locale of fedOptions.locales) { + const metaDataPath = path.join( + fedOptions.workspaceRoot, + fedOptions.outputPath, + locale, + 'remoteEntry.json'); + fs.mkdirSync(path.dirname(metaDataPath), { recursive: true }); + fs.writeFileSync(metaDataPath, JSON.stringify(federationInfo, null, 2)); + } + } } diff --git a/libs/native-federation-core/src/lib/core/write-import-map.ts b/libs/native-federation-core/src/lib/core/write-import-map.ts index 5fa39f11..5f0e5c52 100644 --- a/libs/native-federation-core/src/lib/core/write-import-map.ts +++ b/libs/native-federation-core/src/lib/core/write-import-map.ts @@ -15,10 +15,23 @@ export function writeImportMap( }, {}); const importMap = { imports }; - const importMapPath = path.join( - fedOption.workspaceRoot, - fedOption.outputPath, - 'importmap.json' - ); - fs.writeFileSync(importMapPath, JSON.stringify(importMap, null, 2)); + if (!fedOption.locales?.length) { + const importMapPath = path.join( + fedOption.workspaceRoot, + fedOption.outputPath, + 'importmap.json' + ); + fs.writeFileSync(importMapPath, JSON.stringify(importMap, null, 2)); + } else { + for (const locale of fedOption.locales) { + const importMapPath = path.join( + fedOption.workspaceRoot, + fedOption.outputPath, + locale, + 'importmap.json' + ); + fs.mkdirSync(path.dirname(importMapPath), { recursive: true }); + fs.writeFileSync(importMapPath, JSON.stringify(importMap, null, 2)); + } + } } diff --git a/libs/native-federation-core/src/lib/utils/try-copy-file-to-locales.ts b/libs/native-federation-core/src/lib/utils/try-copy-file-to-locales.ts new file mode 100644 index 00000000..afdfc566 --- /dev/null +++ b/libs/native-federation-core/src/lib/utils/try-copy-file-to-locales.ts @@ -0,0 +1,30 @@ +import path from 'path'; +import fs from 'fs'; +import { FederationOptions } from '../core/federation-options'; + +export function tryCopyFileToLocales(sourceFile: string, fedOptions: FederationOptions) { + + if (!fedOptions.locales?.length) { + // in case the project has not specified localization, ignore this step + return; + } + const sourceBaseName = path.basename(sourceFile); + + for (const locale of fedOptions.locales) { + const destinationFile = path.join( + fedOptions.workspaceRoot, + fedOptions.outputPath, + locale, + sourceBaseName + ); + fs.mkdirSync(path.dirname(destinationFile), { recursive: true }); + + if (fs.existsSync(sourceFile)) { + fs.copyFileSync(sourceFile, destinationFile); + } + const [sourceMapFile, destinationMapFile] = [sourceFile, destinationFile].map(f => f + '.map'); + if (fs.existsSync(sourceMapFile)) { + fs.copyFileSync(sourceMapFile, destinationMapFile); + } + } +} \ No newline at end of file diff --git a/libs/native-federation/src/builders/build/builder.ts b/libs/native-federation/src/builders/build/builder.ts index e299bf38..dd69d8fb 100644 --- a/libs/native-federation/src/builders/build/builder.ts +++ b/libs/native-federation/src/builders/build/builder.ts @@ -6,6 +6,7 @@ import { } from '@angular-devkit/architect'; import { Schema } from '@angular-devkit/build-angular/src/builders/browser-esbuild/schema'; +import { createI18nOptions } from '@angular-devkit/build-angular/src/utils/i18n-options'; import { buildEsbuildBrowser } from '@angular-devkit/build-angular/src/builders/browser-esbuild'; @@ -54,6 +55,10 @@ export async function* runBuilder( _options, builder )) as JsonObject & Schema; + const i18nOptions = createI18nOptions( + await context.getProjectMetadata(target.project), + options.localize + ); const runServer = !!nfOptions.port; const write = !runServer; @@ -75,6 +80,7 @@ export async function* runBuilder( verbose: options.verbose, watch: false, // options.watch, dev: !!nfOptions.dev, + locales: Array.from(i18nOptions.inlineLocales), }; const config = await loadFederationConfig(fedOptions); diff --git a/libs/native-federation/src/utils/updateIndexHtml.ts b/libs/native-federation/src/utils/updateIndexHtml.ts index 76e9fb95..cea4350c 100644 --- a/libs/native-federation/src/utils/updateIndexHtml.ts +++ b/libs/native-federation/src/utils/updateIndexHtml.ts @@ -3,7 +3,18 @@ import * as fs from 'fs'; import { FederationOptions } from '@softarc/native-federation/build'; export function updateIndexHtml(fedOptions: FederationOptions) { - const outputPath = path.join(fedOptions.workspaceRoot, fedOptions.outputPath); + if (!fedOptions.locales?.length) { + const outputPath = path.join(fedOptions.workspaceRoot, fedOptions.outputPath); + updateSingleIndexHtml(outputPath); + } else { + for (const locale of fedOptions.locales) { + const outputPath = path.join(fedOptions.workspaceRoot, fedOptions.outputPath, locale); + updateSingleIndexHtml(outputPath); + } + } +} + +export function updateSingleIndexHtml(outputPath: string) { const indexPath = path.join(outputPath, 'index.html'); const mainName = fs .readdirSync(outputPath)