Skip to content

Commit

Permalink
feat(nf-core): enable watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
manfredsteyer committed Oct 1, 2022
1 parent 0813a76 commit 56e8558
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 32 deletions.
1 change: 1 addition & 0 deletions libs/native-federation-core/src/lib/core/build-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface BuildAdapterOptions {
mappedPaths: MappedPath[];
packageName?: string;
esm?: boolean;
watch?: boolean;
kind: 'shared-package' | 'shared-mapping' | 'exposed';
}

Expand Down
25 changes: 15 additions & 10 deletions libs/native-federation-core/src/lib/core/bundle-exposed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,25 @@ export async function bundleExposed(
tsConfigPath: options.tsConfig,
external: externals,
outfile: outFilePath,
watch: options.watch,
mappedPaths: config.sharedMappings,
kind: 'exposed',
});

const hash = hashFile(outFilePath);
const hashedOutFileName = `${key}-${hash}.js`;
const hashedOutFilePath = path.join(
options.outputPath,
hashedOutFileName
);

fs.renameSync(outFilePath, hashedOutFilePath);
let finalOutFileName = outFileName;
if (!options.watch) {
const hash = hashFile(outFilePath);
finalOutFileName = `${key}-${hash}.js`;
const hashedOutFilePath = path.join(
options.outputPath,
finalOutFileName
);
fs.renameSync(outFilePath, hashedOutFilePath);
}

result.push({
key,
outFileName: hashedOutFileName,
outFileName: finalOutFileName,
dev: !options.dev
? undefined
: {
Expand All @@ -56,7 +59,9 @@ export async function bundleExposed(
});
} catch (e) {
logger.error('Error bundling exposed module ' + entryPoint);
logger.notice('Please check the `exposes` section in your federation.config.js');
logger.notice(
'Please check the `exposes` section in your federation.config.js'
);
logger.error(e);
}
}
Expand Down
20 changes: 12 additions & 8 deletions libs/native-federation-core/src/lib/core/bundle-shared-mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ export async function bundleSharedMappings(
external: externals,
outfile: outFilePath,
mappedPaths: [],
watch: fedOptions.watch,
kind: 'shared-mapping',
});

const hash = hashFile(outFilePath);
const hashedOutFileName = `${key}-${hash}.js`;
const hashedOutFilePath = path.join(
fedOptions.outputPath,
hashedOutFileName
);
fs.renameSync(outFilePath, hashedOutFilePath);
let finalOutFileName = outFileName;
if (!fedOptions.watch) {
const hash = hashFile(outFilePath);
finalOutFileName = `${key}-${hash}.js`;
const hashedOutFilePath = path.join(
fedOptions.outputPath,
finalOutFileName
);
fs.renameSync(outFilePath, hashedOutFilePath);
}

result.push({
packageName: m.key,
outFileName: hashedOutFileName,
outFileName: finalOutFileName,
requiredVersion: '',
singleton: true,
strictVersion: false,
Expand Down
14 changes: 8 additions & 6 deletions libs/native-federation-core/src/lib/core/bundle-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export async function bundleShared(
// 'Make sure, you skip all unneeded packages in your federation.config.js!'
// );

const federationConfigPath = path.join(
fedOptions.workspaceRoot,
fedOptions.federationConfig
);
const hash = hashFile(federationConfigPath);
// const federationConfigPath = path.join(
// fedOptions.workspaceRoot,
// fedOptions.federationConfig
// );

//const hash = hashFile(federationConfigPath);

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

const outFileName = `${encName}-${encVersion}-${hash}.js`;
// const outFileName = `${encName}-${encVersion}-${hash}.js`;
const outFileName = `${encName}-${encVersion}.js`;

const cachePath = path.join(
fedOptions.workspaceRoot,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { NormalizedFederationConfig } from '../config/federation-config';
import { BuildAdapter, setBuildAdapter } from './build-adapter';
import {
buildForFederation,
defaultBuildParams,
} from './build-for-federation';
import { buildForFederation, defaultBuildParams } from './build-for-federation';
import { FederationOptions } from './federation-options';
import { getExternals } from './get-externals';
import { loadFederationConfig } from './load-federation-config';
Expand Down
11 changes: 11 additions & 0 deletions libs/native-federation-core/src/lib/utils/package-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ export function getPackageInfo(
}

let cand = mainPkgJson?.exports?.[relSecondaryPath]?.import;

if (typeof cand === 'object') {
if (cand.module) {
cand = cand.module;
} else if (cand.default) {
cand = cand.default;
} else {
cand = null;
}
}

if (cand) {
return {
entryPoint: path.join(mainPkgPath, cand),
Expand Down
14 changes: 13 additions & 1 deletion libs/native-federation-esbuild/src/lib/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BuildAdapter,
BuildAdapterOptions,
logger,
} from '@softarc/native-federation/build';
import * as esbuild from 'esbuild';
import { rollup } from 'rollup';
Expand All @@ -23,7 +24,7 @@ export interface EsBuildAdapterConfig {

export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
return async (options: BuildAdapterOptions) => {
const { entryPoint, external, outfile } = options;
const { entryPoint, external, outfile, watch } = options;

const isPkg = entryPoint.includes('node_modules');
const pkgName = isPkg ? inferePkgName(entryPoint) : '';
Expand All @@ -40,6 +41,17 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
bundle: true,
sourcemap: true,
minify: true,
watch: !watch
? false
: {
onRebuild: (err) => {
if (err) {
logger.error('Error rebuilding ' + entryPoint);
} else {
logger.info('Rebuilt ' + entryPoint);
}
},
},
format: 'esm',
target: ['esnext'],
plugins: [...config.plugins],
Expand Down
1 change: 1 addition & 0 deletions libs/native-federation/src/builders/build/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function runBuilder(
federationConfig: infereConfigPath(options.tsConfig),
tsConfig: options.tsConfig,
verbose: options.verbose,
watch: options.watch,
};

const config = await loadFederationConfig(fedOptions);
Expand Down
22 changes: 19 additions & 3 deletions libs/native-federation/src/utils/angular-esbuild-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@ import * as fs from 'fs';
import { PluginItem, transformAsync } from '@babel/core';

export const AngularEsBuildAdapter: BuildAdapter = async (options) => {
const { entryPoint, tsConfigPath, external, outfile, mappedPaths, kind } =
options;
const {
entryPoint,
tsConfigPath,
external,
outfile,
mappedPaths,
kind,
watch,
} = options;

if (kind === 'shared-package') {
await runRollup(entryPoint, external, outfile);
} else {
await runEsbuild(entryPoint, external, outfile, tsConfigPath, mappedPaths);
await runEsbuild(
entryPoint,
external,
outfile,
tsConfigPath,
mappedPaths,
watch
);
}
if (kind === 'shared-package' && fs.existsSync(outfile)) {
await link(outfile);
Expand Down Expand Up @@ -65,6 +79,7 @@ async function runEsbuild(
outfile: string,
tsConfigPath: string,
mappedPaths: MappedPath[],
watch?: boolean,
plugins: esbuild.Plugin[] | null = null,
absWorkingDir: string | undefined = undefined,
logLevel: esbuild.LogLevel = 'warning'
Expand All @@ -75,6 +90,7 @@ async function runEsbuild(
external,
outfile,
logLevel,
watch: !!watch,
bundle: true,
sourcemap: true,
minify: true,
Expand Down
Binary file added softarc-native-federation-1.0.0-beta.3.tgz
Binary file not shown.
Binary file not shown.

0 comments on commit 56e8558

Please sign in to comment.