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

feat: framework agnostic component preview solution #563

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 0 additions & 2 deletions e2e/fixtures/plugin/doc/components/component.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';

export default function HelloWorld() {
return <div>Hello World</div>;
}
18 changes: 0 additions & 18 deletions e2e/fixtures/plugin/doc/components/guide.mdx
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
# Guide

```jsx
import React from 'react';

export default function HelloWorld() {
return <div>Hello</div>;
}
```

```jsx
import React from 'react';

export default function HelloWorld() {
return <div>World</div>;
}
```

<code src="./component.jsx" />
9 changes: 0 additions & 9 deletions e2e/fixtures/plugin/doc/index.md

This file was deleted.

14 changes: 14 additions & 0 deletions e2e/fixtures/plugin/doc/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Hello World

```jsx
export default function HelloWorld() {
return <div>这是 solidjs 渲染的结果11</div>;
}
```

```jsx
// 如果要引入自定义的外部组件,可以使用 tsconfig.json 中的 paths 别名配置
export default function HelloWorld() {
return <div>这是 solidjs 渲染的结果</div>;
}
```
8 changes: 4 additions & 4 deletions e2e/fixtures/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"preview": "rspress preview"
},
"dependencies": {
"@rspress/plugin-playground": "workspace:*",
"@rspress/plugin-preview": "workspace:*",
"@rspress/shared": "workspace:*",
"react": "^18",
"react-dom": "^18",
"react-transition-group": "4.4.5",
"rspress": "workspace:*",
"@rspress/shared": "workspace:*",
"@rspress/plugin-preview": "workspace:*",
"@rspress/plugin-playground": "workspace:*"
"rspress": "workspace:*"
},
"devDependencies": {
"@types/node": "^14"
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures/plugin/rspress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export default defineConfig({
isMobile: true,
iframePosition: 'fixed',
}),
// pluginPlayground(),
],

});
2 changes: 1 addition & 1 deletion e2e/fixtures/plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"esModuleInterop": true,
"jsx": "react-jsx"
"jsx": "preserve"
}
}
11 changes: 10 additions & 1 deletion packages/core/src/node/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ export async function dev(options: DevOptions): Promise<ServerInstance> {
false,
extraBuilderConfig,
);
builder.addPlugins([
{
name: 'rspress:afterBuild',
setup(api) {
api.onDevCompileDone(async () => {
await pluginDriver.afterBuild();
});
},
},
]);
const { server } = await builder.startDevServer({
// We will support the following options in the future
getPortSilently: true,
});

await pluginDriver.afterBuild();
return server;
} finally {
await writeSearchIndex(config);
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-preview/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export default defineConfig({
format: 'cjs',
sourceMap: true,
input: ['./src/index.ts'],
externals: [
'@rsbuild/core',
'@rsbuild/plugin-babel',
'@rsbuild/plugin-solid',
],
target: 'es2020',
dts: {
respectExternal: false,
Expand Down
14 changes: 9 additions & 5 deletions packages/plugin-preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,23 @@
"dependencies": {
"@mdx-js/mdx": "2.2.1",
"@rspress/shared": "workspace:*",
"lodash": "4.17.21",
"qrcode.react": "^3.1.0",
"remark-gfm": "3.0.1",
"rspack-plugin-virtual-module": "0.1.12",
"lodash": "4.17.21"
"solid-js": "1.8.11"
},
"devDependencies": {
"@biomejs/biome": "1.5.2",
"@rsbuild/core": "0.3.1",
"@rsbuild/plugin-solid": "0.3.1",
"@rsbuild/plugin-babel": "0.3.1",
"@types/lodash": "4.14.200",
"@types/mdast": "^3.0.10",
"@types/node": "^18.11.17",
"@types/lodash": "4.14.200",
"@types/react": "^18",
"@types/react-dom": "^18",
"mdast-util-mdxjs-esm": "^1.3.0",
"@biomejs/biome": "1.5.2",
"react": "^18",
"react-dom": "^18",
"react-router-dom": "^6.8.1",
Expand All @@ -58,9 +62,9 @@
"unist-util-visit": "^4.1.1"
},
"peerDependencies": {
"@rspress/core": "^1.0.2",
"react": ">=17",
"react-router-dom": "^6.8.1",
"@rspress/core": "^1.0.2"
"react-router-dom": "^6.8.1"
},
"files": [
"dist",
Expand Down
41 changes: 28 additions & 13 deletions packages/plugin-preview/src/codeToDemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import {
import type { Plugin } from 'unified';
import type { Root } from 'mdast';
import type { MdxjsEsm } from 'mdast-util-mdxjs-esm';

import { injectDemoBlockImport, generateId } from './utils';
import { demoBlockComponentPath } from './constant';
import { demoRoutes } from '.';
// import { demoRoutes } from '.';

export const demoEntryMap: Record<string, string> = {};
export const DEMO_SERVER_PORT = '7890';

// FIXME: remove it
const json = JSON.parse(
fs.readFileSync(resolve(process.cwd(), './package.json'), 'utf8'),
);

/**
* remark plugin to transform code to demo
*/
Expand Down Expand Up @@ -56,15 +59,15 @@ export const remarkCodeToDemo: Plugin<
// Only for external demo
externalDemoIndex?: number,
) {
const demoRoute = `/~demo/${demoId}`;
if (isMobileMode) {
// only add demoRoutes in mobile mode
demoRoutes.push({
path: demoRoute,
});
} else {
demos.push(getASTNodeImport(`Demo${demoId}`, demoPath));
}
// const demoRoute = `/~demo/${demoId}`;
// if (isMobileMode) {
// // only add demoRoutes in mobile mode
// demoRoutes.push({
// path: demoRoute,
// });
// } else {
// demos.push(getASTNodeImport(`Demo${demoId}`, demoPath));
// }

// get external demo content
const tempVar = `externalDemoContent${externalDemoIndex}`;
Expand Down Expand Up @@ -92,7 +95,7 @@ export const remarkCodeToDemo: Plugin<
{
type: 'mdxJsxAttribute',
name: 'url',
value: demoRoute,
value: demoPath,
},
{
type: 'mdxJsxAttribute',
Expand Down Expand Up @@ -187,6 +190,14 @@ export const remarkCodeToDemo: Plugin<
);
const id = generateId(pageName, index++);
const virtualModulePath = join(demoDir, `${id}.tsx`);
const virtualEntryPath = join(demoDir, `${id}.entry.tsx`);

const entryContent = `
import { render } from 'solid-js/web';
import Demo from '${virtualModulePath}';

render(() => <Demo /> , document.getElementById('root'));
`;
fs.ensureDirSync(join(demoDir));
// Only when the content of the file changes, the file will be written
// Avoid to trigger the hmr indefinitely
Expand All @@ -195,8 +206,12 @@ export const remarkCodeToDemo: Plugin<
if (content !== value) {
fs.writeFileSync(virtualModulePath, value);
}
fs.writeFileSync(virtualEntryPath, entryContent);
}
constructDemoNode(id, virtualModulePath, node, isMobileMode);
demoEntryMap[id] = virtualEntryPath;

const demoPath = `http://localhost:${DEMO_SERVER_PORT}/demos/${id}.html`;
constructDemoNode(id, demoPath, node, isMobileMode);
}
});

Expand Down
49 changes: 48 additions & 1 deletion packages/plugin-preview/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
normalizePosixPath,
} from '@rspress/shared';
import { uniqBy } from 'lodash';
import { remarkCodeToDemo } from './codeToDemo';
import { createRsbuild } from '@rsbuild/core';
import { pluginSolid } from '@rsbuild/plugin-solid';
import { pluginBabel } from '@rsbuild/plugin-babel';
import { DEMO_SERVER_PORT, demoEntryMap, remarkCodeToDemo } from './codeToDemo';
import { generateId, injectDemoBlockImport } from './utils';
import {
demoBlockComponentPath,
Expand Down Expand Up @@ -308,6 +311,50 @@ import Demo from ${JSON.stringify(demoComponentPath)}
}
return demoRoutes;
},
async afterBuild() {
// Create demo dev server
const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
dev: {
client: {
port: DEMO_SERVER_PORT,
},
progressBar: false,
},
server: {
// Don't display the demo server log
printUrls: () => undefined,
},
source: {
entry: demoEntryMap,
alias: {
'solid-js/web': `${require.resolve('solid-js/web/dist/web.js')}`,
},
},
output: {
distPath: {
root: 'demo_build',
},
},
tools: {
rspack: {
output: {
publicPath: '/demos',
},
},
},
},
});
rsbuildInstance.addPlugins([
pluginBabel({
include: /\.(?:jsx|tsx)$/,
}),
pluginSolid(),
]);
await rsbuildInstance.startDevServer({
getPortSilently: true,
});
},
};
}

Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-preview/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ export const normalizeId = (routePath: string) => {
return toValidVarName(result);
};

export const injectDemoBlockImport = (str: string, path: string): string => {
export const injectDemoBlockImport = (str: string, _path: string): string => {
return `
import DemoBlock from '${path}';
${str}
`;
};
2 changes: 1 addition & 1 deletion packages/plugin-preview/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"declaration": false,
"module": "ESNext",
"target": "ESNext",
"jsx": "react-jsx",
"jsx": "preserve",
"baseUrl": "src",
"rootDir": ".",
"lib": ["ESNext", "DOM"],
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"rootDir": "src",
"baseUrl": ".",
"skipLibCheck": true,
"jsx": "react-jsx",
"jsx": "preserve",
"esModuleInterop": true
},
"include": ["./src/**/*"]
Expand Down
Loading