Skip to content

Commit 56e8558

Browse files
committed
feat(nf-core): enable watch mode
1 parent 0813a76 commit 56e8558

File tree

11 files changed

+81
-32
lines changed

11 files changed

+81
-32
lines changed

libs/native-federation-core/src/lib/core/build-adapter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface BuildAdapterOptions {
1414
mappedPaths: MappedPath[];
1515
packageName?: string;
1616
esm?: boolean;
17+
watch?: boolean;
1718
kind: 'shared-package' | 'shared-mapping' | 'exposed';
1819
}
1920

libs/native-federation-core/src/lib/core/bundle-exposed.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ export async function bundleExposed(
3232
tsConfigPath: options.tsConfig,
3333
external: externals,
3434
outfile: outFilePath,
35+
watch: options.watch,
3536
mappedPaths: config.sharedMappings,
3637
kind: 'exposed',
3738
});
3839

39-
const hash = hashFile(outFilePath);
40-
const hashedOutFileName = `${key}-${hash}.js`;
41-
const hashedOutFilePath = path.join(
42-
options.outputPath,
43-
hashedOutFileName
44-
);
45-
46-
fs.renameSync(outFilePath, hashedOutFilePath);
40+
let finalOutFileName = outFileName;
41+
if (!options.watch) {
42+
const hash = hashFile(outFilePath);
43+
finalOutFileName = `${key}-${hash}.js`;
44+
const hashedOutFilePath = path.join(
45+
options.outputPath,
46+
finalOutFileName
47+
);
48+
fs.renameSync(outFilePath, hashedOutFilePath);
49+
}
4750

4851
result.push({
4952
key,
50-
outFileName: hashedOutFileName,
53+
outFileName: finalOutFileName,
5154
dev: !options.dev
5255
? undefined
5356
: {
@@ -56,7 +59,9 @@ export async function bundleExposed(
5659
});
5760
} catch (e) {
5861
logger.error('Error bundling exposed module ' + entryPoint);
59-
logger.notice('Please check the `exposes` section in your federation.config.js');
62+
logger.notice(
63+
'Please check the `exposes` section in your federation.config.js'
64+
);
6065
logger.error(e);
6166
}
6267
}

libs/native-federation-core/src/lib/core/bundle-shared-mappings.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,24 @@ export async function bundleSharedMappings(
2929
external: externals,
3030
outfile: outFilePath,
3131
mappedPaths: [],
32+
watch: fedOptions.watch,
3233
kind: 'shared-mapping',
3334
});
3435

35-
const hash = hashFile(outFilePath);
36-
const hashedOutFileName = `${key}-${hash}.js`;
37-
const hashedOutFilePath = path.join(
38-
fedOptions.outputPath,
39-
hashedOutFileName
40-
);
41-
fs.renameSync(outFilePath, hashedOutFilePath);
36+
let finalOutFileName = outFileName;
37+
if (!fedOptions.watch) {
38+
const hash = hashFile(outFilePath);
39+
finalOutFileName = `${key}-${hash}.js`;
40+
const hashedOutFilePath = path.join(
41+
fedOptions.outputPath,
42+
finalOutFileName
43+
);
44+
fs.renameSync(outFilePath, hashedOutFilePath);
45+
}
4246

4347
result.push({
4448
packageName: m.key,
45-
outFileName: hashedOutFileName,
49+
outFileName: finalOutFileName,
4650
requiredVersion: '',
4751
singleton: true,
4852
strictVersion: false,

libs/native-federation-core/src/lib/core/bundle-shared.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ export async function bundleShared(
2626
// 'Make sure, you skip all unneeded packages in your federation.config.js!'
2727
// );
2828

29-
const federationConfigPath = path.join(
30-
fedOptions.workspaceRoot,
31-
fedOptions.federationConfig
32-
);
33-
const hash = hashFile(federationConfigPath);
29+
// const federationConfigPath = path.join(
30+
// fedOptions.workspaceRoot,
31+
// fedOptions.federationConfig
32+
// );
33+
34+
//const hash = hashFile(federationConfigPath);
3435

3536
let first = true;
3637
for (const pi of packageInfos) {
@@ -39,7 +40,8 @@ export async function bundleShared(
3940
const encName = pi.packageName.replace(/[^A-Za-z0-9]/g, '_');
4041
const encVersion = pi.version.replace(/[^A-Za-z0-9]/g, '_');
4142

42-
const outFileName = `${encName}-${encVersion}-${hash}.js`;
43+
// const outFileName = `${encName}-${encVersion}-${hash}.js`;
44+
const outFileName = `${encName}-${encVersion}.js`;
4345

4446
const cachePath = path.join(
4547
fedOptions.workspaceRoot,

libs/native-federation-core/src/lib/core/federation-builder.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { NormalizedFederationConfig } from '../config/federation-config';
22
import { BuildAdapter, setBuildAdapter } from './build-adapter';
3-
import {
4-
buildForFederation,
5-
defaultBuildParams,
6-
} from './build-for-federation';
3+
import { buildForFederation, defaultBuildParams } from './build-for-federation';
74
import { FederationOptions } from './federation-options';
85
import { getExternals } from './get-externals';
96
import { loadFederationConfig } from './load-federation-config';

libs/native-federation-core/src/lib/utils/package-info.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ export function getPackageInfo(
5353
}
5454

5555
let cand = mainPkgJson?.exports?.[relSecondaryPath]?.import;
56+
57+
if (typeof cand === 'object') {
58+
if (cand.module) {
59+
cand = cand.module;
60+
} else if (cand.default) {
61+
cand = cand.default;
62+
} else {
63+
cand = null;
64+
}
65+
}
66+
5667
if (cand) {
5768
return {
5869
entryPoint: path.join(mainPkgPath, cand),

libs/native-federation-esbuild/src/lib/adapter.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
BuildAdapter,
33
BuildAdapterOptions,
4+
logger,
45
} from '@softarc/native-federation/build';
56
import * as esbuild from 'esbuild';
67
import { rollup } from 'rollup';
@@ -23,7 +24,7 @@ export interface EsBuildAdapterConfig {
2324

2425
export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
2526
return async (options: BuildAdapterOptions) => {
26-
const { entryPoint, external, outfile } = options;
27+
const { entryPoint, external, outfile, watch } = options;
2728

2829
const isPkg = entryPoint.includes('node_modules');
2930
const pkgName = isPkg ? inferePkgName(entryPoint) : '';
@@ -40,6 +41,17 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
4041
bundle: true,
4142
sourcemap: true,
4243
minify: true,
44+
watch: !watch
45+
? false
46+
: {
47+
onRebuild: (err) => {
48+
if (err) {
49+
logger.error('Error rebuilding ' + entryPoint);
50+
} else {
51+
logger.info('Rebuilt ' + entryPoint);
52+
}
53+
},
54+
},
4355
format: 'esm',
4456
target: ['esnext'],
4557
plugins: [...config.plugins],

libs/native-federation/src/builders/build/builder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export async function runBuilder(
4040
federationConfig: infereConfigPath(options.tsConfig),
4141
tsConfig: options.tsConfig,
4242
verbose: options.verbose,
43+
watch: options.watch,
4344
};
4445

4546
const config = await loadFederationConfig(fedOptions);

libs/native-federation/src/utils/angular-esbuild-adapter.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@ import * as fs from 'fs';
1111
import { PluginItem, transformAsync } from '@babel/core';
1212

1313
export const AngularEsBuildAdapter: BuildAdapter = async (options) => {
14-
const { entryPoint, tsConfigPath, external, outfile, mappedPaths, kind } =
15-
options;
14+
const {
15+
entryPoint,
16+
tsConfigPath,
17+
external,
18+
outfile,
19+
mappedPaths,
20+
kind,
21+
watch,
22+
} = options;
1623

1724
if (kind === 'shared-package') {
1825
await runRollup(entryPoint, external, outfile);
1926
} else {
20-
await runEsbuild(entryPoint, external, outfile, tsConfigPath, mappedPaths);
27+
await runEsbuild(
28+
entryPoint,
29+
external,
30+
outfile,
31+
tsConfigPath,
32+
mappedPaths,
33+
watch
34+
);
2135
}
2236
if (kind === 'shared-package' && fs.existsSync(outfile)) {
2337
await link(outfile);
@@ -65,6 +79,7 @@ async function runEsbuild(
6579
outfile: string,
6680
tsConfigPath: string,
6781
mappedPaths: MappedPath[],
82+
watch?: boolean,
6883
plugins: esbuild.Plugin[] | null = null,
6984
absWorkingDir: string | undefined = undefined,
7085
logLevel: esbuild.LogLevel = 'warning'
@@ -75,6 +90,7 @@ async function runEsbuild(
7590
external,
7691
outfile,
7792
logLevel,
93+
watch: !!watch,
7894
bundle: true,
7995
sourcemap: true,
8096
minify: true,
22.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)