Skip to content

Commit

Permalink
feat(ng-esbuild):add call of compensateExports to output files
Browse files Browse the repository at this point in the history
  • Loading branch information
sedx committed Mar 12, 2024
1 parent 775db9d commit bf4467c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions libs/native-federation-core/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
BuildAdapterOptions,
BuildResult,
BuildKind,
EntryPoint,
} from './lib/core/build-adapter';
export { withNativeFederation } from './lib/config/with-native-federation';
export { buildForFederation } from './lib/core/build-for-federation';
Expand Down
5 changes: 5 additions & 0 deletions libs/native-federation-esbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"name": "@softarc/native-federation-esbuild",
"version": "2.0.8",
"type": "commonjs",
"repository": {
"type": "git",
"url": "angular-architects/module-federation-plugin.git",
"directory": "libs/native-federation-esbuild"
},
"dependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^13.3.0",
Expand Down
40 changes: 28 additions & 12 deletions libs/native-federation-esbuild/src/lib/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
BuildAdapter,
BuildAdapterOptions,
BuildResult,
EntryPoint,
logger,
} from '@softarc/native-federation/build';
import * as esbuild from 'esbuild';
import { rollup } from 'rollup';
Expand All @@ -25,10 +27,18 @@ export type ReplacementConfig = {
file: string;
};

type EntryPointWithMeta = EntryPoint & {
meta: {
isPkg: boolean;
originalFileName: string;
};
};

export interface EsBuildAdapterConfig {
plugins: esbuild.Plugin[];
fileReplacements?: Record<string, string | ReplacementConfig>;
skipRollup?: boolean;
/** Identify packages for which compensating missing named exports */
compensateExports?: RegExp[];
loader?: { [ext: string]: esbuild.Loader };
}
Expand All @@ -43,7 +53,8 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {

// TODO: Do we need to prepare packages anymore as esbuild has evolved?

for (const entryPoint of entryPoints) {
const preparedEntryPoints = entryPoints as EntryPointWithMeta[];
for (const entryPoint of preparedEntryPoints) {
const isPkg = entryPoint.fileName.includes('node_modules');
const pkgName = isPkg ? inferePkgName(entryPoint.fileName) : '';
const tmpFolder = `node_modules/.tmp/${pkgName}`;
Expand All @@ -56,13 +67,16 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
config,
!!options.dev
);

entryPoint.meta = {
originalFileName: entryPoint.fileName,
isPkg,
};
entryPoint.fileName = tmpFolder;
}
}

const ctx = await esbuild.context({
entryPoints: entryPoints.map((ep) => ({
entryPoints: preparedEntryPoints.map((ep) => ({
in: ep.fileName,
out: path.parse(ep.outName).name,
})),
Expand All @@ -82,16 +96,18 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
const result = await ctx.rebuild();
const writtenFiles = writeResult(result, outdir);
ctx.dispose();
preparedEntryPoints.forEach((entryPoint) => {
const { meta, fileName, outName } = entryPoint;
const normEntryPoint = meta.originalFileName.replace(/\\/g, '/');
if (
meta.isPkg &&
config?.compensateExports?.find((regExp) => regExp.exec(normEntryPoint))
) {
logger.verbose('compensate exports for ' + meta.originalFileName);
compensateExports(fileName, path.join(outdir, outName));
}
});
return writtenFiles.map((fileName) => ({ fileName }));

// const normEntryPoint = entryPoint.replace(/\\/g, '/');
// if (
// isPkg &&
// config?.compensateExports?.find((regExp) => regExp.exec(normEntryPoint))
// ) {
// logger.verbose('compensate exports for ' + tmpFolder);
// compensateExports(tmpFolder, outfile);
// }
};
}

Expand Down

0 comments on commit bf4467c

Please sign in to comment.