diff --git a/crates/plugin_runtime/src/handle_entry_resources.rs b/crates/plugin_runtime/src/handle_entry_resources.rs index 5ff038cc8..1cb05d234 100644 --- a/crates/plugin_runtime/src/handle_entry_resources.rs +++ b/crates/plugin_runtime/src/handle_entry_resources.rs @@ -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() }, diff --git a/examples/react-ssr-stream/.eslintrc.json b/examples/react-ssr-stream/.eslintrc.json new file mode 100644 index 000000000..a4a47b6ce --- /dev/null +++ b/examples/react-ssr-stream/.eslintrc.json @@ -0,0 +1,8 @@ +{ + "root": true, + "extends": "../../.eslintrc.base.json", + "parserOptions": { + "project": ["./examples/react-ssr-stream/tsconfig.json"] + }, + "rules": {} +} diff --git a/examples/react-ssr-stream/.gitignore b/examples/react-ssr-stream/.gitignore new file mode 100644 index 000000000..6551bfded --- /dev/null +++ b/examples/react-ssr-stream/.gitignore @@ -0,0 +1,3 @@ +dist +build +node_modules \ No newline at end of file diff --git a/examples/react-ssr-stream/README.md b/examples/react-ssr-stream/README.md new file mode 100644 index 000000000..08766f742 --- /dev/null +++ b/examples/react-ssr-stream/README.md @@ -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` diff --git a/examples/react-ssr-stream/configs/farm.config.server.ts b/examples/react-ssr-stream/configs/farm.config.server.ts new file mode 100644 index 000000000..e69de29bb diff --git a/examples/react-ssr-stream/configs/farm.config.ts b/examples/react-ssr-stream/configs/farm.config.ts new file mode 100644 index 000000000..e69de29bb diff --git a/examples/react-ssr-stream/index.html b/examples/react-ssr-stream/index.html new file mode 100644 index 000000000..326ccad2a --- /dev/null +++ b/examples/react-ssr-stream/index.html @@ -0,0 +1,13 @@ + + + + + + + Document + + +
app-html-to-replace
+ + + \ No newline at end of file diff --git a/examples/react-ssr-stream/package.json b/examples/react-ssr-stream/package.json new file mode 100644 index 000000000..42935033d --- /dev/null +++ b/examples/react-ssr-stream/package.json @@ -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" + } +} \ No newline at end of file diff --git a/examples/react-ssr-stream/src/entry.client.tsx b/examples/react-ssr-stream/src/entry.client.tsx new file mode 100644 index 000000000..e69de29bb diff --git a/examples/react-ssr-stream/src/root.tsx b/examples/react-ssr-stream/src/root.tsx new file mode 100644 index 000000000..e69de29bb diff --git a/examples/react-ssr-stream/tsconfig.json b/examples/react-ssr-stream/tsconfig.json new file mode 100644 index 000000000..e0110e29c --- /dev/null +++ b/examples/react-ssr-stream/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "node", + "noEmit": true, + "jsx": "react" + }, + "include": ["src", "configs", "server"] +} diff --git a/packages/core/src/config/normalize-config/normalize-persistent-cache.ts b/packages/core/src/config/normalize-config/normalize-persistent-cache.ts index a656c55e3..6aecedc26 100644 --- a/packages/core/src/config/normalize-config/normalize-persistent-cache.ts +++ b/packages/core/src/config/normalize-config/normalize-persistent-cache.ts @@ -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'], @@ -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 = []; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 72769fb1e..d1d8442c1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -726,6 +726,40 @@ importers: specifier: ^0.14.0 version: 0.14.0 + examples/react-ssr-stream: + dependencies: + react: + specifier: '18' + version: 18.2.0 + react-dom: + specifier: '18' + version: 18.2.0(react@18.2.0) + react-router-dom: + specifier: ^6.13.0 + version: 6.18.0(react-dom@18.2.0)(react@18.2.0) + devDependencies: + '@farmfe/cli': + specifier: workspace:* + version: link:../../packages/cli + '@farmfe/core': + specifier: workspace:* + version: link:../../packages/core + '@farmfe/plugin-react': + specifier: workspace:* + version: link:../../rust-plugins/react + '@farmfe/plugin-sass': + specifier: workspace:* + version: link:../../rust-plugins/sass + '@types/react': + specifier: '18' + version: 18.2.35 + '@types/react-dom': + specifier: '18' + version: 18.2.14 + react-refresh: + specifier: ^0.14.0 + version: 0.14.0 + examples/resolve-module-graph: devDependencies: '@farmfe/cli':