From 84c3fd66179c1531e8da68b49f60b92e72314b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3rur=20Zachariasen?= Date: Tue, 12 Dec 2023 16:15:55 +0100 Subject: [PATCH] =?UTF-8?q?fix(react):=20skip=20adding=20comma=20to=20conf?= =?UTF-8?q?ig=20when=20adding=20remote=20to=20host=20if=E2=80=A6=20(#20620?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/module-federation/ast-utils.spec.ts | 35 +++++++++++++++++++ .../react/src/module-federation/ast-utils.ts | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/react/src/module-federation/ast-utils.spec.ts b/packages/react/src/module-federation/ast-utils.spec.ts index 0b79423575fc9..470a3d7149009 100644 --- a/packages/react/src/module-federation/ast-utils.spec.ts +++ b/packages/react/src/module-federation/ast-utils.spec.ts @@ -89,6 +89,41 @@ describe('addRemoteToConfig', () => { expect(result).toEqual(sourceCode); }); + + it('should not add comma if the existing array has a trailing comma', async () => { + const sourceCode = stripIndents` + module.exports = { + name: 'shell', + remotes: [ + 'app1', + 'app2', + ] + }; + `; + + const source = ts.createSourceFile( + '/module-federation.config.js', + sourceCode, + ts.ScriptTarget.Latest, + true + ); + + const result = applyChangesToString( + sourceCode, + addRemoteToConfig(source, 'new-app') + ); + + expect(result).toEqual(stripIndents` + module.exports = { + name: 'shell', + remotes: [ + 'app1', + 'app2', + 'new-app', + ] + }; + `); + }); }); describe('addRemoteDefinition', () => { diff --git a/packages/react/src/module-federation/ast-utils.ts b/packages/react/src/module-federation/ast-utils.ts index c00d475304da5..221d7e70cbbfe 100644 --- a/packages/react/src/module-federation/ast-utils.ts +++ b/packages/react/src/module-federation/ast-utils.ts @@ -37,7 +37,7 @@ export function addRemoteToConfig( const lastElement = arrayExpression.elements[arrayExpression.elements.length - 1]; return [ - lastElement + lastElement && !arrayExpression.elements.hasTrailingComma ? { type: ChangeType.Insert, index: lastElement.end,