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

WIP: chore: add react ssr stream example #947

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion crates/plugin_runtime/src/handle_entry_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ pub fn handle_entry_resources(
// TODO support sourcemap
entry_js_resource.bytes = vec![
if !dep_resources.is_empty() {
format!("import \"./{}\";", runtime_resource.name)
match context.config.output.format {
ModuleFormat::EsModule => format!("import \"./{}\";", runtime_resource.name),
ModuleFormat::CommonJs => format!("require(\"./{}\");", runtime_resource.name),
}
} else {
runtime_code.clone()
},
Expand Down
8 changes: 8 additions & 0 deletions examples/react-ssr-stream/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"root": true,
"extends": "../../.eslintrc.base.json",
"parserOptions": {
"project": ["./examples/react-ssr-stream/tsconfig.json"]
},
"rules": {}
}
3 changes: 3 additions & 0 deletions examples/react-ssr-stream/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
build
node_modules
23 changes: 23 additions & 0 deletions examples/react-ssr-stream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Farm React SSR Example
React + React Router + SSR + PipeableStream.

## Start
```sh
npm start; # start the client dev server
npm run watch; # compile and watch the server procution in development mode
```

Then visit `http://localhost:9000`.

## Build For Production
Build for both client and server.
```sh
npm run build && npm run build:server
```

then launch the production server:
```sh
NODE_ENV=production node server.js
```

Visit `http://localhost:3000`
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions examples/react-ssr-stream/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="root"><div>app-html-to-replace</div></div>
<script src="./src/index-client.tsx"></script>
</body>
</html>
26 changes: 26 additions & 0 deletions examples/react-ssr-stream/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@farmfe-examples/react-ssr-stream",
"version": "0.0.1",
"private": true,
"dependencies": {
"react": "18",
"react-dom": "18",
"react-router-dom": "^6.13.0"
},
"devDependencies": {
"@farmfe/cli": "workspace:*",
"@farmfe/core": "workspace:*",
"@farmfe/plugin-react": "workspace:*",
"@farmfe/plugin-sass": "workspace:*",
"@types/react": "18",
"@types/react-dom": "18",
"react-refresh": "^0.14.0"
},
"scripts": {
"start": "farm start",
"watch": "farm watch --config farm.config.server.mjs",
"build": "farm build",
"build:server": "farm build --config farm.config.server.mjs",
"preview": "farm preview"
}
}
Empty file.
Empty file.
11 changes: 11 additions & 0 deletions examples/react-ssr-stream/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"noEmit": true,
"jsx": "react"
},
"include": ["src", "configs", "server"]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { createRequire } from 'node:module';
import path from 'node:path';
import { existsSync, readFileSync } from 'node:fs';

import { Config } from '../../../binding/index.js';
import { ResolvedUserConfig } from '../index.js';
import { RustPlugin } from '../../plugin/index.js';
import { traceDependencies } from '../../utils/trace-dependencies.js';
import path from 'node:path';

export async function normalizePersistentCache(
config: Config['config'],
Expand All @@ -29,6 +30,19 @@ export async function normalizePersistentCache(
config.persistentCache.envs = resolvedUserConfig.env;
}

// add type of package.json to envs
const packageJsonPath = path.join(
config.root ?? process.cwd(),
'package.json'
);

if (existsSync(packageJsonPath)) {
const s = readFileSync(packageJsonPath).toString();
const packageJson = JSON.parse(s);
config.persistentCache.envs['package.json[type]'] =
packageJson.type ?? 'unknown';
}

if (!config.persistentCache.buildDependencies) {
config.persistentCache.buildDependencies = [];
}
Expand Down
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading