From d21397ae2fa212384ccfc327e0d3b14668136186 Mon Sep 17 00:00:00 2001 From: targeral Date: Thu, 24 Oct 2024 19:17:54 +0800 Subject: [PATCH] feat: merge Initialisation Code --- .../plugin-runtime/scripts/gen-static.ts | 17 ++---- .../runtime/DeferredDataScripts.node.tsx | 54 ++++--------------- .../src/router/runtime/constants.ts | 9 +++- .../static/modern-define-init.js | 1 - .../plugin-runtime/static/modern-inline.js | 1 + .../static/router-data-attr-init-inline.js | 1 - 6 files changed, 21 insertions(+), 62 deletions(-) delete mode 100644 packages/runtime/plugin-runtime/static/modern-define-init.js create mode 100644 packages/runtime/plugin-runtime/static/modern-inline.js delete mode 100644 packages/runtime/plugin-runtime/static/router-data-attr-init-inline.js diff --git a/packages/runtime/plugin-runtime/scripts/gen-static.ts b/packages/runtime/plugin-runtime/scripts/gen-static.ts index ea506a979b02..6bfd3cca25d2 100644 --- a/packages/runtime/plugin-runtime/scripts/gen-static.ts +++ b/packages/runtime/plugin-runtime/scripts/gen-static.ts @@ -1,8 +1,7 @@ import path from 'path'; import { fs } from '@modern-js/utils'; import { - defineRunInlineAndRouterDataInit, - initRouterDataAttrs, + modernInline, runRouterDataFnStr, runWindowFnStr, } from '../src/router/runtime/constants'; @@ -11,18 +10,8 @@ import { const targetDir = path.join(__dirname, '../static'); await fs.ensureDir(targetDir); - const modernDefineInitPath = path.join(targetDir, 'modern-define-init.js'); - await fs.writeFile( - modernDefineInitPath, - defineRunInlineAndRouterDataInit, - 'utf-8', - ); - - const initRouterDataFilePath = path.join( - targetDir, - 'router-data-attr-init-inline.js', - ); - await fs.writeFile(initRouterDataFilePath, initRouterDataAttrs, 'utf-8'); + const modernDefineInitPath = path.join(targetDir, 'modern-inline.js'); + await fs.writeFile(modernDefineInitPath, modernInline, 'utf-8'); const runRouterDataFilePath = path.join( targetDir, diff --git a/packages/runtime/plugin-runtime/src/router/runtime/DeferredDataScripts.node.tsx b/packages/runtime/plugin-runtime/src/router/runtime/DeferredDataScripts.node.tsx index eaa4706702f9..c2dcad2476e1 100644 --- a/packages/runtime/plugin-runtime/src/router/runtime/DeferredDataScripts.node.tsx +++ b/packages/runtime/plugin-runtime/src/router/runtime/DeferredDataScripts.node.tsx @@ -5,13 +5,8 @@ import type { } from '@modern-js/runtime-utils/remix-router'; import { Await, useAsyncError } from '@modern-js/runtime-utils/router'; import { Suspense, useEffect, useMemo, useRef } from 'react'; -import { ROUTER_DATA_JSON_ID } from 'src/core/constants'; -import { - defineRunInlineAndRouterDataInit, - initRouterDataAttrs, - runRouterDataFnStr, - runWindowFnStr, -} from './constants'; +import { ROUTER_DATA_JSON_ID } from '../../core/constants'; +import { modernInline, runRouterDataFnStr, runWindowFnStr } from './constants'; import { serializeErrors } from './utils'; /** @@ -37,8 +32,6 @@ const DeferredDataScripts = (props?: { | [ string, string, - { fnName: string; fnArgs: any[]; fnRun: string; fnScriptSrc: string }, - string, { fnName: string; fnArgs: any[]; fnRun: string; fnScriptSrc: string }[], JSX.Element[], ] @@ -54,19 +47,11 @@ const DeferredDataScripts = (props?: { errors: serializeErrors(staticContext.errors), }; - const initialScript0 = defineRunInlineAndRouterDataInit; - // - const initialScript1 = inlineScript ? '' : `${serializeJson(_ROUTER_DATA)}`; + const initialScript0 = inlineScript ? '' : `${serializeJson(_ROUTER_DATA)}`; - const initialScript2 = { - fnName: 'initRouterData', - fnArgs: [ROUTER_DATA_JSON_ID], - fnRun: runWindowFnStr, - fnScriptSrc: 'modern-run-window-fn', - }; + const initialScript1 = modernInline; - const initialScript3 = initRouterDataAttrs; const deferredDataScripts: JSX.Element[] = []; const initialScripts = Object.entries(activeDeferreds).map( ([routeId, deferredData]) => { @@ -133,8 +118,6 @@ const DeferredDataScripts = (props?: { return [ initialScript0, initialScript1, - initialScript2, - initialScript3, initialScripts, deferredDataScripts, ]; @@ -148,41 +131,24 @@ const DeferredDataScripts = (props?: { <> {!hydratedRef.current && ( <> -