vite-plugin-react-server 1.1.18 → 1.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/dist/package.json +1 -1
- package/dist/plugin/config/defaults.d.ts +1 -1
- package/dist/plugin/config/defaults.d.ts.map +1 -1
- package/dist/plugin/config/defaults.js +2 -2
- package/dist/plugin/config/defaults.js.map +1 -1
- package/dist/plugin/config/resolveOptions.d.ts.map +1 -1
- package/dist/plugin/config/resolveOptions.js +4 -4
- package/dist/plugin/config/resolveOptions.js.map +1 -1
- package/dist/plugin/helpers/handleServerAction.d.ts +34 -0
- package/dist/plugin/helpers/handleServerAction.d.ts.map +1 -0
- package/dist/plugin/helpers/handleServerAction.js +48 -0
- package/dist/plugin/helpers/handleServerAction.js.map +1 -0
- package/dist/plugin/loader/handleExports.d.ts +16 -10
- package/dist/plugin/loader/handleExports.d.ts.map +1 -1
- package/dist/plugin/loader/handleExports.js +32 -16
- package/dist/plugin/loader/handleExports.js.map +1 -1
- package/dist/plugin/loader/transformModuleIfNeeded.d.ts.map +1 -1
- package/dist/plugin/loader/transformModuleIfNeeded.js +22 -11
- package/dist/plugin/loader/transformModuleIfNeeded.js.map +1 -1
- package/dist/plugin/loader/transformModuleWithPreservedFunctions.d.ts +21 -0
- package/dist/plugin/loader/transformModuleWithPreservedFunctions.d.ts.map +1 -1
- package/dist/plugin/loader/transformModuleWithPreservedFunctions.js +395 -73
- package/dist/plugin/loader/transformModuleWithPreservedFunctions.js.map +1 -1
- package/dist/plugin/loader/types.d.ts +9 -3
- package/dist/plugin/loader/types.d.ts.map +1 -1
- package/dist/plugin/react-client/configureWorkerRequestHandler.d.ts.map +1 -1
- package/dist/plugin/react-client/configureWorkerRequestHandler.js +12 -69
- package/dist/plugin/react-client/configureWorkerRequestHandler.js.map +1 -1
- package/dist/plugin/react-client/handleWorkerServerAction.d.ts +12 -0
- package/dist/plugin/react-client/handleWorkerServerAction.d.ts.map +1 -0
- package/dist/plugin/react-client/handleWorkerServerAction.js +47 -0
- package/dist/plugin/react-client/handleWorkerServerAction.js.map +1 -0
- package/dist/plugin/react-server/configureReactServer.js.map +1 -1
- package/dist/plugin/react-server/handleServerAction.d.ts +1 -1
- package/dist/plugin/react-server/handleServerAction.d.ts.map +1 -1
- package/dist/plugin/react-server/handleServerAction.js +42 -6
- package/dist/plugin/react-server/handleServerAction.js.map +1 -1
- package/dist/plugin/react-server/plugin.js +2 -2
- package/dist/plugin/react-server/plugin.js.map +1 -1
- package/dist/plugin/react-static/plugin.d.ts.map +1 -1
- package/dist/plugin/react-static/plugin.js +10 -2
- package/dist/plugin/react-static/plugin.js.map +1 -1
- package/dist/plugin/source-map/createMappingsSerializer.js +128 -157
- package/dist/plugin/source-map/createMappingsSerializer.js.map +1 -0
- package/dist/plugin/types.d.ts +5 -7
- package/dist/plugin/types.d.ts.map +1 -1
- package/dist/plugin/vendor/types.js +1 -0
- package/dist/plugin/worker/rsc/handleRender.d.ts.map +1 -1
- package/dist/plugin/worker/rsc/handleRender.js +0 -1
- package/dist/plugin/worker/rsc/handleRender.js.map +1 -1
- package/dist/plugin/worker/rsc/handlers.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/plugin/config/defaults.tsx +5 -2
- package/plugin/config/resolveOptions.ts +9 -10
- package/plugin/helpers/handleServerAction.ts +79 -0
- package/plugin/loader/handleExports.ts +50 -40
- package/plugin/loader/transformModuleIfNeeded.ts +50 -15
- package/plugin/loader/transformModuleWithPreservedFunctions.ts +494 -127
- package/plugin/loader/types.ts +12 -4
- package/plugin/react-client/configureWorkerRequestHandler.ts +11 -87
- package/plugin/react-client/handleWorkerServerAction.ts +74 -0
- package/plugin/react-server/configureReactServer.ts +1 -1
- package/plugin/react-server/handleServerAction.ts +49 -12
- package/plugin/react-server/plugin.ts +2 -2
- package/plugin/react-static/plugin.ts +10 -2
- package/plugin/types.ts +6 -7
- package/plugin/vendor/types.ts +1 -0
- package/plugin/worker/rsc/handleRender.ts +0 -2
- package/plugin/worker/rsc/handlers.ts +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleExports.js","sources":["../../../plugin/loader/handleExports.ts"],"sourcesContent":["import type { Program } from \"./types.js\";\nimport type { FunctionDeclaration, VariableDeclaration, VariableDeclarator } from \"acorn\";\n\n/**\n * Collects and organizes export information from a module.\n *\n * For all modules:\n * - Collects import statements\n * - Collects export names\n * - Collects declarations\n *\n * The actual transformation of exports (like wrapping with registerServerReference)\n * happens in transformModuleWithPreservedFunctions.\n *\n * @param source - The source code of the module\n * @param url - The URL of the module\n * @param program - The parsed AST program\n * @param isServerFunction - Whether this is a server module\n * @param isClientComponent - Whether this is a client module\n * @returns Object containing imports, declarations, and export names\n */\nexport function handleExports(\n source: string,\n program: Program,\n isServerFunction: boolean | RegExpMatchArray | null,\n isClientComponent: boolean | RegExpMatchArray | null\n): {\n imports: string[];\n declarations: string[];\n exportNames: string[];\n exports: Map<\n string,\n {\n type: \"function\" | \"class\" | \"variable\" | \"default\" | \"all\";\n declaration?: string;\n localName?: string;\n before?: string[];\n after?: string[];\n isAsync?: boolean;\n isServerAction?: boolean;\n }\n >;\n} {\n const imports: string[] = [];\n const declarations: string[] = [];\n const exportNames: string[] = [];\n // Track exports and their types\n const exports = new Map<\n string,\n {\n type: \"function\" | \"class\" | \"variable\" | \"default\" | \"all\";\n declaration?: string;\n localName?: string;\n before?: string[];\n after?: string[];\n isAsync?: boolean;\n isServerAction?: boolean;\n }\n >();\n\n let lastEnd = 0;\n let currentBefore: string[] = [];\n let foundFirstExport = false;\n\n // First pass: collect all exports and code between them\n for (const node of program.body) {\n // Add any code before this node\n if (node.start > lastEnd) {\n const beforeCode = source.slice(lastEnd, node.start);\n if (beforeCode.trim()) {\n currentBefore.push(beforeCode);\n }\n }\n\n if (node.type === \"ImportDeclaration\") {\n const importSource = node.source.value as string;\n const isServerImport = importSource.includes(\".server.\");\n imports.push(source.slice(node.start, node.end));\n\n // If this is a server import, mark all imported functions as server actions\n if (isServerImport) {\n for (const spec of node.specifiers) {\n if (spec.type === \"ImportSpecifier\") {\n const localName = spec.local.type === \"Identifier\" ? spec.local.name : \"\";\n const importedName = spec.imported.type === \"Identifier\" ? spec.imported.name : \"\";\n if (localName && importedName) {\n exports.set(localName, {\n type: \"function\",\n localName,\n isServerAction: true,\n before: [...currentBefore],\n });\n exportNames.push(localName);\n }\n }\n }\n }\n } else if (node.type === \"ExportAllDeclaration\") {\n // For export * from './other', just add the * export\n exports.set(\"*\", {\n type: \"all\",\n before: [...currentBefore],\n });\n currentBefore = [];\n exportNames.push(\"*\");\n } else if (node.type === \"ExportNamedDeclaration\") {\n if (!foundFirstExport) {\n // This is the first export, so all code before it goes into its before array\n foundFirstExport = true;\n }\n\n if (node.declaration) {\n if (\n node.declaration.type === \"FunctionDeclaration\" &&\n node.declaration.id\n ) {\n const name = node.declaration.id.name;\n exports.set(name, {\n type: \"function\",\n declaration: source.slice(node.declaration.start, node.declaration.end),\n before: [...currentBefore],\n isAsync: node.declaration.async,\n isServerAction: isServerFunction === true,\n });\n exportNames.push(name);\n currentBefore = [];\n } else if (\n node.declaration.type === \"ClassDeclaration\" &&\n node.declaration.id\n ) {\n const name = node.declaration.id.name;\n exports.set(name, {\n type: \"class\",\n declaration: source.slice(node.declaration.start, node.declaration.end),\n before: [...currentBefore],\n });\n exportNames.push(name);\n currentBefore = [];\n } else if (node.declaration.type === \"VariableDeclaration\") {\n for (const decl of node.declaration.declarations) {\n if (decl.id && decl.id.type === \"Identifier\") {\n const name = decl.id.name;\n const init = decl.init;\n const isFunction = init && (\n init.type === \"FunctionExpression\" ||\n init.type === \"ArrowFunctionExpression\"\n );\n const isAsync = isFunction && (\n (init.type === \"FunctionExpression\" && init.async === true) ||\n (init.type === \"ArrowFunctionExpression\" && init.async === true)\n );\n exports.set(name, {\n type: isFunction ? \"function\" : \"variable\",\n declaration: source.slice(decl.start, decl.end),\n before: [...currentBefore],\n isAsync: isAsync || false,\n isServerAction: isFunction != null && isServerFunction === true,\n });\n exportNames.push(name);\n currentBefore = [];\n }\n }\n }\n } else if (node.specifiers) {\n // For named exports (export { a, b, c })\n for (const spec of node.specifiers) {\n if (spec.type === \"ExportSpecifier\") {\n const localName = spec.local.type === \"Identifier\" ? spec.local.name : \"\";\n const exportedName = spec.exported.type === \"Identifier\" ? spec.exported.name : \"\";\n if (localName && exportedName) {\n // Find the function declaration in the AST\n const functionDecl = program.body.find(\n (node): node is FunctionDeclaration =>\n node.type === \"FunctionDeclaration\" &&\n node.id?.name === localName\n );\n\n if (functionDecl) {\n exports.set(exportedName, {\n type: \"function\",\n localName,\n declaration: source.slice(functionDecl.start, functionDecl.end),\n before: [...currentBefore],\n isAsync: functionDecl.async,\n isServerAction: isServerFunction === true,\n });\n exportNames.push(exportedName);\n } else {\n // If we can't find a function declaration, check if it's a variable declaration\n const varDecl = program.body.find(\n (node): node is VariableDeclaration =>\n node.type === \"VariableDeclaration\" &&\n node.declarations.some(\n (decl) =>\n decl.id.type === \"Identifier\" &&\n decl.id.name === localName &&\n decl.init &&\n (decl.init.type === \"FunctionExpression\" ||\n decl.init.type === \"ArrowFunctionExpression\")\n )\n );\n\n if (varDecl) {\n const decl = varDecl.declarations.find(\n (d: VariableDeclarator) => d.id.type === \"Identifier\" && d.id.name === localName\n );\n if (decl && decl.init) {\n const isAsync = decl.init.type === \"FunctionExpression\" ? decl.init.async :\n decl.init.type === \"ArrowFunctionExpression\" ? decl.init.async : false;\n exports.set(exportedName, {\n type: \"function\",\n localName,\n declaration: source.slice(decl.start, decl.end),\n before: [...currentBefore],\n isAsync,\n isServerAction: isServerFunction === true,\n });\n exportNames.push(exportedName);\n }\n } else {\n exports.set(exportedName, {\n type: \"variable\",\n localName,\n before: [...currentBefore],\n });\n exportNames.push(exportedName);\n }\n }\n }\n }\n }\n }\n } else if (node.type === \"ExportDefaultDeclaration\") {\n if (node.declaration && node.declaration.type === \"FunctionDeclaration\" && node.declaration.id) {\n exports.set(\"default\", {\n type: \"default\",\n before: [...currentBefore],\n localName: node.declaration.id.name, // Capture the function name\n isAsync: node.declaration.async\n });\n } else {\n exports.set(\"default\", {\n type: \"default\",\n before: [...currentBefore]\n });\n }\n currentBefore = []; // Reset for next export\n } else if (\n node.type === \"FunctionDeclaration\" ||\n node.type === \"VariableDeclaration\" ||\n node.type === \"ClassDeclaration\"\n ) {\n // For non-exported declarations:\n // - Keep all declarations for server functions\n // - Remove function declarations for client components\n if (\n isServerFunction ||\n !isClientComponent ||\n node.type !== \"FunctionDeclaration\"\n ) {\n declarations.push(source.slice(node.start, node.end));\n }\n }\n\n lastEnd = node.end;\n }\n\n // Add any remaining code after the last node\n if (lastEnd < source.length) {\n const afterCode = source.slice(lastEnd);\n if (afterCode.trim()) {\n // Add to the last export's after array\n const lastExport = Array.from(exports.values()).pop();\n if (lastExport) {\n lastExport.after = [afterCode];\n }\n }\n }\n\n // Second pass: generate transformed exports\n // Process all exports regardless of environment - the actual transformation\n // happens in transformModuleWithPreservedFunctions\n for (const [name, info] of exports) {\n if (info.before) {\n declarations.push(...info.before);\n }\n if (info.declaration) {\n declarations.push(info.declaration);\n }\n if (info.after) {\n declarations.push(...info.after);\n }\n if (!exportNames.includes(name)) {\n exportNames.push(name);\n }\n }\n\n return { imports, declarations, exportNames, exports };\n}\n"],"names":["node"],"mappings":";;;;;AAqBO,SAAS,aACd,CAAA,MAAA,EACA,OACA,EAAA,gBAAA,EACA,iBAiBA,EAAA;AACA,EAAA,MAAM,UAAoB,EAAC;AAC3B,EAAA,MAAM,eAAyB,EAAC;AAChC,EAAA,MAAM,cAAwB,EAAC;AAE/B,EAAM,MAAA,OAAA,uBAAc,GAWlB,EAAA;AAEF,EAAA,IAAI,OAAU,GAAA,CAAA;AACd,EAAA,IAAI,gBAA0B,EAAC;AAI/B,EAAW,KAAA,MAAA,IAAA,IAAQ,QAAQ,IAAM,EAAA;AAE/B,IAAI,IAAA,IAAA,CAAK,QAAQ,O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,EAAA;AACf,MAAa,YAAA,CAAA,IAAA,CAAK,GAAG,IAAA,CAAK,MAAM,CAAA;AAAA;AAElC,IAAA,IAAI,KAAK,WAAa,EAAA;AACpB,MAAa,YAAA,CAAA,IAAA,CAAK,KAAK,WAAW,CAAA;AAAA;AAEpC,IAAA,IAAI,KAAK,KAAO,EAAA;AACd,MAAa,YAAA,CAAA,IAAA,CAAK,GAAG,IAAA,CAAK,KAAK,CAAA;AAAA;AAEjC,IAAA,IAAI,CAAC,WAAA,CAAY,QAAS,CAAA,IAAI,CAAG,EAAA;AAC/B,MAAA,WAAA,CAAY,KAAK,IAAI,CAAA;AAAA;AACvB;AAGF,EAAA,OAAO,EAAE,OAAA,EAAS,YAAc,EAAA,WAAA,EAAa,OAAQ,EAAA;AACvD;;;;"}
|
|
1
|
+
{"version":3,"file":"handleExports.js","sources":["../../../plugin/loader/handleExports.ts"],"sourcesContent":["import type { Program, Node } from \"./types.js\";\nimport type { \n FunctionDeclaration, VariableDeclaration,\n VariableDeclarator\n} from \"acorn\";\n\nexport interface ExportInfo {\n name: string;\n localName?: string;\n type: \"function\" | \"variable\" | \"class\" | \"unknown\" | \"all\";\n node?: Node;\n declaration?: string;\n before?: string[];\n after?: string[];\n isAsync?: boolean;\n loc?: { line: number; column: number };\n}\n\n/**\n * Collects and organizes export information from a module.\n *\n * For all modules:\n * - Collects import statements\n * - Collects export names\n * - Collects declarations\n *\n * The actual transformation of exports (like wrapping with registerServerReference)\n * happens in transformModuleWithPreservedFunctions.\n *\n * @param source - The source code of the module\n * @param url - The URL of the module\n * @param program - The parsed AST program\n * @param isServerFunction - Whether this is a server module\n * @param isClientComponent - Whether this is a client module\n * @returns Object containing imports, declarations, and export names\n */\nexport function handleExports(\n source: string,\n program: Program,\n isServerFunction: boolean | RegExpMatchArray | null,\n isClientComponent: boolean | RegExpMatchArray | null\n): {\n imports: string[];\n declarations: string[];\n exportNames: string[];\n exports: Map<string, ExportInfo>;\n} {\n const imports: string[] = [];\n const declarations: string[] = [];\n const exportNames: string[] = [];\n const exports = new Map<string, ExportInfo>();\n\n let lastEnd = 0;\n let currentBefore: string[] = [];\n let foundFirstExport = false;\n\n // Helper function to get function type\n function getFunctionType(node: Node): \"function\" | \"variable\" | \"class\" | \"unknown\" {\n if (node.type === \"FunctionDeclaration\" || node.type === \"FunctionExpression\" || node.type === \"ArrowFunctionExpression\") {\n return \"function\";\n }\n if (node.type === \"ClassDeclaration\") {\n return \"class\";\n }\n if (node.type === \"VariableDeclaration\") {\n return \"variable\";\n }\n return \"unknown\";\n }\n\n // First pass: collect all exports and code between them\n for (const node of program.body) {\n // Add any code before this node\n if (node.start > lastEnd) {\n const beforeCode = source.slice(lastEnd, node.start);\n if (beforeCode.trim()) {\n currentBefore.push(beforeCode);\n }\n }\n\n if (node.type === \"ImportDeclaration\") {\n const importSource = node.source.value as string;\n const isServerImport = importSource.includes(\".server.\");\n imports.push(source.slice(node.start, node.end));\n\n // If this is a server import, mark all imported functions as server actions\n if (isServerImport) {\n for (const spec of node.specifiers) {\n if (spec.type === \"ImportSpecifier\") {\n const localName = spec.local.type === \"Identifier\" ? spec.local.name : \"\";\n const importedName = spec.imported.type === \"Identifier\" ? spec.imported.name : \"\";\n if (localName && importedName) {\n exports.set(localName, {\n name: localName,\n type: \"function\",\n localName,\n before: [...currentBefore],\n });\n exportNames.push(localName);\n }\n }\n }\n }\n } else if (node.type === \"ExportAllDeclaration\") {\n // For export * from './other', just add the * export\n exports.set(\"*\", {\n name: \"*\",\n type: \"all\",\n before: [...currentBefore],\n });\n currentBefore = [];\n exportNames.push(\"*\");\n } else if (node.type === \"ExportNamedDeclaration\") {\n if (!foundFirstExport) {\n // This is the first export, so all code before it goes into its before array\n foundFirstExport = true;\n }\n\n if (node.declaration) {\n if (\n node.declaration.type === \"FunctionDeclaration\" &&\n node.declaration.id\n ) {\n const name = node.declaration.id.name;\n exports.set(name, {\n name,\n type: getFunctionType(node.declaration),\n declaration: source.slice(node.declaration.start, node.declaration.end),\n before: [...currentBefore],\n isAsync: node.declaration.async,\n });\n exportNames.push(name);\n currentBefore = [];\n } else if (\n node.declaration.type === \"ClassDeclaration\" &&\n node.declaration.id\n ) {\n const name = node.declaration.id.name;\n exports.set(name, {\n name,\n type: getFunctionType(node.declaration),\n declaration: source.slice(node.declaration.start, node.declaration.end),\n before: [...currentBefore],\n });\n exportNames.push(name);\n currentBefore = [];\n } else if (node.declaration.type === \"VariableDeclaration\") {\n for (const decl of node.declaration.declarations) {\n if (decl.id && decl.id.type === \"Identifier\") {\n const name = decl.id.name;\n const init = decl.init;\n const isFunction = init != null && (\n init.type === \"FunctionExpression\" ||\n init.type === \"ArrowFunctionExpression\"\n );\n const isAsync = isFunction && (\n (init.type === \"FunctionExpression\" && init.async === true) ||\n (init.type === \"ArrowFunctionExpression\" && init.async === true)\n );\n exports.set(name, {\n name,\n type: isFunction ? \"function\" : \"variable\",\n declaration: source.slice(decl.start, decl.end),\n before: [...currentBefore],\n isAsync: isAsync || false,\n });\n exportNames.push(name);\n currentBefore = [];\n }\n }\n }\n } else if (node.specifiers) {\n // For named exports (export { a, b, c })\n for (const spec of node.specifiers) {\n if (spec.type === \"ExportSpecifier\") {\n const localName = spec.local.type === \"Identifier\" ? spec.local.name : \"\";\n const exportedName = spec.exported.type === \"Identifier\" ? spec.exported.name : \"\";\n if (localName && exportedName) {\n // Find the function declaration in the AST\n const functionDecl = program.body.find(\n (node): node is FunctionDeclaration =>\n node.type === \"FunctionDeclaration\" &&\n node.id?.name === localName\n );\n\n if (functionDecl) {\n exports.set(exportedName, {\n name: exportedName,\n type: getFunctionType(functionDecl),\n localName,\n declaration: source.slice(functionDecl.start, functionDecl.end),\n before: [...currentBefore],\n isAsync: functionDecl.async,\n });\n exportNames.push(exportedName);\n } else {\n // If we can't find a function declaration, check if it's a variable declaration\n const varDecl = program.body.find(\n (node): node is VariableDeclaration =>\n node.type === \"VariableDeclaration\" &&\n node.declarations.some(\n (decl) =>\n decl.id.type === \"Identifier\" &&\n decl.id.name === localName &&\n decl.init &&\n (decl.init.type === \"FunctionExpression\" ||\n decl.init.type === \"ArrowFunctionExpression\")\n )\n );\n\n if (varDecl) {\n const decl = varDecl.declarations.find(\n (d: VariableDeclarator) => d.id.type === \"Identifier\" && d.id.name === localName\n );\n if (decl && decl.init) {\n const isAsync = decl.init.type === \"FunctionExpression\" ? decl.init.async :\n decl.init.type === \"ArrowFunctionExpression\" ? decl.init.async : false;\n exports.set(exportedName, {\n name: exportedName,\n type: \"function\",\n localName,\n declaration: source.slice(decl.start, decl.end),\n before: [...currentBefore],\n isAsync,\n });\n exportNames.push(exportedName);\n }\n } else {\n exports.set(exportedName, {\n name: exportedName,\n type: \"variable\",\n localName,\n before: [...currentBefore],\n });\n exportNames.push(exportedName);\n }\n }\n }\n }\n }\n }\n } else if (node.type === \"ExportDefaultDeclaration\") {\n if (node.declaration && node.declaration.type === \"FunctionDeclaration\" && node.declaration.id) {\n exports.set(\"default\", {\n name: \"default\",\n type: getFunctionType(node.declaration),\n localName: node.declaration.id.name, // Capture the function name\n isAsync: node.declaration.async,\n });\n } else {\n exports.set(\"default\", {\n name: \"default\",\n type: \"unknown\",\n before: [...currentBefore]\n });\n }\n currentBefore = []; // Reset for next export\n } else if (\n node.type === \"FunctionDeclaration\" ||\n node.type === \"VariableDeclaration\" ||\n node.type === \"ClassDeclaration\"\n ) {\n // For non-exported declarations:\n // - Keep all declarations for server functions\n // - Remove function declarations for client components\n if (\n isServerFunction ||\n !isClientComponent ||\n node.type !== \"FunctionDeclaration\"\n ) {\n declarations.push(source.slice(node.start, node.end));\n }\n }\n\n lastEnd = node.end;\n }\n\n // Add any remaining code after the last node\n if (lastEnd < source.length) {\n const afterCode = source.slice(lastEnd);\n if (afterCode.trim()) {\n // Add to the last export's after array\n const lastExport = Array.from(exports.values()).pop();\n if (lastExport) {\n lastExport.after = [afterCode];\n }\n }\n }\n\n // Second pass: generate transformed exports\n // Process all exports regardless of environment - the actual transformation\n // happens in transformModuleWithPreservedFunctions\n for (const [name, info] of exports) {\n if (info.before) {\n declarations.push(...info.before);\n }\n if (info.declaration) {\n declarations.push(info.declaration);\n }\n if (info.after) {\n declarations.push(...info.after);\n }\n if (!exportNames.includes(name)) {\n exportNames.push(name);\n }\n }\n\n return { imports, declarations, exportNames, exports };\n}\n"],"names":["node"],"mappings":";;;;;AAoCO,SAAS,aACd,CAAA,MAAA,EACA,OACA,EAAA,gBAAA,EACA,iBAMA,EAAA;AACA,EAAA,MAAM,UAAoB,EAAC;AAC3B,EAAA,MAAM,eAAyB,EAAC;AAChC,EAAA,MAAM,cAAwB,EAAC;AAC/B,EAAM,MAAA,OAAA,uBAAc,GAAwB,EAAA;AAE5C,EAAA,IAAI,OAAU,GAAA,CAAA;AACd,EAAA,IAAI,gBAA0B,EAAC;AAI/B,EAAA,SAAS,gBAAgB,IAA2D,EAAA;AAClF,IAAI,IAAA,IAAA,CAAK,SAAS,qBAAyB,IAAA,IAAA,CAAK,SAAS,oBAAwB,IAAA,IAAA,CAAK,SAAS,yBAA2B,EAAA;AACxH,MAAO,OAAA,UAAA;AAAA;AAET,IAAI,IAAA,IAAA,CAAK,SAAS,kBAAoB,EAAA;AACpC,MAAO,OAAA,OAAA;AAAA;AAET,IAAI,IAAA,IAAA,CAAK,SAAS,qBAAuB,EAAA;AACvC,MAAO,OAAA,UAAA;AAAA;AAET,IAAO,OAAA,SAAA;AAAA;AAIT,EAAW,KAAA,MAAA,IAAA,IAAQ,QAAQ,IAAM,EAAA;AAE/B,IAAI,IAAA,IAAA,CAAK,QAAQ,OAAS,EAAA;AACxB,MAAA,MAAM,UAAa,GAAA,MAAA,CAAO,KAAM,CAAA,OAAA,EAAS,KAAK,KAAK,CAAA;AACnD,MAAI,IAAA,UAAA,CAAW,MAAQ,EAAA;AACrB,QAAA,aAAA,CAAc,KAAK,UAAU,CAAA;AAAA;AAC/B;AAGF,IAAI,IAAA,IAAA,CAAK,SAAS,mBAAqB,EAAA;AACrC,MAAM,MAAA,YAAA,GAAe,K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AEpC,IAAA,IAAI,KAAK,KAAO,EAAA;AACd,MAAa,YAAA,CAAA,IAAA,CAAK,GAAG,IAAA,CAAK,KAAK,CAAA;AAAA;AAEjC,IAAA,IAAI,CAAC,WAAA,CAAY,QAAS,CAAA,IAAI,CAAG,EAAA;AAC/B,MAAA,WAAA,CAAY,KAAK,IAAI,CAAA;AAAA;AACvB;AAGF,EAAA,OAAO,EAAE,OAAA,EAAS,YAAc,EAAA,WAAA,EAAa,OAAQ,EAAA;AACvD;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformModuleIfNeeded.d.ts","sourceRoot":"","sources":["../../../plugin/loader/transformModuleIfNeeded.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transformModuleIfNeeded.d.ts","sourceRoot":"","sources":["../../../plugin/loader/transformModuleIfNeeded.ts"],"names":[],"mappings":"AAyBA,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,gBAAgB,GAAE,OAAO,GAAG,gBAAgB,GAAG,IAA0E,EACzH,iBAAiB,GAAE,OAAO,GAAG,gBAAgB,GAAG,IAA2E,EAC3H,mBAAmB,UAAoC,UAkCxD"}
|
|
@@ -8,18 +8,29 @@ import { transformModuleWithPreservedFunctions } from './transformModuleWithPres
|
|
|
8
8
|
import { parse } from './parse.js';
|
|
9
9
|
import { DEFAULT_CONFIG } from '../config/defaults.js';
|
|
10
10
|
|
|
11
|
-
function transformModuleIfNeeded(source, moduleId, isServerFunction = DEFAULT_CONFIG.AUTO_DISCOVER.isServerFunctionCode(source, moduleId), isClientComponent = DEFAULT_CONFIG.AUTO_DISCOVER.isClientComponentCode(source), isServerEnvironment = getCondition() === "react-server") {
|
|
12
|
-
if (isServerEnvironment
|
|
13
|
-
|
|
11
|
+
function transformModuleIfNeeded(source, moduleId, isServerFunction = DEFAULT_CONFIG.AUTO_DISCOVER.isServerFunctionCode(source, moduleId), isClientComponent = DEFAULT_CONFIG.AUTO_DISCOVER.isClientComponentCode(source, moduleId), isServerEnvironment = getCondition() === "react-server") {
|
|
12
|
+
if (isServerEnvironment) {
|
|
13
|
+
if (!isServerFunction && !isClientComponent) {
|
|
14
|
+
return source;
|
|
15
|
+
}
|
|
16
|
+
} else {
|
|
17
|
+
if (isClientComponent) {
|
|
18
|
+
return source;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const result = transformModuleWithPreservedFunctions(
|
|
23
|
+
source,
|
|
24
|
+
moduleId,
|
|
25
|
+
parse(source),
|
|
26
|
+
isServerFunction,
|
|
27
|
+
isClientComponent
|
|
28
|
+
);
|
|
29
|
+
return result;
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error(`Error transforming module ${moduleId}:`, error);
|
|
32
|
+
throw error;
|
|
14
33
|
}
|
|
15
|
-
const result = transformModuleWithPreservedFunctions(
|
|
16
|
-
source,
|
|
17
|
-
moduleId,
|
|
18
|
-
parse(source),
|
|
19
|
-
isServerFunction,
|
|
20
|
-
isClientComponent
|
|
21
|
-
);
|
|
22
|
-
return result;
|
|
23
34
|
}
|
|
24
35
|
|
|
25
36
|
export { transformModuleIfNeeded };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformModuleIfNeeded.js","sources":["../../../plugin/loader/transformModuleIfNeeded.ts"],"sourcesContent":["import { getCondition } from \"../config/getCondition.js\";\nimport { transformModuleWithPreservedFunctions } from \"./transformModuleWithPreservedFunctions.js\";\nimport { parse } from \"./parse.js\";\nimport { DEFAULT_CONFIG } from \"../config/defaults.js\";\n\nexport function transformModuleIfNeeded(\n source: string,\n moduleId: string,\n isServerFunction: boolean | RegExpMatchArray | null = DEFAULT_CONFIG.AUTO_DISCOVER.isServerFunctionCode(source, moduleId),\n isClientComponent: boolean | RegExpMatchArray | null = DEFAULT_CONFIG.AUTO_DISCOVER.isClientComponentCode(source),\n isServerEnvironment = getCondition() === \"react-server\"\n) {\n // Handle environment-specific cases\n if (\n
|
|
1
|
+
{"version":3,"file":"transformModuleIfNeeded.js","sources":["../../../plugin/loader/transformModuleIfNeeded.ts"],"sourcesContent":["import { getCondition } from \"../config/getCondition.js\";\nimport { transformModuleWithPreservedFunctions } from \"./transformModuleWithPreservedFunctions.js\";\nimport { parse } from \"./parse.js\";\nimport { DEFAULT_CONFIG } from \"../config/defaults.js\";\n\n// --- React RSC Directive Handling ---\n//\n// 1. 'use client' at file top:\n// - Pass through code as-is (after removing directive).\n// - Do not transform or register anything.\n// - Required for React client features to work.\n// - See: https://react.dev/reference/rsc/use-client\n//\n// 2. 'use server' at file top:\n// - Register all exported async functions as server actions.\n// - See: https://react.dev/reference/rsc/use-server\n//\n// 3. 'use server' at function top:\n// - Register only that async function as a server action.\n// - See: https://react.dev/reference/rsc/use-server\n//\n// 4. No directive:\n// - Treat as a normal shared or server-only module.\n// - No special registration or transformation.\n\nexport function transformModuleIfNeeded(\n source: string,\n moduleId: string,\n isServerFunction: boolean | RegExpMatchArray | null = DEFAULT_CONFIG.AUTO_DISCOVER.isServerFunctionCode(source, moduleId),\n isClientComponent: boolean | RegExpMatchArray | null = DEFAULT_CONFIG.AUTO_DISCOVER.isClientComponentCode(source, moduleId),\n isServerEnvironment = getCondition() === \"react-server\"\n) {\n // Handle environment-specific cases\n if (isServerEnvironment) {\n // In server environment:\n // - Server functions need transformation\n // - Client components need transformation\n // - Other modules can pass through\n if (!isServerFunction && !isClientComponent) {\n return source;\n }\n } else {\n // In client environment:\n // - Only client components should pass through\n // - Server functions should be transformed\n if (isClientComponent) {\n return source;\n }\n }\n\n try {\n const result = transformModuleWithPreservedFunctions(\n source,\n moduleId,\n parse(source),\n isServerFunction,\n isClientComponent\n );\n return result;\n } catch (error) {\n // Log the error and rethrow\n console.error(`Error transforming module ${moduleId}:`, error);\n throw error;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;AAyBO,SAAS,uBAAA,CACd,QACA,QACA,EAAA,gBAAA,GAAsD,eAAe,aAAc,CAAA,oBAAA,CAAqB,QAAQ,QAAQ,CAAA,EACxH,oBAAuD,cAAe,CAAA,aAAA,CAAc,sBAAsB,MAAQ,EAAA,QAAQ,GAC1H,mBAAsB,GAAA,YAAA,OAAmB,cACzC,EAAA;AAEA,EAAA,IAAI,mBAAqB,EAAA;AAKvB,IAAI,IAAA,CAAC,gBAAoB,IAAA,CAAC,iBAAmB,EAAA;AAC3C,MAAO,OAAA,MAAA;AAAA;AACT,GACK,MAAA;AAIL,IAAA,IAAI,iBAAmB,EAAA;AACrB,MAAO,OAAA,MAAA;AAAA;AACT;AAGF,EAAI,IAAA;AACF,IAAA,MAAM,MAAS,GAAA,qCAAA;AAAA,MACb,MAAA;AAAA,MACA,QAAA;AAAA,MACA,MAAM,MAAM,CAAA;AAAA,MACZ,gBAAA;AAAA,MACA;AAAA,KACF;AACA,IAAO,OAAA,MAAA;AAAA,WACA,KAAO,EAAA;AAEd,IAAA,OAAA,CAAQ,KAAM,CAAA,CAAA,0BAAA,EAA6B,QAAQ,CAAA,CAAA,CAAA,EAAK,KAAK,CAAA;AAC7D,IAAM,MAAA,KAAA;AAAA;AAEV;;;;"}
|
|
@@ -8,6 +8,27 @@ export interface TransformOptions {
|
|
|
8
8
|
isClientModule?: boolean;
|
|
9
9
|
program?: Program;
|
|
10
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* --- React RSC Directive Handling ---
|
|
13
|
+
*
|
|
14
|
+
* 1. 'use client' at file top:
|
|
15
|
+
* - Pass through code as-is (after removing directive).
|
|
16
|
+
* - Do not transform or register anything.
|
|
17
|
+
* - Required for React client features to work.
|
|
18
|
+
* - See: https://react.dev/reference/rsc/use-client
|
|
19
|
+
*
|
|
20
|
+
* 2. 'use server' at file top:
|
|
21
|
+
* - Register all exported async functions as server actions.
|
|
22
|
+
* - See: https://react.dev/reference/rsc/use-server#caveats
|
|
23
|
+
*
|
|
24
|
+
* 3. 'use server' at function top:
|
|
25
|
+
* - Register only that async function as a server action.
|
|
26
|
+
* - See: https://react.dev/reference/rsc/use-server#caveats
|
|
27
|
+
*
|
|
28
|
+
* 4. No directive:
|
|
29
|
+
* - Treat as a normal shared or server-only module.
|
|
30
|
+
* - No special registration or transformation.
|
|
31
|
+
*/
|
|
11
32
|
/**
|
|
12
33
|
* Transforms a module for RSC boundaries.
|
|
13
34
|
* - Server modules: exports are wrapped with server references while preserving functionality.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformModuleWithPreservedFunctions.d.ts","sourceRoot":"","sources":["../../../plugin/loader/transformModuleWithPreservedFunctions.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAG1C,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;
|
|
1
|
+
{"version":3,"file":"transformModuleWithPreservedFunctions.d.ts","sourceRoot":"","sources":["../../../plugin/loader/transformModuleWithPreservedFunctions.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAG1C,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAuDD;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,qCAAqC,CACnD,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,OAAO,EAChB,gBAAgB,EAAE,OAAO,GAAG,gBAAgB,GAAG,IAAI,EACnD,iBAAiB,EAAE,OAAO,GAAG,gBAAgB,GAAG,IAAI,GACnD,MAAM,CAgdR"}
|