Skip to content

Commit

Permalink
refactor: split runtime utils into a seperate package (#4636)
Browse files Browse the repository at this point in the history
* refactor: split runtime utils into a seperate package

* chore: update lockfile

* chore: changeset
  • Loading branch information
xuchaobei committed Sep 14, 2023
1 parent 578f94f commit 6a1d46e
Show file tree
Hide file tree
Showing 68 changed files with 431 additions and 181 deletions.
14 changes: 14 additions & 0 deletions .changeset/thin-ducks-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@modern-js/plugin-router-v5': patch
'@modern-js/plugin-data-loader': patch
'@modern-js/runtime': patch
'@modern-js/create-request': patch
'@modern-js/runtime-utils': patch
'@modern-js/app-tools': patch
'@modern-js/prod-server': patch
'@modern-js/server': patch
'@modern-js/utils': patch
---

refactor: split runtime utils into a seperate package
refactor: runtime utils 单独拆分成一个包
1 change: 1 addition & 0 deletions packages/cli/plugin-data-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"dependencies": {
"@babel/core": "^7.22.15",
"@modern-js/utils": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"@remix-run/node": "^1.12.0",
"path-to-regexp": "^6.2.0",
"@swc/helpers": "0.5.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/plugin-data-loader/src/cli/createRequest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable node/prefer-global/url */
// Todo move this file to `runtime/` dir
import { compile } from 'path-to-regexp';
import { redirect } from '@modern-js/utils/runtime/router';
import { type UNSAFE_DeferredData as DeferredData } from '@modern-js/utils/runtime/remix-router';
import { redirect } from '@modern-js/runtime-utils/router';
import { type UNSAFE_DeferredData as DeferredData } from '@modern-js/runtime-utils/remix-router';
import {
LOADER_ID_PARAM,
DIRECT_PARAM,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/plugin-data-loader/src/cli/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import {
UNSAFE_DeferredData as DeferredData,
AbortedDeferredError,
} from '@modern-js/utils/runtime/remix-router';
} from '@modern-js/runtime-utils/remix-router';

const DEFERRED_VALUE_PLACEHOLDER_PREFIX = '__deferred_promise:';
export async function parseDeferredReadableStream(
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/plugin-data-loader/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import {
ErrorResponse,
UNSAFE_DEFERRED_SYMBOL as DEFERRED_SYMBOL,
type UNSAFE_DeferredData as DeferredData,
} from '@modern-js/utils/runtime/remix-router';
import { transformNestedRoutes } from '@modern-js/utils/runtime/nested-routes';
} from '@modern-js/runtime-utils/remix-router';
import { transformNestedRoutes } from '@modern-js/runtime-utils/browser';
import { isPlainObject } from '@modern-js/utils/lodash';
import {
matchEntry,
ServerContext,
createRequestContext,
reporterCtx,
} from '@modern-js/utils/runtime-node';
import { time } from '@modern-js/utils/universal/time';
} from '@modern-js/runtime-utils/node';
import { time } from '@modern-js/runtime-utils/time';
import { LOADER_REPORTER_NAME } from '@modern-js/utils/universal/constants';
import { CONTENT_TYPE_DEFERRED, LOADER_ID_PARAM } from '../common/constants';
import { createDeferredReadableStream } from './response';
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/plugin-data-loader/src/runtime/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { TextEncoder } from 'util';
import {
type UNSAFE_DeferredData as DeferredData,
type TrackedPromise,
} from '@modern-js/utils/runtime/remix-router';
import { serializeJson } from '@modern-js/utils/runtime-node';
} from '@modern-js/runtime-utils/remix-router';
import { serializeJson } from '@modern-js/runtime-utils/node';

function isTrackedPromise(value: any): value is TrackedPromise {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/plugin-data-loader/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs';
import type { ServerPlugin } from '@modern-js/server-core';
import type { ServerRoute } from '@modern-js/types';
import { MAIN_ENTRY_NAME, SERVER_BUNDLE_DIRECTORY } from '@modern-js/utils';
import { matchEntry, ServerContext } from '@modern-js/utils/runtime-node';
import { matchEntry, ServerContext } from '@modern-js/runtime-utils/node';

export default (): ServerPlugin => ({
name: '@modern-js/plugin-data-loader',
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/plugin-router-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"@modern-js/core": "workspace:*",
"@modern-js/runtime": "workspace:*",
"@modern-js/utils": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"@scripts/build": "workspace:*",
"@scripts/jest-config": "workspace:*",
"@testing-library/react": "^13.4.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/plugin-router-v5/src/runtime/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import hoistNonReactStatics from 'hoist-non-react-statics';
import { RuntimeReactContext, isBrowser } from '@modern-js/runtime';
import type { Plugin } from '@modern-js/runtime';
import { parsedJSONFromElement } from '@modern-js/utils/runtime-browser';
import { parsedJSONFromElement } from '@modern-js/runtime-utils/browser';
import { renderRoutes, getLocation, urlJoin } from './utils';
import { modifyRoutesHook } from './hooks';

Expand Down
1 change: 1 addition & 0 deletions packages/runtime/plugin-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"@modern-js/plugin": "workspace:*",
"@modern-js/types": "workspace:*",
"@modern-js/utils": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"@remix-run/node": "^1.12.0",
"@types/loadable__component": "^5.13.4",
"@types/react-helmet": "^6.1.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable react/no-danger */
import { type TrackedPromise } from '@modern-js/utils/runtime/remix-router';
import { type TrackedPromise } from '@modern-js/runtime-utils/remix-router';
import { Suspense, useEffect, useRef, useMemo, useContext } from 'react';
import {
Await,
UNSAFE_DataRouterContext as DataRouterContext,
useAsyncError,
} from '@modern-js/utils/runtime/router';
import { serializeJson } from '@modern-js/utils/runtime-node';
} from '@modern-js/runtime-utils/router';
import { serializeJson } from '@modern-js/runtime-utils/node';
import { JSX_SHELL_STREAM_END_MARK } from '../../common';
import { serializeErrors } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
useMatches,
NavLink as RouterNavLink,
NavLinkProps as RouterNavLinkProps,
} from '@modern-js/utils/runtime/router';
} from '@modern-js/runtime-utils/router';
import { RuntimeReactContext } from '../../core';
import { RouteAssets, RouteManifest } from './types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createWaterfall } from '@modern-js/plugin';
import { RouteObject } from '@modern-js/utils/runtime/router';
import { RouteObject } from '@modern-js/runtime-utils/router';

const modifyRoutes = createWaterfall<RouteObject[]>();

Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/plugin-runtime/src/router/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRouteLoaderData as useRouteData } from '@modern-js/utils/runtime/router';
import { useRouteLoaderData as useRouteData } from '@modern-js/runtime-utils/router';
import { routerPlugin } from './plugin';
import type { SingleRouteConfig, RouterConfig } from './types';

Expand Down Expand Up @@ -79,7 +79,7 @@ export type {
Search,
ShouldRevalidateFunction,
To,
} from '@modern-js/utils/runtime/router';
} from '@modern-js/runtime-utils/router';

// Note: Keep in sync with react-router-dom exports!
export {
Expand Down Expand Up @@ -148,4 +148,4 @@ export {
defer,
json,
redirect,
} from '@modern-js/utils/runtime/router';
} from '@modern-js/runtime-utils/router';
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React, { useContext } from 'react';
import { createStaticHandler } from '@modern-js/utils/runtime/remix-router';
import { createStaticHandler } from '@modern-js/runtime-utils/remix-router';
import {
createStaticRouter,
StaticRouterProvider,
} from '@modern-js/utils/runtime-node/router';
} from '@modern-js/runtime-utils/node/router';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { createRoutesFromElements } from '@modern-js/utils/runtime/router';
import { createRoutesFromElements } from '@modern-js/runtime-utils/router';
import {
createRequestContext,
reporterCtx,
} from '@modern-js/utils/runtime-node';
import { time } from '@modern-js/utils/universal/time';
} from '@modern-js/runtime-utils/node';
import { time } from '@modern-js/runtime-utils/time';
import { LOADER_REPORTER_NAME } from '@modern-js/utils/universal/constants';
import { RuntimeReactContext } from '../../core';
import type { Plugin } from '../../core';
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/plugin-runtime/src/router/runtime/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
useMatches,
useLocation,
RouteObject,
} from '@modern-js/utils/runtime/router';
} from '@modern-js/runtime-utils/router';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { parsedJSONFromElement } from '@modern-js/utils/runtime-browser';
import type { RouterSubscriber } from '@modern-js/utils/runtime/remix-router';
import { parsedJSONFromElement } from '@modern-js/runtime-utils/browser';
import type { RouterSubscriber } from '@modern-js/runtime-utils/remix-router';
import { Plugin, RuntimeReactContext } from '../../core';
import { modifyRoutes as modifyRoutesHook } from './hooks';
import { deserializeErrors, renderRoutes, urlJoin } from './utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from '@modern-js/utils/runtime-node/router';
export * from '@modern-js/runtime-utils/node/router';
4 changes: 2 additions & 2 deletions packages/runtime/plugin-runtime/src/router/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type {
RouteProps,
RouteObject,
Params,
} from '@modern-js/utils/runtime/router';
} from '@modern-js/runtime-utils/router';
import { PageRoute, NestedRoute } from '@modern-js/types';
import type { RequestContext } from '@modern-js/utils/runtime-node';
import type { RequestContext } from '@modern-js/runtime-utils/node';

declare global {
interface Window {
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/plugin-runtime/src/router/runtime/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Route, isRouteErrorResponse } from '@modern-js/utils/runtime/router';
import { Route, isRouteErrorResponse } from '@modern-js/runtime-utils/router';
import type {
Reporter,
NestedRoute,
Expand All @@ -10,8 +10,8 @@ import {
ErrorResponse,
type StaticHandlerContext,
type Router,
} from '@modern-js/utils/runtime/remix-router';
import { renderNestedRoute } from '@modern-js/utils/runtime/nested-routes';
} from '@modern-js/runtime-utils/remix-router';
import { renderNestedRoute } from '@modern-js/runtime-utils/browser';
import { RouterConfig } from './types';
import { DefaultNotFound } from './DefaultNotFound';
import DeferredDataScripts from './DeferredDataScripts';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
useLocation,
useNavigate,
useParams,
} from '@modern-js/utils/runtime/router';
} from '@modern-js/runtime-utils/router';

export interface WithRouterProps {
location: ReturnType<typeof useLocation>;
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/plugin-runtime/src/runtimeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type StaticHandlerContext,
type Router,
type RouterState,
} from '@modern-js/utils/runtime/remix-router';
} from '@modern-js/runtime-utils/remix-router';
import { createContext } from 'react';
import { createLoaderManager } from './core/loader/loaderManager';
import { runtime } from './core/plugin';
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/plugin-runtime/src/ssr/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ export const ssrPlugin = (): CliPlugin<AppTools> => ({
return {
source: {
alias: {
// ensure that all packages use the same storage in @modern-js/utils/runtime-node
'@modern-js/utils/runtime-node$': require.resolve(
'@modern-js/utils/runtime-node',
// ensure that all packages use the same storage in @modern-js/runtime-utils/node
'@modern-js/runtime-utils/node$': require.resolve(
'@modern-js/runtime-utils/node',
),
'@modern-js/runtime/plugins': pluginsExportsUtils.getPath(),
},
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/plugin-runtime/src/ssr/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loadableReady } from '@loadable/component';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { parsedJSONFromElement } from '@modern-js/utils/runtime-browser';
import { parsedJSONFromElement } from '@modern-js/runtime-utils/browser';
import type { Plugin } from '../core';
import {
RenderLevel,
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/plugin-runtime/src/ssr/prefetch.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderToStaticMarkup } from 'react-dom/server';
import { run } from '@modern-js/utils/runtime-node';
import { run } from '@modern-js/runtime-utils/node';
import { ChunkExtractor } from '@loadable/server';
import { RuntimeContext } from '../core';
import { SSRPluginConfig } from './serverRender/types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { serializeJson } from '@modern-js/utils/runtime-node';
import { serializeJson } from '@modern-js/runtime-utils/node';
import { RenderLevel, RuntimeContext } from '../types';
import { attributesToString } from '../utils';
import { BuildTemplateCb, buildTemplate } from './buildTemplate.share';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ReactHelmet, { HelmetData } from 'react-helmet';
// Todo: This import will introduce router code, like remix, even if router config is false
import { matchRoutes } from '@modern-js/utils/runtime/router';
import { matchRoutes } from '@modern-js/runtime-utils/router';
import helmetReplace from '../helmet';
import { RuntimeContext } from '../types';
import { CSS_CHUNKS_PLACEHOLDER } from '../utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createElement } from 'react';
import { run } from '@modern-js/utils/runtime-node';
import { time } from '@modern-js/utils/universal/time';
import { run } from '@modern-js/runtime-utils/node';
import { time } from '@modern-js/runtime-utils/time';
import { PreRender } from '../../react/prerender';
import { ServerRenderOptions } from '../types';
import { SSRErrors, SSRTimings } from '../tracker';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { serializeJson } from '@modern-js/utils/runtime-node';
import { serializeJson } from '@modern-js/runtime-utils/node';
import ReactHelmet, { HelmetData } from 'react-helmet';
// Todo: This import will introduce router code, like remix, even if router config is false
import { time } from '@modern-js/utils/universal/time';
import { time } from '@modern-js/runtime-utils/time';
import { serializeErrors } from '../../../router/runtime/utils';
import helmetReplace from '../helmet';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { run } from '@modern-js/utils/runtime-node';
import { time } from '@modern-js/utils/universal/time';
import { run } from '@modern-js/runtime-utils/node';
import { time } from '@modern-js/runtime-utils/time';
import { ServerRenderOptions } from '../types';
import { PreRender } from '../../react/prerender';
import { SSRTimings } from '../tracker';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
createMemoryRouter,
LoaderFunctionArgs,
RouterProvider,
} from '@modern-js/utils/runtime/router';
} from '@modern-js/runtime-utils/router';
import { render, fireEvent, act, waitFor } from '@testing-library/react';
import { Link } from '../../src/router';
import { RuntimeReactContext } from '../../src';
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/plugin-runtime/tests/ssr/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { time } from '@modern-js/utils/universal/time';
import { time } from '@modern-js/runtime-utils/time';
import plugin from '../../src/ssr';
import cliPlugin from '../../src/ssr/cli';
import { formatClient, formatServer } from '../../src/ssr/utils';
Expand Down
1 change: 1 addition & 0 deletions packages/server/create-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
},
"dependencies": {
"@modern-js/utils": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"node-fetch": "^2.6.1",
"path-to-regexp": "^6.2.0",
"query-string": "^7.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/create-request/src/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nodeFetch from 'node-fetch';
import { compile, pathToRegexp, Key } from 'path-to-regexp';
import { useHeaders } from '@modern-js/utils/runtime-node';
import { useHeaders } from '@modern-js/runtime-utils/node';
import { stringify } from 'query-string';
import { handleRes } from './handleRes';
import type {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/create-request/tests/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @jest-environment node
*/
import nock from 'nock';
import { run } from '@modern-js/utils/runtime-node';
import { run } from '@modern-js/runtime-utils/node';
// 如果通过 default 引入会报 "Property exprName of TSTypeQuery expected node to be of a type ["TSEntityName","TSImportType"] but instead got "MemberExpression"
import * as fetch from 'node-fetch';
import { Response } from 'node-fetch';
Expand Down
1 change: 1 addition & 0 deletions packages/server/prod-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@modern-js/plugin": "workspace:*",
"@modern-js/server-core": "workspace:*",
"@modern-js/utils": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"@swc/helpers": "0.5.1",
"cookie": "^0.4.2",
"etag": "^1.8.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/server/prod-server/src/libs/preload/parseLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
fs,
} from '@modern-js/utils';
import { parse as htmlParse } from 'node-html-parser';
import { matchRoutes } from '@modern-js/utils/runtime/remix-router';
import { matchEntry } from '@modern-js/utils/runtime-node';
import { matchRoutes } from '@modern-js/runtime-utils/remix-router';
import { matchEntry } from '@modern-js/runtime-utils/node';

export interface Link {
uri: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/server/prod-server/src/server/modernServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
LoaderHandler,
} from '@modern-js/server-core';
import { type ModernServerContext, type ServerRoute } from '@modern-js/types';
import { time } from '@modern-js/utils/universal/time';
import { time } from '@modern-js/runtime-utils/time';
import type { ContextOptions } from '../libs/context';
import {
ModernServerOptions,
Expand Down
1 change: 1 addition & 0 deletions packages/server/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@modern-js/server-utils": "workspace:*",
"@modern-js/types": "workspace:*",
"@modern-js/utils": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"@swc/helpers": "0.5.1",
"connect-history-api-fallback": "^2.0.0",
"http-compression": "1.0.6",
Expand Down
Loading

0 comments on commit 6a1d46e

Please sign in to comment.