Replace serialize-javascript by JSON.serialize

Signed-off-by: David Festal <dfestal@redhat.com>
This commit is contained in:
David Festal
2025-12-15 18:26:14 +01:00
parent 5f23e94397
commit 1f5dc0b787
3 changed files with 11 additions and 24 deletions
@@ -39,14 +39,12 @@
"@backstage/config": "workspace:^",
"@backstage/errors": "workspace:^",
"@backstage/types": "workspace:^",
"@module-federation/runtime": "^0.21.6",
"serialize-javascript": "^6.0.0"
"@module-federation/runtime": "^0.21.6"
},
"devDependencies": {
"@backstage/cli": "workspace:^",
"@backstage/test-utils": "workspace:^",
"@types/react": "^18.0.0",
"@types/serialize-javascript": "^5.0.4",
"react": "^18.0.2",
"react-dom": "^18.0.2",
"react-router-dom": "^6.3.0"
@@ -15,7 +15,6 @@
*/
import { Host, Runtime, SharedDependencies } from './types';
import { default as serialize } from 'serialize-javascript';
import type { UserOptions } from '@module-federation/runtime/types';
import { ForwardedError } from '@backstage/errors';
@@ -37,7 +36,8 @@ const BACKSTAGE_RUNTIME_SHARED_DEPENDENCIES_GLOBAL =
export function prepareRuntimeSharedDependenciesScript(
hostSharedDependencies: SharedDependencies<Host & { version: string }>,
) {
const runtimeSharedDependencies: SharedDependencies<Host & Runtime> =
type StringModule = Omit<Runtime, 'module'> & { module: string };
const runtimeSharedDependencies: SharedDependencies<Host & StringModule> =
Object.fromEntries(
Object.entries(hostSharedDependencies).map(([name, sharedDep]) => [
name,
@@ -48,20 +48,18 @@ export function prepareRuntimeSharedDependenciesScript(
? { singleton: sharedDep.singleton }
: {}),
...(sharedDep.eager !== undefined ? { eager: sharedDep.eager } : {}),
// eslint-disable-next-line no-new-func
module: new Function(
`return () => import('${name}')`,
)() as () => Promise<any>,
module: `() => import('${name}')`,
},
]),
);
return `window['${BACKSTAGE_RUNTIME_SHARED_DEPENDENCIES_GLOBAL}'] = ${serialize(
return `window['${BACKSTAGE_RUNTIME_SHARED_DEPENDENCIES_GLOBAL}'] = ${JSON.stringify(
runtimeSharedDependencies,
{
space: 2,
unsafe: true,
},
null,
2,
).replace(
/(^\s+"module":\s*)"([^"]+)"$/gm,
(_, start, unquoted) => `${start}${unquoted}`,
)};`;
}