Skip to content

1st attempt at i18n support #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -55,9 +56,11 @@ export async function bundleExposedAndMappings(
const sharedResult: Array<SharedInfo> = [];

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,
Expand All @@ -68,14 +71,17 @@ export async function bundleExposedAndMappings(
entryPoint: normalize(path.normalize(item.fileName)),
},
});
tryCopyFileToLocales(outFile, fedOptions);
}

const exposedResult: Array<ExposesInfo> = [];

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
: {
Expand All @@ -84,6 +90,7 @@ export async function bundleExposedAndMappings(
),
},
});
tryCopyFileToLocales(outFile, fedOptions);
}

return { mappings: sharedResult, exposes: exposedResult };
Expand Down
2 changes: 2 additions & 0 deletions libs/native-federation-core/src/lib/core/bundle-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface FederationOptions {
dev?: boolean;
watch?: boolean;
packageJson?: string;
locales?: string[];
}
24 changes: 18 additions & 6 deletions libs/native-federation-core/src/lib/core/write-federation-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
25 changes: 19 additions & 6 deletions libs/native-federation-core/src/lib/core/write-import-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
6 changes: 6 additions & 0 deletions libs/native-federation/src/builders/build/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
13 changes: 12 additions & 1 deletion libs/native-federation/src/utils/updateIndexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down