Skip to content

Commit ba51b14

Browse files
feat(native-federation-typescript): speedup (#2341)
Co-authored-by: ScriptedAlchemy <[email protected]>
1 parent 671f64b commit ba51b14

File tree

7 files changed

+123
-41
lines changed

7 files changed

+123
-41
lines changed

packages/native-federation-tests/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const NativeFederationTestsRemote = createUnplugin(
5555
watchChange: (this as UnpluginOptions).writeBundle,
5656
};
5757
},
58-
webpack: (compiler) => {
58+
webpack(compiler) {
5959
compiler.options.devServer = mergeDeepRight(
6060
compiler.options.devServer || {},
6161
{
@@ -65,7 +65,7 @@ export const NativeFederationTestsRemote = createUnplugin(
6565
},
6666
);
6767
},
68-
rspack: (compiler) => {
68+
rspack(compiler) {
6969
compiler.options.devServer = mergeDeepRight(
7070
compiler.options.devServer || {},
7171
{

packages/native-federation-typescript/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"ansi-colors": "^4.1.3",
5757
"axios": "^1.6.7",
5858
"rambda": "^9.1.0",
59-
"unplugin": "^1.9.0"
59+
"unplugin": "^1.9.0",
60+
"piscina": "^4.4.0"
6061
},
6162
"peerDependencies": {
6263
"typescript": "^4.9.0 || ^5.0.0",

packages/native-federation-typescript/src/index.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ describe('index', () => {
7575
children: [
7676
{ name: 'archiveHandler.d.ts' },
7777
{ name: 'typeScriptCompiler.d.ts' },
78+
{
79+
name: 'writeBundle',
80+
children: [{ name: 'host.d.ts' }, { name: 'remote.d.ts' }],
81+
},
7882
],
7983
},
8084
],
@@ -237,6 +241,13 @@ describe('index', () => {
237241
children: [
238242
{ name: 'archiveHandler.d.ts' },
239243
{ name: 'typeScriptCompiler.d.ts' },
244+
{
245+
name: 'writeBundle',
246+
children: [
247+
{ name: 'host.d.ts' },
248+
{ name: 'remote.d.ts' },
249+
],
250+
},
240251
],
241252
},
242253
],

packages/native-federation-typescript/src/index.ts

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import ansiColors from 'ansi-colors';
2-
import { rm } from 'fs/promises';
32
import { resolve } from 'path';
43
import { mergeDeepRight } from 'rambda';
54
import { UnpluginOptions, createUnplugin } from 'unplugin';
@@ -8,31 +7,22 @@ import { retrieveHostConfig } from './configurations/hostPlugin';
87
import { retrieveRemoteConfig } from './configurations/remotePlugin';
98
import { HostOptions } from './interfaces/HostOptions';
109
import { RemoteOptions } from './interfaces/RemoteOptions';
11-
import { createTypesArchive, downloadTypesArchive } from './lib/archiveHandler';
12-
import {
13-
compileTs,
14-
retrieveMfTypesPath,
15-
retrieveOriginalOutDir,
16-
} from './lib/typeScriptCompiler';
10+
import { retrieveOriginalOutDir } from './lib/typeScriptCompiler';
11+
// @ts-expect-error function exported through module.exports
12+
import remoteWriteBundle from './lib/writeBundle/remote';
13+
// @ts-expect-error function exported through module.exports
14+
import hostWriteBundle from './lib/writeBundle/host';
1715

1816
export const NativeFederationTypeScriptRemote = createUnplugin(
1917
(options: RemoteOptions) => {
20-
const { remoteOptions, tsConfig, mapComponentsToExpose } =
21-
retrieveRemoteConfig(options);
18+
const remoteConfig = retrieveRemoteConfig(options);
19+
const { remoteOptions, tsConfig } = remoteConfig;
2220
return {
2321
name: 'native-federation-typescript/remote',
2422
async writeBundle() {
2523
try {
26-
compileTs(mapComponentsToExpose, tsConfig, remoteOptions);
24+
await remoteWriteBundle(remoteConfig);
2725

28-
await createTypesArchive(tsConfig, remoteOptions);
29-
30-
if (remoteOptions.deleteTypesFolder) {
31-
await rm(retrieveMfTypesPath(tsConfig, remoteOptions), {
32-
recursive: true,
33-
force: true,
34-
});
35-
}
3626
console.log(ansiColors.green('Federated types created correctly'));
3727
} catch (error) {
3828
console.error(
@@ -48,7 +38,7 @@ export const NativeFederationTypeScriptRemote = createUnplugin(
4838
watchChange: (this as UnpluginOptions).writeBundle,
4939
};
5040
},
51-
webpack: (compiler) => {
41+
webpack(compiler) {
5242
compiler.options.devServer = mergeDeepRight(
5343
compiler.options.devServer || {},
5444
{
@@ -60,7 +50,7 @@ export const NativeFederationTypeScriptRemote = createUnplugin(
6050
},
6151
);
6252
},
63-
rspack: (compiler) => {
53+
rspack(compiler) {
6454
compiler.options.devServer = mergeDeepRight(
6555
compiler.options.devServer || {},
6656
{
@@ -78,26 +68,12 @@ export const NativeFederationTypeScriptRemote = createUnplugin(
7868

7969
export const NativeFederationTypeScriptHost = createUnplugin(
8070
(options: HostOptions) => {
81-
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(options);
71+
const hostConfig = retrieveHostConfig(options);
8272
return {
8373
name: 'native-federation-typescript/host',
8474
async writeBundle() {
85-
if (hostOptions.deleteTypesFolder) {
86-
await rm(hostOptions.typesFolder, {
87-
recursive: true,
88-
force: true,
89-
}).catch((error) =>
90-
console.error(
91-
ansiColors.red(`Unable to remove types folder, ${error}`),
92-
),
93-
);
94-
}
95-
96-
const typesDownloader = downloadTypesArchive(hostOptions);
97-
const downloadPromises =
98-
Object.entries(mapRemotesToDownload).map(typesDownloader);
75+
await hostWriteBundle(hostConfig);
9976

100-
await Promise.allSettled(downloadPromises);
10177
console.log(ansiColors.green('Federated types extraction completed'));
10278
},
10379
get vite() {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Piscina from 'piscina';
2+
import { isMainThread } from 'worker_threads';
3+
import { rm } from 'fs/promises';
4+
import ansiColors from 'ansi-colors';
5+
6+
import { HostOptions } from '../../interfaces/HostOptions';
7+
import { downloadTypesArchive } from '../archiveHandler';
8+
9+
interface HostWriteBundle {
10+
hostOptions: Required<HostOptions>;
11+
mapRemotesToDownload: Record<string, string>;
12+
}
13+
14+
if (isMainThread) {
15+
const piscina = new Piscina({ filename: __filename });
16+
17+
module.exports = piscina.run.bind(piscina);
18+
} else {
19+
module.exports = async ({
20+
hostOptions,
21+
mapRemotesToDownload,
22+
}: HostWriteBundle) => {
23+
if (hostOptions.deleteTypesFolder) {
24+
await rm(hostOptions.typesFolder, {
25+
recursive: true,
26+
force: true,
27+
}).catch((error) =>
28+
console.error(
29+
ansiColors.red(`Unable to remove types folder, ${error}`),
30+
),
31+
);
32+
}
33+
34+
const typesDownloader = downloadTypesArchive(hostOptions);
35+
const downloadPromises =
36+
Object.entries(mapRemotesToDownload).map(typesDownloader);
37+
38+
await Promise.allSettled(downloadPromises);
39+
};
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Piscina from 'piscina';
2+
import ts from 'typescript';
3+
import { rm } from 'fs/promises';
4+
import { isMainThread } from 'worker_threads';
5+
6+
import { RemoteOptions } from '../../interfaces/RemoteOptions';
7+
import { compileTs, retrieveMfTypesPath } from '../typeScriptCompiler';
8+
import { createTypesArchive } from '../archiveHandler';
9+
10+
interface RemoteWriteBundle {
11+
remoteOptions: Required<RemoteOptions>;
12+
tsConfig: ts.CompilerOptions;
13+
mapComponentsToExpose: Record<string, string>;
14+
}
15+
16+
if (isMainThread) {
17+
const piscina = new Piscina({ filename: __filename });
18+
19+
module.exports = piscina.run.bind(piscina);
20+
} else {
21+
module.exports = async ({
22+
remoteOptions,
23+
tsConfig,
24+
mapComponentsToExpose,
25+
}: RemoteWriteBundle) => {
26+
compileTs(mapComponentsToExpose, tsConfig, remoteOptions);
27+
28+
await createTypesArchive(tsConfig, remoteOptions);
29+
30+
if (remoteOptions.deleteTypesFolder) {
31+
await rm(retrieveMfTypesPath(tsConfig, remoteOptions), {
32+
recursive: true,
33+
force: true,
34+
});
35+
}
36+
};
37+
}

pnpm-lock.yaml

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)