Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(devtools): dataflow implementation of client #5222

Merged
merged 21 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
220b1af
refactor: rpc channels
Asuka109 Jan 11, 2024
68960d2
fix: update pnpm file to avoid package name undefined error
Asuka109 Jan 12, 2024
2a4e924
test: add test for message port channel
Asuka109 Jan 12, 2024
8209b02
test: add test for message port channel
Asuka109 Jan 12, 2024
b27c988
fix: empty body of react devtools wall via message port channel
Asuka109 Jan 12, 2024
aa847f6
feat: implement new method `bindRemote` for `WallAgent`
Asuka109 Jan 12, 2024
ae73105
test: close message channel port after test completed
Asuka109 Jan 12, 2024
01adf72
fix: service worker compiler plugin use unique name
Asuka109 Jan 12, 2024
606f1b9
fix: client ws connection can not respond to heartbeat
Asuka109 Jan 12, 2024
5e6c84d
fix: failed to fetch file system routes
Asuka109 Jan 12, 2024
03118b8
Merge branch 'main' into refactor/devtools-state
Asuka109 Jan 14, 2024
6873597
chore: add changeset
Asuka109 Jan 15, 2024
418d19a
chore: update pnpm lock file
Asuka109 Jan 15, 2024
2f79535
fix(CI): mount message channel to global this
Asuka109 Jan 15, 2024
c9b79f2
Merge branch 'main' into refactor/devtools-state
Asuka109 Jan 15, 2024
d08179e
chore: update pnpm lock file
Asuka109 Jan 15, 2024
156d68a
Merge branch 'main' into refactor/devtools-state
Asuka109 Jan 15, 2024
aaac8e6
feat: restrict typings
Asuka109 Jan 15, 2024
6086acf
chore: remove the comments in Chinese
Asuka109 Jan 15, 2024
f2b3fd4
test: simplify jest config and fix hanging issue
Asuka109 Jan 15, 2024
6af0955
chore: remove babel.config.js (for jest transformer)
Asuka109 Jan 15, 2024
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
8 changes: 8 additions & 0 deletions .changeset/few-swans-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@modern-js/devtools-client': patch
'@modern-js/plugin-devtools': patch
'@modern-js/devtools-kit': patch
---

refactor: dataflow of devtools client
refactor: 重构 devtools 客户端数据流
2 changes: 1 addition & 1 deletion .pnpmfile.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function readPackage(pkg, _context) {
const outsideModernPkgList = ['@modern-js/mdx-rs-binding'];

if (
(pkg.name.startsWith('@rspress/') || pkg.name.startsWith('rspress')) &&
(pkg.name?.startsWith('@rspress/') || pkg.name?.startsWith('rspress')) &&
pkg.dependencies
) {
pkg.dependencies = Object.fromEntries(
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools/client/.browserslistrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
chrome >= 51
chrome >= 55
edge >= 15
firefox >= 54
safari >= 10
ios_saf >= 10
safari >= 11
ios_saf >= 11
2 changes: 1 addition & 1 deletion packages/devtools/client/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
module.exports = {
root: true,
extends: ['@modern-js'],
ignorePatterns: ['plugins/'],
ignorePatterns: ['plugins/', 'tests/'],
};
6 changes: 6 additions & 0 deletions packages/devtools/client/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
};
198 changes: 198 additions & 0 deletions packages/devtools/client/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
*/

/** @type {import('jest').Config} */
module.exports = {
// All imported modules in your tests should be mocked automatically
// automock: false,

// Stop running tests after `n` failures
// bail: 0,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/private/var/folders/b1/0fd1b6hs7lz0fm_mh346lybm0000gn/T/jest_dx",

// Automatically clear mock calls, instances, contexts and results before every test
clearMocks: true,

// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,

// The directory where Jest should output its coverage files
// coverageDirectory: undefined,

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],

// Indicates which provider should be used to instrument code for coverage
// coverageProvider: "babel",

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,

// A path to a custom dependency extractor
// dependencyExtractor: undefined,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// The default configuration for fake timers
// fakeTimers: {
// "enableGlobally": false
// },

// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,

// A set of global variables that need to be available in all test environments
// globals: {},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",

// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],

// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "mjs",
// "cjs",
// "jsx",
// "ts",
// "tsx",
// "json",
// "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],

// Activates notifications for test results
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: undefined,

// Run tests from one or more projects
// projects: undefined,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,

// Automatically reset mock state before every test
// resetMocks: false,

// Reset the module registry before running each individual test
// resetModules: false,

// A path to a custom resolver
// resolver: undefined,

// Automatically restore mock state and implementation before every test
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,

// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",

// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

// The test environment that will be used for testing
// testEnvironment: "jest-environment-node",

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},

// Adds a location field to test results
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: undefined,

// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",

// A map from regular expressions to paths to transformers
transform: {
'\\.[jt]sx?$': 'babel-jest',
},

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/",
// "\\.pnp\\.[^\\/]+$"
// ],

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: undefined,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],

// Whether to use watchman for file crawling
// watchman: true,
};
1 change: 1 addition & 0 deletions packages/devtools/client/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default defineConfig<'rspack'>({
addPlugins(require('postcss-custom-media'));
},
bundlerChain(chain) {
chain.output.uniqueName('modernjsDevtools');
chain.module
.rule('RADIX_TOKEN')
.test(require.resolve('@radix-ui/themes/styles.css'))
Expand Down
10 changes: 7 additions & 3 deletions packages/devtools/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"serve": "modern serve",
"new": "modern new",
"lint": "modern lint",
"upgrade": "modern upgrade"
"upgrade": "modern upgrade",
"test": "jest"
},
"engines": {
"node": ">=14.0.0"
Expand Down Expand Up @@ -50,16 +51,17 @@
"@types/react-dom": "^18",
"birpc": "0.2.13",
"clsx": "^1.2.1",
"hookable": "^5.5.3",
"lodash": "^4.17.21",
"nanoid": "3.3.4",
"p-defer": "^3.0.0",
"postcss-custom-media": "^10.0.1",
"react": "^18.2.0",
"react-exp": "npm:react@0.0.0-experimental-51ffd3564-20231025",
"react-base16-styling": "^0.9.1",
"react-devtools-inline": "^4.28.5",
"react-dom": "^18.2.0",
"react-dom-exp": "npm:react-dom@0.0.0-experimental-51ffd3564-20231025",
"react-exp": "npm:react@0.0.0-experimental-51ffd3564-20231025",
"react-icons": "^4.11.0",
"react-is": "^18",
"react-json-tree": "^0.18.0",
Expand All @@ -71,6 +73,8 @@
"type-fest": "^4.1.0",
"typescript": "~5.0.4",
"ufo": "^1.3.0",
"valtio": "^1.11.1"
"valtio": "^1.11.1",
"babel-jest": "^29.5.0",
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
"jest": "^29"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export class ServiceWorkerCompilerPlugin {
js: false,
css: false,
},
cleanDistPath: false,
distPath: {
root: './dist',
worker: './public',
},
},
tools: {
bundlerChain(chain) {
chain.output.uniqueName('modernjsDevtoolsSW');
chain.output.delete('libraryTarget');
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/devtools/client/src/components/Devtools/FrameBox.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useState } from 'react';
import { Box } from '@radix-ui/themes';
import type { BoxProps } from '@radix-ui/themes/dist/cjs/components/box';
import { HiMiniXMark } from 'react-icons/hi2';
import { LoaderIcon } from '../LoadingIcon';
import styles from './FrameBox.module.scss';
import { ReactComponent as XMarkIcon } from './xmark.svg';

export interface FrameBoxProps
extends BoxProps,
Expand All @@ -26,7 +26,7 @@ export const FrameBox: React.FC<FrameBoxProps> = ({
onLoad={() => setShowFrame(true)}
src={src}
></iframe>
<XMarkIcon className={styles.closeButton} onClick={() => onClose?.()} />
<HiMiniXMark className={styles.closeButton} onClick={onClose} />
<div
className={styles.backdrop}
style={{ display: showFrame ? 'none' : undefined }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { FileSystemRoutes } from '@modern-js/devtools-kit';
import type { RouteLegacy, ServerRoute } from '@modern-js/types';
import _ from 'lodash';
import React from 'react';
import { useSnapshot } from 'valtio';
import React, { Suspense } from 'react';
import { proxy, useSnapshot } from 'valtio';
import { Promisable } from 'type-fest';
import { LegacyRouteStats } from './LegacyRoute/Stats';
import { RemixRouteStats } from './RemixRoute/Stats';
import { useStore } from '@/entries/client/stores';
import { $framework, $server } from '@/entries/client/routes/state';
import { useThrowable } from '@/utils';

export const $fileSystemRoutes = proxy<
Record<string, Promisable<FileSystemRoutes>>
>({});

export interface ClientRouteStatsProps {
route: ServerRoute;
Expand All @@ -14,9 +20,9 @@ export interface ClientRouteStatsProps {
export const ClientRouteStats: React.FC<ClientRouteStatsProps> = ({
route,
}) => {
const $store = useStore();
const store = useSnapshot($store);
const { entrypoints } = store.framework.context;
const server = useThrowable($server);
const framework = useSnapshot($framework);
const { entrypoints } = framework.context;
const entrypoint =
route.entryName && _.find(entrypoints, { entryName: route.entryName });

Expand All @@ -26,16 +32,25 @@ export const ClientRouteStats: React.FC<ClientRouteStatsProps> = ({
);
}

const fileSystemRoute =
store.framework.fileSystemRoutes[entrypoint.entryName];
const $fileSystemRoute = server.remote.getFileSystemRoutes(
entrypoint.entryName,
);

if (isLegacyRoutes(fileSystemRoute as any)) {
return <LegacyRouteStats />;
} else {
return (
<RemixRouteStats remixRoutes={fileSystemRoute as any} route={route} />
);
}
const Dispatcher: React.FC = () => {
const fileSystemRoute = useThrowable($fileSystemRoute);
if (isLegacyRoutes(fileSystemRoute as any)) {
return <LegacyRouteStats />;
} else {
return (
<RemixRouteStats remixRoutes={fileSystemRoute as any} route={route} />
);
}
};
return (
<Suspense>
<Dispatcher />
</Suspense>
);
};

const isLegacyRoutes = (routes: FileSystemRoutes): routes is RouteLegacy[] =>
Expand Down
Loading