Skip to content

Commit 7e2fa2f

Browse files
committed
feat(nf): run ngcc for older libs if needed
1 parent b8dd205 commit 7e2fa2f

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ export {
2222
BuildHelperParams,
2323
} from './lib/core/federation-builder';
2424
export { logger, setLogLevel } from './lib/utils/logger';
25+
export { hashFile } from './lib/utils/hash-file';

libs/native-federation-core/src/lib/utils/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ log.addLevel(
2323
'verbose',
2424
levels.verbose,
2525
{ fg: 'brightWhite', bg: 'brightBlue' },
26-
' MORE '
26+
' VRB! '
2727
);
2828
log.addLevel('silly', levels.silly, { fg: 'black', bg: 'white' }, ' DBG! ');
2929

libs/native-federation/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"@babel/core": "^7.19.0",
2626
"@softarc/native-federation": "1.0.0",
2727
"@softarc/native-federation-runtime": "1.0.0",
28-
"@rollup/plugin-json": "^4.1.0"
28+
"@rollup/plugin-json": "^4.1.0",
29+
"cross-spawn": "^7.0.3"
2930
},
3031
"peerDependencies": {
3132
"@angular/compiler-cli": "*"

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { ExecutionTransformer } from '@angular-architects/build-angular';
1010
import * as path from 'path';
1111
import * as fs from 'fs';
1212
import {
13+
hashFile,
14+
logger,
1315
NormalizedFederationConfig,
1416
setLogLevel,
1517
} from '@softarc/native-federation/build';
@@ -23,6 +25,8 @@ import { getExternals } from '@softarc/native-federation/build';
2325
import { loadFederationConfig } from '@softarc/native-federation/build';
2426
import { buildForFederation } from '@softarc/native-federation/build';
2527

28+
import * as crossSpawn from 'cross-spawn';
29+
2630
export async function runBuilder(
2731
options: Schema,
2832
context: BuilderContext
@@ -41,6 +45,8 @@ export async function runBuilder(
4145
const config = await loadFederationConfig(fedOptions);
4246
const externals = getExternals(config);
4347

48+
runNgccIfNeeded(fedOptions, fedOptions.workspaceRoot);
49+
4450
options.externalDependencies = externals.filter((e) => e !== 'tslib');
4551
const output = await build(config, options, context);
4652

@@ -53,6 +59,83 @@ export async function runBuilder(
5359

5460
export default createBuilder(runBuilder);
5561

62+
function runNgccIfNeeded(fedOptions: FederationOptions, workspaceRoot: string) {
63+
const hash = getLockFileHash(fedOptions);
64+
const skip = skipNgcc(hash, workspaceRoot);
65+
66+
if (!skip) {
67+
runNgcc(workspaceRoot);
68+
writeNgccLock(hash, workspaceRoot);
69+
}
70+
}
71+
72+
function runNgcc(workspaceRoot: string) {
73+
logger.verbose('Running ngcc');
74+
const command = getNpxCommand(workspaceRoot);
75+
const result = crossSpawn.sync(command, [
76+
'ngcc',
77+
'--async',
78+
'--create-ivy-entry-points',
79+
'--first-only'
80+
], { stdio: 'inherit' });
81+
82+
if (result.status !== 0) {
83+
const error = result.error || '';
84+
logger.error('Error running ngcc: ' + error);
85+
}
86+
}
87+
88+
function getLockFileHash(fedOptions: FederationOptions) {
89+
const lockFileName = getLockFileName(fedOptions.workspaceRoot);
90+
const hash = hashFile(lockFileName);
91+
return hash;
92+
}
93+
94+
const NGCC_LOCK_DIR = 'node_modules/.cache/native-federation/ngcc';
95+
96+
function skipNgcc(hash: string, workspaceRoot: string) {
97+
98+
const ngccLockFileDir = path.join(workspaceRoot, NGCC_LOCK_DIR);
99+
const ngccLockFileName = path.join(ngccLockFileDir, hash + '.lock');
100+
101+
let exists = false;
102+
if (fs.existsSync(ngccLockFileName)) {
103+
exists = true;
104+
}
105+
return exists;
106+
}
107+
108+
function writeNgccLock(hash: string, workspaceRoot: string) {
109+
const ngccLockFileDir = path.join(workspaceRoot, NGCC_LOCK_DIR);
110+
const ngccLockFileName = path.join(ngccLockFileDir, hash + '.lock');
111+
112+
if (!fs.existsSync(ngccLockFileDir)) {
113+
fs.mkdirSync(ngccLockFileDir, { recursive: true });
114+
}
115+
116+
fs.writeFileSync(ngccLockFileName, '');
117+
}
118+
119+
function getLockFileName(workspaceRoot: string) {
120+
let candPath = '';
121+
for (const candLock of ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml']) {
122+
candPath = path.join(workspaceRoot, candLock);
123+
if (fs.existsSync(candPath)) {
124+
break;
125+
}
126+
}
127+
return candPath;
128+
}
129+
130+
function getNpxCommand(workspaceRoot: string): string {
131+
switch (getLockFileName(workspaceRoot)) {
132+
case 'package-lock.json': return 'npx';
133+
case 'yarn.lock': return 'yarn';
134+
case 'pnpm-lock.yaml': return 'pnpx';
135+
default: return 'npx';
136+
}
137+
}
138+
56139
function updateIndexHtml(fedOptions: FederationOptions) {
57140
const outputPath = path.join(fedOptions.workspaceRoot, fedOptions.outputPath);
58141
const indexPath = path.join(outputPath, 'index.html');

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@rollup/plugin-json": "^4.1.0",
4444
"@softarc/native-federation": "file:softarc-native-federation-0.9.2-beta.3.tgz",
4545
"@types/browser-sync": "^2.26.3",
46+
"@types/cross-spawn": "^6.0.2",
4647
"es-module-shims": "^1.5.12",
4748
"npmlog": "^6.0.2",
4849
"rxjs": "~6.6.3",

0 commit comments

Comments
 (0)