Skip to content
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

feat(ng-esbuild):add call of compensateExports to output files #501

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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
79 changes: 48 additions & 31 deletions libs/native-federation-esbuild/src/lib/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import {
BuildAdapter,
BuildAdapterOptions,
BuildResult,
EntryPoint,
logger,
} from '@softarc/native-federation/build';
import * as esbuild from 'esbuild';
import { rollup } from 'rollup';
import resolve from '@rollup/plugin-node-resolve';
import { externals } from 'rollup-plugin-node-externals';
import * as fs from 'fs';
import path from 'path';
import { collectExports } from './collect-exports';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const commonjs = require('@rollup/plugin-commonjs');
Expand All @@ -24,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 @@ -42,11 +53,17 @@ 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}`;

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

if (isPkg) {
await prepareNodePackage(
entryPoint.fileName,
Expand All @@ -55,13 +72,12 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
config,
!!options.dev
);

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 @@ -81,16 +97,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 All @@ -110,25 +128,24 @@ function writeResult(
return writtenFiles;
}

// TODO: Unused, to delete?
// function compensateExports(entryPoint: string, outfile?: string): void {
// const inExports = collectExports(entryPoint);
// const outExports = outfile ? collectExports(outfile) : inExports;
//
// if (!outExports.hasDefaultExport || outExports.hasFurtherExports) {
// return;
// }
// const defaultName = outExports.defaultExportName;
//
// let exports = '/*Try to compensate missing exports*/\n\n';
// for (const exp of inExports.exports) {
// exports += `let ${exp}$softarc = ${defaultName}.${exp};\n`;
// exports += `export { ${exp}$softarc as ${exp} };\n`;
// }
//
// const target = outfile ?? entryPoint;
// fs.appendFileSync(target, exports, 'utf-8');
// }
function compensateExports(entryPoint: string, outfile?: string): void {
const inExports = collectExports(entryPoint);
const outExports = outfile ? collectExports(outfile) : inExports;

if (!outExports.hasDefaultExport || outExports.hasFurtherExports) {
return;
}
const defaultName = outExports.defaultExportName;

let exports = '/*Try to compensate missing exports*/\n\n';
for (const exp of inExports.exports) {
exports += `let ${exp}$softarc = ${defaultName}.${exp};\n`;
exports += `export { ${exp}$softarc as ${exp} };\n`;
}

const target = outfile ?? entryPoint;
fs.appendFileSync(target, exports, 'utf-8');
}

async function prepareNodePackage(
entryPoint: string,
Expand Down