Skip to content

Commit

Permalink
fix: update environment name for allowed characters (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Oct 3, 2024
1 parent cb72b18 commit a01d82b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 56 deletions.
2 changes: 1 addition & 1 deletion examples/react-server/src/features/style/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function vitePluginServerCss({
createVirtualPlugin(VIRTUAL_COPY_SERVER_CSS.slice(8), async () => {
if ($__global.server) {
const urls = await collectStyleUrls(
$__global.server.environments["react-server"],
$__global.server.environments["rsc"],
// TODO: lazy import is not crawled until it's imported
["/src/entry-server"],
);
Expand Down
29 changes: 13 additions & 16 deletions examples/react-server/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ export default defineConfig((_env) => ({
// by traversing server module graph and going over client boundary
// TODO: this causes single plugin to be reused by two react-server builds
manager.buildStep = "scan";
await builder.build(builder.environments["react-server"]!);
await builder.build(builder.environments["rsc"]!);
manager.buildStep = undefined;

await builder.build(builder.environments["react-server"]!);
await builder.build(builder.environments["rsc"]!);
await builder.build(builder.environments["client"]!);
await builder.build(builder.environments["ssr"]!);
},
Expand Down Expand Up @@ -127,7 +127,7 @@ function vitePluginReactServer(): PluginOption {
name: vitePluginReactServer.name,
config(config, _env) {
tinyassert(config.environments);
config.environments["react-server"] = {
config.environments["rsc"] = {
resolve: {
conditions: ["react-server"],
noExternal: true,
Expand Down Expand Up @@ -160,15 +160,15 @@ function vitePluginReactServer(): PluginOption {
manager.config = config;
},
async configureServer(server) {
const reactServerEnv = server.environments["react-server"];
const reactServerEnv = server.environments["rsc"];
tinyassert(reactServerEnv);
// no hmr setup for custom node environment
const reactServerRunner = createServerModuleRunner(reactServerEnv);
$__global.server = server;
$__global.reactServerRunner = reactServerRunner;
},
hotUpdate(ctx) {
if (this.environment.name === "react-server") {
if (this.environment.name === "rsc") {
const ids = ctx.modules.map((mod) => mod.id).filter(typedBoolean);
if (ids.length > 0) {
// client reference id is also in react server module graph,
Expand Down Expand Up @@ -215,7 +215,7 @@ function vitePluginUseClient(): PluginOption {
const transformPlugin: Plugin = {
name: vitePluginUseClient.name + ":transform",
async transform(code, id, _options) {
if (this.environment.name !== "react-server") {
if (this.environment.name !== "rsc") {
return;
}
manager.clientReferenceMap.delete(id);
Expand Down Expand Up @@ -254,7 +254,7 @@ function vitePluginUseClient(): PluginOption {
const virtualPlugin: Plugin = createVirtualPlugin(
"client-references",
function () {
tinyassert(this.environment?.name !== "react-server");
tinyassert(this.environment?.name !== "rsc");
tinyassert(this.environment?.mode === "build");

return [
Expand Down Expand Up @@ -297,8 +297,8 @@ function vitePluginServerAction(): PluginOption {
}
const ast = await parseAstAsync(code);
tinyassert(this.environment);
const runtimeId = await normalizeReferenceId(id, "react-server");
if (this.environment.name === "react-server") {
const runtimeId = await normalizeReferenceId(id, "rsc");
if (this.environment.name === "rsc") {
const { output } = await transformServerActionServer(code, ast, {
id: runtimeId,
runtime: "$$register",
Expand Down Expand Up @@ -345,7 +345,7 @@ function vitePluginServerAction(): PluginOption {
if (manager.buildStep === "scan") {
return `export default {}`;
}
tinyassert(this.environment?.name === "react-server");
tinyassert(this.environment?.name === "rsc");
tinyassert(this.environment.mode === "build");
return [
"export default {",
Expand All @@ -361,7 +361,7 @@ function vitePluginServerAction(): PluginOption {
name: "patch-react-server-dom-webpack",
transform(code, id, _options) {
if (
this.environment?.name === "react-server" &&
this.environment?.name === "rsc" &&
id.includes("react-server-dom-webpack")
) {
// rename webpack markers in react server runtime
Expand Down Expand Up @@ -389,10 +389,7 @@ function vitePluginServerAction(): PluginOption {
return [transformPlugin, virtualServerReference, patchPlugin];
}

async function normalizeReferenceId(
id: string,
name: "client" | "react-server",
) {
async function normalizeReferenceId(id: string, name: "client" | "rsc") {
if (manager.config.command === "build") {
return hashString(path.relative(manager.config.root, id));
}
Expand All @@ -411,7 +408,7 @@ async function normalizeReferenceId(
runtimeId = m?.[1];
break;
}
case "react-server": {
case "rsc": {
// `dynamicDeps` is available for ssrTransform
runtimeId = transformed.dynamicDeps?.[0];
break;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"tsup": "^8.1.2",
"tsx": "^4.16.2",
"typescript": "^5.5.3",
"vite": "6.0.0-beta.2",
"vite": "https://pkg.pr.new/vite@87949fe",
"vitest": "^2.0.3",
"wrangler": "^3.79.0"
},
Expand Down
Loading

0 comments on commit a01d82b

Please sign in to comment.