Skip to content
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
75 changes: 75 additions & 0 deletions packages/angular/src/schematics/update-v4/schematic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { HostTree, type SchematicContext } from '@angular-devkit/schematics';

import updateV4 from './schematic.js';

const V4_CONFIG_IMPORT =
"import { withNativeFederation, shareAll } from '@angular-architects/native-federation-v4/config';";

describe('update-v4 schematic', () => {
let tree: HostTree;

beforeEach(() => {
vi.spyOn(console, 'log').mockImplementation(() => undefined);
tree = new HostTree();
tree.create(
'angular.json',
JSON.stringify({
projects: {
shell: { root: '', sourceRoot: 'src', architect: {} },
},
})
);
});

afterEach(() => {
vi.restoreAllMocks();
});

async function runSchematic(): Promise<void> {
await updateV4({ project: 'shell' })(tree, {} as SchematicContext);
}

it('migrates a CJS federation.config.js to ESM with a single -v4 suffix', async () => {
tree.create(
'federation.config.js',
[
"const { withNativeFederation, shareAll } = require('@angular-architects/native-federation/config');",
'',
'module.exports = withNativeFederation({',
" name: 'shell',",
' shared: {',
' ...shareAll({ singleton: true }),',
' },',
'});',
'',
].join('\n')
);

await runSchematic();

expect(tree.exists('federation.config.mjs')).toBe(true);
const content = tree.readText('federation.config.mjs');
expect(content).toContain(V4_CONFIG_IMPORT);
expect(content).not.toContain('native-federation-v4-v4');
expect(content).toContain('export default withNativeFederation({');
});

it('leaves an already-updated v4 require path untouched', async () => {
tree.create(
'federation.config.js',
[
"const { withNativeFederation, shareAll } = require('@angular-architects/native-federation-v4/config');",
'',
'module.exports = withNativeFederation({});',
'',
].join('\n')
);

await runSchematic();

expect(tree.exists('federation.config.mjs')).toBe(true);
const content = tree.readText('federation.config.mjs');
expect(content).toContain(V4_CONFIG_IMPORT);
expect(content).not.toContain('native-federation-v4-v4');
});
});
7 changes: 5 additions & 2 deletions packages/angular/src/schematics/update-v4/schematic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ function migrateFederationConfigs(tree: Tree, workspace: any, options: UpdateV4S
const requireRegex = /const\s+(\{[^}]+\})\s*=\s*require\(\s*['"]([^'"]+)['"]\s*\)\s*;?/g;
const imports: string[] = [];
content = content.replace(requireRegex, (_match, bindings: string, modulePath: string) => {
const updatedPath = modulePath.replace(V3_PACKAGE, V4_PACKAGE);
const updatedPath = modulePath.replace(
new RegExp(escapeRegExp(V3_PACKAGE) + '(?!-v4)'),
V4_PACKAGE
);
imports.push(`import ${bindings} from '${updatedPath}';`);
return ''; // Remove the require line; import will be prepended
});
Expand All @@ -105,7 +108,7 @@ function migrateFederationConfigs(tree: Tree, workspace: any, options: UpdateV4S
new RegExp(escapeRegExp(V3_PACKAGE + '/config'), 'g'),
V4_PACKAGE + '/config'
);
content = content.replace(new RegExp(escapeRegExp(V3_PACKAGE) + '(?!/)', 'g'), V4_PACKAGE);
content = content.replace(new RegExp(escapeRegExp(V3_PACKAGE) + '(?!/|-v4)', 'g'), V4_PACKAGE);

if (content !== originalContent) {
tree.delete(configPath);
Expand Down