rwsdk 0.1.16 → 0.1.17-test.20250715200658
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/dist/lib/$.d.mts +8 -0
- package/dist/lib/$.mjs +5 -0
- package/dist/lib/constants.d.mts +4 -0
- package/dist/lib/constants.mjs +6 -0
- package/dist/lib/findWranglerConfig.d.mts +1 -0
- package/dist/lib/findWranglerConfig.mjs +12 -0
- package/dist/lib/getShortName.d.mts +1 -0
- package/dist/lib/getShortName.mjs +2 -0
- package/dist/lib/getSrcPaths.d.ts +15 -0
- package/dist/lib/getSrcPaths.js +80 -0
- package/dist/lib/hasPkgScript.d.mts +1 -0
- package/dist/lib/hasPkgScript.mjs +9 -0
- package/dist/lib/jsonUtils.d.mts +28 -0
- package/dist/lib/jsonUtils.mjs +167 -0
- package/dist/lib/setupEnvFiles.d.mts +4 -0
- package/dist/lib/setupEnvFiles.mjs +31 -0
- package/dist/lib/smokeTests/artifacts.d.mts +10 -0
- package/dist/lib/smokeTests/artifacts.mjs +164 -0
- package/dist/lib/smokeTests/browser.d.mts +48 -0
- package/dist/lib/smokeTests/browser.mjs +1041 -0
- package/dist/lib/smokeTests/cleanup.d.mts +5 -0
- package/dist/lib/smokeTests/cleanup.mjs +214 -0
- package/dist/lib/smokeTests/codeUpdates.d.mts +8 -0
- package/dist/lib/smokeTests/codeUpdates.mjs +229 -0
- package/dist/lib/smokeTests/constants.d.mts +5 -0
- package/dist/lib/smokeTests/constants.mjs +10 -0
- package/dist/lib/smokeTests/development.d.mts +11 -0
- package/dist/lib/smokeTests/development.mjs +209 -0
- package/dist/lib/smokeTests/environment.d.mts +14 -0
- package/dist/lib/smokeTests/environment.mjs +163 -0
- package/dist/lib/smokeTests/release.d.mts +61 -0
- package/dist/lib/smokeTests/release.mjs +526 -0
- package/dist/lib/smokeTests/reporting.d.mts +13 -0
- package/dist/lib/smokeTests/reporting.mjs +355 -0
- package/dist/lib/smokeTests/runSmokeTests.d.mts +5 -0
- package/dist/lib/smokeTests/runSmokeTests.mjs +144 -0
- package/dist/lib/smokeTests/state.d.mts +48 -0
- package/dist/lib/smokeTests/state.mjs +57 -0
- package/dist/lib/smokeTests/templates/SmokeTest.template.d.ts +1 -0
- package/dist/lib/smokeTests/templates/SmokeTest.template.js +81 -0
- package/dist/lib/smokeTests/templates/SmokeTestClient.template.d.ts +1 -0
- package/dist/lib/smokeTests/templates/SmokeTestClient.template.js +159 -0
- package/dist/lib/smokeTests/templates/smokeTestFunctions.template.d.ts +1 -0
- package/dist/lib/smokeTests/templates/smokeTestFunctions.template.js +19 -0
- package/dist/lib/smokeTests/types.d.mts +75 -0
- package/dist/lib/smokeTests/utils.d.mts +15 -0
- package/dist/lib/smokeTests/utils.mjs +147 -0
- package/dist/llms/index.d.ts +3 -0
- package/dist/llms/index.js +35 -0
- package/dist/llms/rules/interruptors.d.ts +1 -0
- package/dist/llms/rules/interruptors.js +243 -0
- package/dist/llms/rules/middleware.d.ts +1 -0
- package/dist/llms/rules/middleware.js +71 -0
- package/dist/llms/rules/react.d.ts +1 -0
- package/dist/llms/rules/react.js +106 -0
- package/dist/llms/rules/request-response.d.ts +1 -0
- package/dist/llms/rules/request-response.js +209 -0
- package/dist/runtime/client.d.ts +17 -0
- package/dist/runtime/client.js +74 -0
- package/dist/runtime/clientNavigation.d.ts +4 -0
- package/dist/runtime/clientNavigation.js +53 -0
- package/dist/runtime/clientNavigation.test.d.ts +1 -0
- package/dist/runtime/clientNavigation.test.js +55 -0
- package/dist/runtime/constants.d.ts +1 -0
- package/dist/runtime/constants.js +1 -0
- package/dist/runtime/entries/auth.d.ts +1 -0
- package/dist/runtime/entries/auth.js +1 -0
- package/dist/runtime/entries/client.d.ts +4 -0
- package/dist/runtime/entries/client.js +4 -0
- package/dist/runtime/entries/clientSSR.d.ts +1 -0
- package/dist/runtime/entries/clientSSR.js +1 -0
- package/dist/runtime/entries/no-react-server.d.ts +0 -0
- package/dist/runtime/entries/no-react-server.js +2 -0
- package/dist/runtime/entries/react-server-only.d.ts +0 -0
- package/dist/runtime/entries/react-server-only.js +2 -0
- package/dist/runtime/entries/router.d.ts +2 -0
- package/dist/runtime/entries/router.js +2 -0
- package/dist/runtime/entries/ssr.d.ts +1 -0
- package/dist/runtime/entries/ssr.js +1 -0
- package/dist/runtime/entries/worker.d.ts +9 -0
- package/dist/runtime/entries/worker.js +9 -0
- package/dist/runtime/error.d.ts +6 -0
- package/dist/runtime/error.js +8 -0
- package/dist/runtime/imports/ClientOnly.d.ts +3 -0
- package/dist/runtime/imports/ClientOnly.js +8 -0
- package/dist/runtime/imports/client.d.ts +4 -0
- package/dist/runtime/imports/client.js +33 -0
- package/dist/runtime/imports/ssr.d.ts +5 -0
- package/dist/runtime/imports/ssr.js +20 -0
- package/dist/runtime/imports/worker.d.ts +5 -0
- package/dist/runtime/imports/worker.js +22 -0
- package/dist/runtime/lib/auth/index.d.ts +1 -0
- package/dist/runtime/lib/auth/index.js +1 -0
- package/dist/runtime/lib/auth/session.d.ts +50 -0
- package/dist/runtime/lib/auth/session.js +148 -0
- package/dist/runtime/lib/db/DOWorkerDialect.d.ts +29 -0
- package/dist/runtime/lib/db/DOWorkerDialect.js +66 -0
- package/dist/runtime/lib/db/SqliteDurableObject.d.ts +14 -0
- package/dist/runtime/lib/db/SqliteDurableObject.js +42 -0
- package/dist/runtime/lib/db/createDb.d.ts +2 -0
- package/dist/runtime/lib/db/createDb.js +33 -0
- package/dist/runtime/lib/db/index.d.ts +4 -0
- package/dist/runtime/lib/db/index.js +3 -0
- package/dist/runtime/lib/db/migrations.d.ts +23 -0
- package/dist/runtime/lib/db/migrations.js +34 -0
- package/dist/runtime/lib/db/typeInference/assert.d.ts +2 -0
- package/dist/runtime/lib/db/typeInference/assert.js +1 -0
- package/dist/runtime/lib/db/typeInference/builders/alterColumn.d.ts +27 -0
- package/dist/runtime/lib/db/typeInference/builders/alterColumn.js +1 -0
- package/dist/runtime/lib/db/typeInference/builders/alterTable.d.ts +53 -0
- package/dist/runtime/lib/db/typeInference/builders/alterTable.js +1 -0
- package/dist/runtime/lib/db/typeInference/builders/columnDefinition.d.ts +26 -0
- package/dist/runtime/lib/db/typeInference/builders/columnDefinition.js +1 -0
- package/dist/runtime/lib/db/typeInference/builders/createTable.d.ts +49 -0
- package/dist/runtime/lib/db/typeInference/builders/createTable.js +1 -0
- package/dist/runtime/lib/db/typeInference/builders/createView.d.ts +17 -0
- package/dist/runtime/lib/db/typeInference/builders/createView.js +1 -0
- package/dist/runtime/lib/db/typeInference/builders/dropTable.d.ts +11 -0
- package/dist/runtime/lib/db/typeInference/builders/dropTable.js +1 -0
- package/dist/runtime/lib/db/typeInference/builders/dropView.d.ts +12 -0
- package/dist/runtime/lib/db/typeInference/builders/dropView.js +1 -0
- package/dist/runtime/lib/db/typeInference/builders/schema.d.ts +24 -0
- package/dist/runtime/lib/db/typeInference/builders/schema.js +1 -0
- package/dist/runtime/lib/db/typeInference/database.d.ts +27 -0
- package/dist/runtime/lib/db/typeInference/database.js +1 -0
- package/dist/runtime/lib/db/typeInference/typetests/alterTable.typetest.d.ts +1 -0
- package/dist/runtime/lib/db/typeInference/typetests/alterTable.typetest.js +360 -0
- package/dist/runtime/lib/db/typeInference/typetests/createTable.typetest.d.ts +1 -0
- package/dist/runtime/lib/db/typeInference/typetests/createTable.typetest.js +33 -0
- package/dist/runtime/lib/db/typeInference/typetests/dropTable.typetest.d.ts +1 -0
- package/dist/runtime/lib/db/typeInference/typetests/dropTable.typetest.js +143 -0
- package/dist/runtime/lib/db/typeInference/typetests/print.d.ts +3 -0
- package/dist/runtime/lib/db/typeInference/typetests/print.js +1 -0
- package/dist/runtime/lib/db/typeInference/typetests/testUtils.d.ts +2 -0
- package/dist/runtime/lib/db/typeInference/typetests/testUtils.js +1 -0
- package/dist/runtime/lib/db/typeInference/typetests/typeInference.typetest.d.ts +1 -0
- package/dist/runtime/lib/db/typeInference/typetests/typeInference.typetest.js +17 -0
- package/dist/runtime/lib/db/typeInference/utils.d.ts +82 -0
- package/dist/runtime/lib/db/typeInference/utils.js +2 -0
- package/dist/runtime/lib/debug.d.ts +2 -0
- package/dist/runtime/lib/debug.js +36 -0
- package/dist/runtime/lib/links.d.ts +14 -0
- package/dist/runtime/lib/links.js +38 -0
- package/dist/runtime/lib/realtime/client.d.ts +7 -0
- package/dist/runtime/lib/realtime/client.js +166 -0
- package/dist/runtime/lib/realtime/constants.d.ts +1 -0
- package/dist/runtime/lib/realtime/constants.js +1 -0
- package/dist/runtime/lib/realtime/durableObject.d.ts +29 -0
- package/dist/runtime/lib/realtime/durableObject.js +187 -0
- package/dist/runtime/lib/realtime/renderRealtimeClients.d.ts +7 -0
- package/dist/runtime/lib/realtime/renderRealtimeClients.js +6 -0
- package/dist/runtime/lib/realtime/shared.d.ts +10 -0
- package/dist/runtime/lib/realtime/shared.js +10 -0
- package/dist/runtime/lib/realtime/validateUpgradeRequest.d.ts +6 -0
- package/dist/runtime/lib/realtime/validateUpgradeRequest.js +29 -0
- package/dist/runtime/lib/realtime/worker.d.ts +3 -0
- package/dist/runtime/lib/realtime/worker.js +16 -0
- package/dist/runtime/lib/router.d.ts +56 -0
- package/dist/runtime/lib/router.js +210 -0
- package/dist/runtime/lib/router.test.d.ts +1 -0
- package/dist/runtime/lib/router.test.js +58 -0
- package/dist/runtime/lib/streams/consumeEventStream.d.ts +4 -0
- package/dist/runtime/lib/streams/consumeEventStream.js +13 -0
- package/dist/runtime/lib/turnstile/TurnstileScript.d.ts +1 -0
- package/dist/runtime/lib/turnstile/TurnstileScript.js +2 -0
- package/dist/runtime/lib/turnstile/turnstile.d.ts +3 -0
- package/dist/runtime/lib/turnstile/turnstile.js +3 -0
- package/dist/runtime/lib/turnstile/useTurnstile.d.ts +4 -0
- package/dist/runtime/lib/turnstile/useTurnstile.js +23 -0
- package/dist/runtime/lib/turnstile/verifyTurnstileToken.d.ts +4 -0
- package/dist/runtime/lib/turnstile/verifyTurnstileToken.js +15 -0
- package/dist/runtime/lib/utils.d.ts +1 -0
- package/dist/runtime/lib/utils.js +1 -0
- package/dist/runtime/register/client.d.ts +1 -0
- package/dist/runtime/register/client.js +5 -0
- package/dist/runtime/register/ssr.d.ts +3 -0
- package/dist/runtime/register/ssr.js +26 -0
- package/dist/runtime/register/worker.d.ts +4 -0
- package/dist/runtime/register/worker.js +42 -0
- package/dist/runtime/render/createClientManifest.d.ts +1 -0
- package/dist/runtime/render/createClientManifest.js +7 -0
- package/dist/runtime/render/createModuleMap.d.ts +1 -0
- package/dist/runtime/render/createModuleMap.js +13 -0
- package/dist/runtime/render/renderRscThenableToHtmlStream.d.ts +9 -0
- package/dist/runtime/render/renderRscThenableToHtmlStream.js +49 -0
- package/dist/runtime/render/renderToRscStream.d.ts +5 -0
- package/dist/runtime/render/renderToRscStream.js +46 -0
- package/dist/runtime/render/renderToStream.d.ts +9 -0
- package/dist/runtime/render/renderToStream.js +27 -0
- package/dist/runtime/render/renderToString.d.ts +7 -0
- package/dist/runtime/render/renderToString.js +26 -0
- package/dist/runtime/render/transformRscToHtmlStream.d.ts +8 -0
- package/dist/runtime/render/transformRscToHtmlStream.js +19 -0
- package/dist/runtime/requestInfo/types.d.ts +11 -0
- package/dist/runtime/requestInfo/types.js +1 -0
- package/dist/runtime/requestInfo/worker.d.ts +5 -0
- package/dist/runtime/requestInfo/worker.js +33 -0
- package/dist/runtime/script.d.ts +5 -0
- package/dist/runtime/script.js +8 -0
- package/dist/runtime/ssrBridge.d.ts +2 -0
- package/dist/runtime/ssrBridge.js +11 -0
- package/dist/runtime/worker.d.ts +18 -0
- package/dist/runtime/worker.js +173 -0
- package/dist/scripts/__sdk.d.mts +1 -0
- package/dist/scripts/__sdk.mjs +14 -0
- package/dist/scripts/debug-sync.d.mts +6 -0
- package/dist/scripts/debug-sync.mjs +224 -0
- package/dist/scripts/dev-init.d.mts +1 -0
- package/dist/scripts/dev-init.mjs +25 -0
- package/dist/scripts/ensure-deploy-env.d.mts +1 -0
- package/dist/scripts/ensure-deploy-env.mjs +271 -0
- package/dist/scripts/ensure-env.d.mts +1 -0
- package/dist/scripts/ensure-env.mjs +9 -0
- package/dist/scripts/migrate-new.d.mts +1 -0
- package/dist/scripts/migrate-new.mjs +51 -0
- package/dist/scripts/smoke-test.d.mts +1 -0
- package/dist/scripts/smoke-test.mjs +166 -0
- package/dist/scripts/worker-run.d.mts +1 -0
- package/dist/scripts/worker-run.mjs +82 -0
- package/dist/vite/checkIsUsingPrisma.d.mts +6 -0
- package/dist/vite/checkIsUsingPrisma.mjs +18 -0
- package/dist/vite/configPlugin.d.mts +9 -0
- package/dist/vite/configPlugin.mjs +169 -0
- package/dist/vite/createDirectiveLookupPlugin.d.mts +21 -0
- package/dist/vite/createDirectiveLookupPlugin.mjs +231 -0
- package/dist/vite/devServerTimingPlugin.d.mts +2 -0
- package/dist/vite/devServerTimingPlugin.mjs +24 -0
- package/dist/vite/directivesPlugin.d.mts +6 -0
- package/dist/vite/directivesPlugin.mjs +200 -0
- package/dist/vite/ensureAliasArray.d.mts +2 -0
- package/dist/vite/ensureAliasArray.mjs +17 -0
- package/dist/vite/findSpecifiers.d.mts +31 -0
- package/dist/vite/findSpecifiers.mjs +230 -0
- package/dist/vite/findSsrSpecifiers.d.mts +11 -0
- package/dist/vite/findSsrSpecifiers.mjs +67 -0
- package/dist/vite/hasDirective.d.mts +7 -0
- package/dist/vite/hasDirective.mjs +54 -0
- package/dist/vite/hasOwnCloudflareVitePlugin.d.mts +3 -0
- package/dist/vite/hasOwnCloudflareVitePlugin.mjs +14 -0
- package/dist/vite/index.d.mts +1 -0
- package/dist/vite/index.mjs +1 -0
- package/dist/vite/injectVitePreamblePlugin.d.mts +4 -0
- package/dist/vite/injectVitePreamblePlugin.mjs +23 -0
- package/dist/vite/invalidateCacheIfPrismaClientChanged.d.mts +3 -0
- package/dist/vite/invalidateCacheIfPrismaClientChanged.mjs +27 -0
- package/dist/vite/invalidateModule.d.mts +6 -0
- package/dist/vite/invalidateModule.mjs +30 -0
- package/dist/vite/miniflareHMRPlugin.d.mts +10 -0
- package/dist/vite/miniflareHMRPlugin.mjs +209 -0
- package/dist/vite/moveStaticAssetsPlugin.d.mts +4 -0
- package/dist/vite/moveStaticAssetsPlugin.mjs +12 -0
- package/dist/vite/normalizeModulePath.d.mts +1 -0
- package/dist/vite/normalizeModulePath.mjs +13 -0
- package/dist/vite/prismaPlugin.d.mts +4 -0
- package/dist/vite/prismaPlugin.mjs +43 -0
- package/dist/vite/reactConditionsResolverPlugin.d.mts +16 -0
- package/dist/vite/reactConditionsResolverPlugin.mjs +179 -0
- package/dist/vite/redwoodPlugin.d.mts +12 -0
- package/dist/vite/redwoodPlugin.mjs +105 -0
- package/dist/vite/ssrBridgePlugin.d.mts +7 -0
- package/dist/vite/ssrBridgePlugin.mjs +137 -0
- package/dist/vite/transformClientComponents.d.mts +12 -0
- package/dist/vite/transformClientComponents.mjs +116 -0
- package/dist/vite/transformClientComponents.test.d.mts +1 -0
- package/dist/vite/transformClientComponents.test.mjs +264 -0
- package/dist/vite/transformJsxScriptTagsPlugin.d.mts +8 -0
- package/dist/vite/transformJsxScriptTagsPlugin.mjs +315 -0
- package/dist/vite/transformJsxScriptTagsPlugin.test.d.mts +1 -0
- package/dist/vite/transformJsxScriptTagsPlugin.test.mjs +334 -0
- package/dist/vite/transformServerFunctions.d.mts +16 -0
- package/dist/vite/transformServerFunctions.mjs +296 -0
- package/dist/vite/transformServerFunctions.test.d.mts +1 -0
- package/dist/vite/transformServerFunctions.test.mjs +124 -0
- package/dist/vite/useClientLookupPlugin.d.mts +5 -0
- package/dist/vite/useClientLookupPlugin.mjs +15 -0
- package/dist/vite/useServerLookupPlugin.d.mts +5 -0
- package/dist/vite/useServerLookupPlugin.mjs +15 -0
- package/dist/vite/useServerPlugin.d.mts +1 -0
- package/dist/vite/useServerPlugin.mjs +1 -0
- package/dist/vite/virtualPlugin.d.mts +2 -0
- package/dist/vite/virtualPlugin.mjs +18 -0
- package/dist/vite/vitePreamblePlugin.d.mts +1 -0
- package/dist/vite/vitePreamblePlugin.mjs +11 -0
- package/package.json +1 -1
- package/dist/runtime/lib/db/typeInference/builders/table.d.ts +0 -10
- package/dist/vite/invalidateClientModule.d.mts +0 -2
- package/dist/vite/invalidateClientModule.mjs +0 -8
- package/dist/vite/invalidateModule copy.d.mts +0 -2
- package/dist/vite/invalidateModule copy.mjs +0 -14
- package/dist/vite/invalidateSSRModule.d.mts +0 -2
- package/dist/vite/invalidateSSRModule.mjs +0 -7
- package/dist/vite/mode.d.mts +0 -5
- package/dist/vite/mode.mjs +0 -25
- package/dist/vite/modePlugin.d.mts +0 -2
- package/dist/vite/modePlugin.mjs +0 -10
- /package/dist/{runtime/lib/db/typeInference/builders/table.js → lib/smokeTests/types.mjs} +0 -0
- /package/dist/vite/{isJsFile.d.ts → isJsFile.d.mts} +0 -0
- /package/dist/vite/{isJsFile.js → isJsFile.mjs} +0 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import path, { resolve } from "node:path";
|
|
2
|
+
import { builtinModules } from "node:module";
|
|
3
|
+
import enhancedResolve from "enhanced-resolve";
|
|
4
|
+
import { SSR_BRIDGE_PATH } from "../lib/constants.mjs";
|
|
5
|
+
// port(justinvdm, 09 Jun 2025):
|
|
6
|
+
// https://github.com/cloudflare/workers-sdk/blob/d533f5ee7da69c205d8d5e2a5f264d2370fc612b/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts#L123-L128
|
|
7
|
+
export const cloudflareBuiltInModules = [
|
|
8
|
+
"cloudflare:email",
|
|
9
|
+
"cloudflare:sockets",
|
|
10
|
+
"cloudflare:workers",
|
|
11
|
+
"cloudflare:workflows",
|
|
12
|
+
];
|
|
13
|
+
export const externalModules = [
|
|
14
|
+
...cloudflareBuiltInModules,
|
|
15
|
+
...builtinModules,
|
|
16
|
+
...builtinModules.map((m) => `node:${m}`),
|
|
17
|
+
];
|
|
18
|
+
export const configPlugin = ({ silent, projectRootDir, clientEntryPathnames, workerEntryPathname, }) => ({
|
|
19
|
+
name: "rwsdk:config",
|
|
20
|
+
config: async (_) => {
|
|
21
|
+
const mode = process.env.NODE_ENV;
|
|
22
|
+
const baseConfig = {
|
|
23
|
+
appType: "custom",
|
|
24
|
+
mode,
|
|
25
|
+
logLevel: silent ? "silent" : "info",
|
|
26
|
+
build: {
|
|
27
|
+
minify: mode !== "development",
|
|
28
|
+
sourcemap: true,
|
|
29
|
+
},
|
|
30
|
+
define: {
|
|
31
|
+
"process.env.NODE_ENV": JSON.stringify(mode),
|
|
32
|
+
},
|
|
33
|
+
ssr: {
|
|
34
|
+
target: "webworker",
|
|
35
|
+
},
|
|
36
|
+
environments: {
|
|
37
|
+
client: {
|
|
38
|
+
consumer: "client",
|
|
39
|
+
build: {
|
|
40
|
+
outDir: resolve(projectRootDir, "dist", "client"),
|
|
41
|
+
manifest: true,
|
|
42
|
+
rollupOptions: {
|
|
43
|
+
input: clientEntryPathnames,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
define: {
|
|
47
|
+
"import.meta.env.RWSDK_ENV": JSON.stringify("client"),
|
|
48
|
+
},
|
|
49
|
+
optimizeDeps: {
|
|
50
|
+
noDiscovery: false,
|
|
51
|
+
include: ["rwsdk/client"],
|
|
52
|
+
esbuildOptions: {
|
|
53
|
+
jsx: "automatic",
|
|
54
|
+
jsxImportSource: "react",
|
|
55
|
+
plugins: [],
|
|
56
|
+
define: {
|
|
57
|
+
"process.env.NODE_ENV": JSON.stringify(mode),
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
resolve: {
|
|
62
|
+
conditions: ["browser", "module"],
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
ssr: {
|
|
66
|
+
resolve: {
|
|
67
|
+
conditions: ["workerd", "module", "browser"],
|
|
68
|
+
noExternal: true,
|
|
69
|
+
},
|
|
70
|
+
define: {
|
|
71
|
+
"import.meta.env.RWSDK_ENV": JSON.stringify("ssr"),
|
|
72
|
+
},
|
|
73
|
+
optimizeDeps: {
|
|
74
|
+
noDiscovery: false,
|
|
75
|
+
entries: [workerEntryPathname],
|
|
76
|
+
exclude: externalModules,
|
|
77
|
+
include: ["rwsdk/__ssr", "rwsdk/__ssr_bridge"],
|
|
78
|
+
esbuildOptions: {
|
|
79
|
+
jsx: "automatic",
|
|
80
|
+
jsxImportSource: "react",
|
|
81
|
+
plugins: [],
|
|
82
|
+
define: {
|
|
83
|
+
"process.env.NODE_ENV": JSON.stringify(mode),
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
build: {
|
|
88
|
+
lib: {
|
|
89
|
+
entry: {
|
|
90
|
+
[path.basename(SSR_BRIDGE_PATH, ".js")]: enhancedResolve.sync(projectRootDir, "rwsdk/__ssr_bridge"),
|
|
91
|
+
},
|
|
92
|
+
formats: ["es"],
|
|
93
|
+
fileName: () => path.basename(SSR_BRIDGE_PATH),
|
|
94
|
+
},
|
|
95
|
+
outDir: path.dirname(SSR_BRIDGE_PATH),
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
worker: {
|
|
99
|
+
resolve: {
|
|
100
|
+
conditions: [
|
|
101
|
+
"workerd",
|
|
102
|
+
"react-server",
|
|
103
|
+
"module",
|
|
104
|
+
// context(justinvdm, 11 Jun 2025): Some packages meant for cloudflare workers, yet
|
|
105
|
+
// their deps have only node import conditions, e.g. `agents` package (meant for CF),
|
|
106
|
+
// has `pkce-challenge` package as a dep, which has only node import conditions.
|
|
107
|
+
// https://github.com/crouchcd/pkce-challenge/blob/master/package.json#L17
|
|
108
|
+
//
|
|
109
|
+
// @cloudflare/vite-plugin should take care of any relevant polyfills for deps with
|
|
110
|
+
// node builtins imports that can be polyfilled though, so it is worth us including this condition here.
|
|
111
|
+
// However, it does mean we will try to run packages meant for node that cannot be run on cloudflare workers.
|
|
112
|
+
// That's the trade-off, but arguably worth it.
|
|
113
|
+
"node",
|
|
114
|
+
],
|
|
115
|
+
noExternal: true,
|
|
116
|
+
},
|
|
117
|
+
define: {
|
|
118
|
+
"import.meta.env.RWSDK_ENV": JSON.stringify("worker"),
|
|
119
|
+
},
|
|
120
|
+
optimizeDeps: {
|
|
121
|
+
noDiscovery: false,
|
|
122
|
+
include: ["rwsdk/worker"],
|
|
123
|
+
exclude: [],
|
|
124
|
+
entries: [workerEntryPathname],
|
|
125
|
+
esbuildOptions: {
|
|
126
|
+
jsx: "automatic",
|
|
127
|
+
jsxImportSource: "react",
|
|
128
|
+
define: {
|
|
129
|
+
"process.env.NODE_ENV": JSON.stringify(mode),
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
build: {
|
|
134
|
+
outDir: resolve(projectRootDir, "dist", "worker"),
|
|
135
|
+
emitAssets: true,
|
|
136
|
+
ssr: true,
|
|
137
|
+
rollupOptions: {
|
|
138
|
+
output: {
|
|
139
|
+
inlineDynamicImports: true,
|
|
140
|
+
},
|
|
141
|
+
input: {
|
|
142
|
+
worker: workerEntryPathname,
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
server: {
|
|
149
|
+
hmr: true,
|
|
150
|
+
},
|
|
151
|
+
builder: {
|
|
152
|
+
buildApp: async (builder) => {
|
|
153
|
+
// note(justinvdm, 27 May 2025): **Ordering is important**:
|
|
154
|
+
// * When building, client needs to be build first, so that we have a
|
|
155
|
+
// manifest file to map to when looking at asset references in JSX
|
|
156
|
+
// (e.g. Document.tsx)
|
|
157
|
+
// * When bundling, the RSC build imports the SSR build - this way
|
|
158
|
+
// they each can have their own environments (e.g. with their own
|
|
159
|
+
// import conditions), while still having all worker-run code go
|
|
160
|
+
// through the processing done by `@cloudflare/vite-plugin`
|
|
161
|
+
await builder.build(builder.environments["client"]);
|
|
162
|
+
await builder.build(builder.environments["ssr"]);
|
|
163
|
+
await builder.build(builder.environments["worker"]);
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
return baseConfig;
|
|
168
|
+
},
|
|
169
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
interface DirectiveLookupConfig {
|
|
3
|
+
kind: "client" | "server";
|
|
4
|
+
directive: "use client" | "use server";
|
|
5
|
+
virtualModuleName: string;
|
|
6
|
+
exportName: string;
|
|
7
|
+
pluginName: string;
|
|
8
|
+
optimizeForEnvironments?: string[];
|
|
9
|
+
}
|
|
10
|
+
export declare const findFilesContainingDirective: ({ projectRootDir, files, directive, debugNamespace, }: {
|
|
11
|
+
projectRootDir: string;
|
|
12
|
+
files: Set<string>;
|
|
13
|
+
directive: string;
|
|
14
|
+
debugNamespace: string;
|
|
15
|
+
}) => Promise<void>;
|
|
16
|
+
export declare const createDirectiveLookupPlugin: ({ projectRootDir, files, config, }: {
|
|
17
|
+
projectRootDir: string;
|
|
18
|
+
files: Set<string>;
|
|
19
|
+
config: DirectiveLookupConfig;
|
|
20
|
+
}) => Promise<Plugin>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import MagicString from "magic-string";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { readFile } from "fs/promises";
|
|
4
|
+
import debug from "debug";
|
|
5
|
+
import { normalizeModulePath } from "./normalizeModulePath.mjs";
|
|
6
|
+
import { pathExists } from "fs-extra";
|
|
7
|
+
import { stat } from "fs/promises";
|
|
8
|
+
import { getSrcPaths } from "../lib/getSrcPaths.js";
|
|
9
|
+
import { hasDirective } from "./hasDirective.mjs";
|
|
10
|
+
export const findFilesContainingDirective = async ({ projectRootDir, files, directive, debugNamespace, }) => {
|
|
11
|
+
const log = debug(debugNamespace);
|
|
12
|
+
log("Starting search for '%s' files in projectRootDir=%s", directive, projectRootDir);
|
|
13
|
+
const filesToScan = await getSrcPaths(projectRootDir);
|
|
14
|
+
log("Found %d files to scan for '%s' directive", filesToScan.length, directive);
|
|
15
|
+
for (const file of filesToScan) {
|
|
16
|
+
try {
|
|
17
|
+
const stats = await stat(file);
|
|
18
|
+
if (!stats.isFile()) {
|
|
19
|
+
process.env.VERBOSE && log("Skipping %s (not a file)", file);
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
process.env.VERBOSE && log("Scanning file: %s", file);
|
|
23
|
+
const content = await readFile(file, "utf-8");
|
|
24
|
+
if (hasDirective(content, directive)) {
|
|
25
|
+
const normalizedPath = normalizeModulePath(projectRootDir, file);
|
|
26
|
+
log("Found '%s' directive in file: %s -> %s", directive, file, normalizedPath);
|
|
27
|
+
files.add(normalizedPath);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
console.error(`Error reading file ${file}:`, error);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
log("Completed scan. Found %d %s files total", files.size, directive);
|
|
35
|
+
process.env.VERBOSE &&
|
|
36
|
+
log("Found files for %s: %j", directive, Array.from(files));
|
|
37
|
+
};
|
|
38
|
+
const resolveOptimizedDep = async (projectRootDir, id, environment, debugNamespace) => {
|
|
39
|
+
const log = debug(debugNamespace);
|
|
40
|
+
try {
|
|
41
|
+
const depsDir = environment === "client" ? "deps" : `deps_${environment}`;
|
|
42
|
+
const nodeModulesDepsDirPath = path.join("node_modules", ".vite", depsDir);
|
|
43
|
+
const depsDirPath = path.join(projectRootDir, nodeModulesDepsDirPath);
|
|
44
|
+
const manifestPath = path.join(depsDirPath, "_metadata.json");
|
|
45
|
+
log("Checking for manifest at: %s", manifestPath);
|
|
46
|
+
const manifestExists = await pathExists(manifestPath);
|
|
47
|
+
if (!manifestExists) {
|
|
48
|
+
log("Manifest not found at %s", manifestPath);
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
const manifestContent = await readFile(manifestPath, "utf-8");
|
|
52
|
+
const manifest = JSON.parse(manifestContent);
|
|
53
|
+
if (manifest.optimized && manifest.optimized[id]) {
|
|
54
|
+
const optimizedFile = manifest.optimized[id].file;
|
|
55
|
+
const optimizedPath = path.join("/", nodeModulesDepsDirPath, optimizedFile);
|
|
56
|
+
log("Found optimized dependency: filePath=%s, optimizedPath=%s", id, optimizedPath);
|
|
57
|
+
return optimizedPath;
|
|
58
|
+
}
|
|
59
|
+
process.env.VERBOSE &&
|
|
60
|
+
log("File not found in optimized dependencies: id=%s", id);
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
process.env.VERBOSE &&
|
|
65
|
+
log("Error resolving optimized dependency for id=%s: %s", id, error);
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const addOptimizedDepsEntries = async ({ projectRootDir, directive, environment, debugNamespace, files, }) => {
|
|
70
|
+
const log = debug(debugNamespace);
|
|
71
|
+
try {
|
|
72
|
+
const depsDir = environment === "client" ? "deps" : `deps_${environment}`;
|
|
73
|
+
const depsDirPath = path.join(projectRootDir, "node_modules", ".vite", depsDir);
|
|
74
|
+
const manifestPath = path.join(depsDirPath, "_metadata.json");
|
|
75
|
+
process.env.VERBOSE && log("Checking for manifest at: %s", manifestPath);
|
|
76
|
+
const manifestExists = await pathExists(manifestPath);
|
|
77
|
+
if (!manifestExists) {
|
|
78
|
+
process.env.VERBOSE && log("Manifest not found at %s", manifestPath);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const manifestContent = await readFile(manifestPath, "utf-8");
|
|
82
|
+
const manifest = JSON.parse(manifestContent);
|
|
83
|
+
for (const entryId of Object.keys(manifest.optimized)) {
|
|
84
|
+
if (entryId.startsWith("/node_modules/")) {
|
|
85
|
+
const srcPath = manifest.optimized[entryId].src;
|
|
86
|
+
const resolvedSrcPath = path.resolve(projectRootDir, "node_modules", ".vite", "deps", srcPath);
|
|
87
|
+
let contents;
|
|
88
|
+
try {
|
|
89
|
+
contents = await readFile(resolvedSrcPath, "utf-8");
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
process.env.VERBOSE &&
|
|
93
|
+
log("Error reading file %s: %s", resolvedSrcPath, error);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (hasDirective(contents, directive)) {
|
|
97
|
+
log("Adding optimized entry to files: %s", entryId);
|
|
98
|
+
files.add(entryId);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
log("Skipping optimized entry %s because it does not contain the '%s' directive", entryId, directive);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
process.env.VERBOSE &&
|
|
108
|
+
log("Error adding optimized deps entries: %s", error);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
export const createDirectiveLookupPlugin = async ({ projectRootDir, files, config, }) => {
|
|
112
|
+
const debugNamespace = `rwsdk:vite:${config.pluginName}`;
|
|
113
|
+
const log = debug(debugNamespace);
|
|
114
|
+
let isDev = false;
|
|
115
|
+
log("Initializing %s plugin with projectRootDir=%s", config.pluginName, projectRootDir);
|
|
116
|
+
await findFilesContainingDirective({
|
|
117
|
+
projectRootDir,
|
|
118
|
+
files,
|
|
119
|
+
directive: config.directive,
|
|
120
|
+
debugNamespace,
|
|
121
|
+
});
|
|
122
|
+
let devServer;
|
|
123
|
+
return {
|
|
124
|
+
name: `rwsdk:${config.pluginName}`,
|
|
125
|
+
config(_, { command, isPreview }) {
|
|
126
|
+
isDev = !isPreview && command === "serve";
|
|
127
|
+
log("Development mode: %s", isDev);
|
|
128
|
+
},
|
|
129
|
+
configureServer(server) {
|
|
130
|
+
devServer = server;
|
|
131
|
+
},
|
|
132
|
+
async configEnvironment(env, viteConfig) {
|
|
133
|
+
log("Configuring environment: env=%s", env);
|
|
134
|
+
// Add optimized deps entries that match our pattern
|
|
135
|
+
await addOptimizedDepsEntries({
|
|
136
|
+
projectRootDir,
|
|
137
|
+
files,
|
|
138
|
+
directive: config.directive,
|
|
139
|
+
environment: env,
|
|
140
|
+
debugNamespace,
|
|
141
|
+
});
|
|
142
|
+
viteConfig.optimizeDeps ??= {};
|
|
143
|
+
viteConfig.optimizeDeps.esbuildOptions ??= {};
|
|
144
|
+
viteConfig.optimizeDeps.esbuildOptions.plugins ??= [];
|
|
145
|
+
viteConfig.optimizeDeps.esbuildOptions.plugins.push({
|
|
146
|
+
name: `rwsdk:${config.pluginName}`,
|
|
147
|
+
setup(build) {
|
|
148
|
+
log("Setting up esbuild plugin for %s", config.virtualModuleName);
|
|
149
|
+
// Handle both direct virtual module name and /@id/ prefixed version
|
|
150
|
+
const escapedVirtualModuleName = config.virtualModuleName.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
151
|
+
const escapedPrefixedModuleName = `/@id/${config.virtualModuleName}`.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
152
|
+
build.onResolve({
|
|
153
|
+
filter: new RegExp(`^(${escapedVirtualModuleName}|${escapedPrefixedModuleName})\.js$`),
|
|
154
|
+
}, () => {
|
|
155
|
+
process.env.VERBOSE &&
|
|
156
|
+
log("Esbuild onResolve: marking %s as external", config.virtualModuleName);
|
|
157
|
+
return {
|
|
158
|
+
path: `${config.virtualModuleName}.js`,
|
|
159
|
+
external: true,
|
|
160
|
+
};
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
const shouldOptimizeForEnv = !config.optimizeForEnvironments ||
|
|
165
|
+
config.optimizeForEnvironments.includes(env);
|
|
166
|
+
if (shouldOptimizeForEnv) {
|
|
167
|
+
log("Applying optimizeDeps and aliasing for environment: %s", env);
|
|
168
|
+
viteConfig.optimizeDeps.include ??= [];
|
|
169
|
+
for (const file of files) {
|
|
170
|
+
const actualFilePath = path.join(projectRootDir, file);
|
|
171
|
+
process.env.VERBOSE &&
|
|
172
|
+
log("Adding to optimizeDeps.entries: %s", actualFilePath);
|
|
173
|
+
const entries = Array.isArray(viteConfig.optimizeDeps.entries)
|
|
174
|
+
? viteConfig.optimizeDeps.entries
|
|
175
|
+
: [].concat(viteConfig.optimizeDeps.entries ?? []);
|
|
176
|
+
viteConfig.optimizeDeps.entries = entries;
|
|
177
|
+
entries.push(actualFilePath);
|
|
178
|
+
}
|
|
179
|
+
log("Environment configuration complete for env=%s", env);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
log("Skipping optimizeDeps and aliasing for environment: %s", env);
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
resolveId(source) {
|
|
186
|
+
process.env.VERBOSE && log("Resolving id=%s", source);
|
|
187
|
+
if (source === `${config.virtualModuleName}.js`) {
|
|
188
|
+
log("Resolving %s module", config.virtualModuleName);
|
|
189
|
+
// context(justinvdm, 16 Jun 2025): Include .js extension
|
|
190
|
+
// so it goes through vite processing chain
|
|
191
|
+
return source;
|
|
192
|
+
}
|
|
193
|
+
process.env.VERBOSE && log("No resolution for id=%s", source);
|
|
194
|
+
},
|
|
195
|
+
async load(id) {
|
|
196
|
+
process.env.VERBOSE && log("Loading id=%s", id);
|
|
197
|
+
if (id === config.virtualModuleName + ".js") {
|
|
198
|
+
log("Loading %s module with %d files", config.virtualModuleName, files.size);
|
|
199
|
+
const environment = this.environment?.name || "client";
|
|
200
|
+
log("Current environment: %s, isDev: %s", environment, isDev);
|
|
201
|
+
const optimizedDeps = {};
|
|
202
|
+
if (isDev && devServer) {
|
|
203
|
+
for (const file of files) {
|
|
204
|
+
const resolvedPath = await resolveOptimizedDep(projectRootDir, file, environment, debugNamespace);
|
|
205
|
+
if (resolvedPath) {
|
|
206
|
+
optimizedDeps[file] = resolvedPath;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const s = new MagicString(`
|
|
211
|
+
export const ${config.exportName} = {
|
|
212
|
+
${Array.from(files)
|
|
213
|
+
.map((file) => `
|
|
214
|
+
"${file}": () => import("${optimizedDeps[file] ?? file}"),
|
|
215
|
+
`)
|
|
216
|
+
.join("")}
|
|
217
|
+
};
|
|
218
|
+
`);
|
|
219
|
+
const code = s.toString();
|
|
220
|
+
const map = s.generateMap();
|
|
221
|
+
log("Generated virtual module code length: %d", code.length);
|
|
222
|
+
process.env.VERBOSE && log("Generated virtual module code: %s", code);
|
|
223
|
+
return {
|
|
224
|
+
code,
|
|
225
|
+
map,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
process.env.VERBOSE && log("No load handling for id=%s", id);
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import debug from "debug";
|
|
2
|
+
const log = debug("rwsdk:vite:dev-server-timing-plugin");
|
|
3
|
+
export const devServerTimingPlugin = () => {
|
|
4
|
+
const startTime = Date.now();
|
|
5
|
+
let hasLoggedFirstResponse = false;
|
|
6
|
+
return {
|
|
7
|
+
name: "rwsdk:dev-server-timing",
|
|
8
|
+
configureServer(server) {
|
|
9
|
+
server.middlewares.use((_req, res, next) => {
|
|
10
|
+
if (!hasLoggedFirstResponse) {
|
|
11
|
+
res.on("finish", () => {
|
|
12
|
+
if (!hasLoggedFirstResponse) {
|
|
13
|
+
hasLoggedFirstResponse = true;
|
|
14
|
+
const endTime = Date.now();
|
|
15
|
+
const duration = endTime - startTime;
|
|
16
|
+
log(`🚀 Dev server first response completed in ${duration}ms`);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
next();
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import debug from "debug";
|
|
4
|
+
import { transformClientComponents } from "./transformClientComponents.mjs";
|
|
5
|
+
import { transformServerFunctions } from "./transformServerFunctions.mjs";
|
|
6
|
+
import { normalizeModulePath } from "./normalizeModulePath.mjs";
|
|
7
|
+
const log = debug("rwsdk:vite:rsc-directives-plugin");
|
|
8
|
+
const getLoader = (filePath) => {
|
|
9
|
+
const ext = path.extname(filePath).slice(1);
|
|
10
|
+
switch (ext) {
|
|
11
|
+
case "mjs":
|
|
12
|
+
case "cjs":
|
|
13
|
+
return "js";
|
|
14
|
+
case "mts":
|
|
15
|
+
case "cts":
|
|
16
|
+
return "ts";
|
|
17
|
+
case "jsx":
|
|
18
|
+
return "jsx";
|
|
19
|
+
case "tsx":
|
|
20
|
+
return "tsx";
|
|
21
|
+
case "ts":
|
|
22
|
+
return "ts";
|
|
23
|
+
case "js":
|
|
24
|
+
default:
|
|
25
|
+
return "js";
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
export const directivesPlugin = ({ projectRootDir, clientFiles, serverFiles, }) => {
|
|
29
|
+
let devServer;
|
|
30
|
+
let isAfterFirstResponse = false;
|
|
31
|
+
const addModule = (kind, environment, id) => {
|
|
32
|
+
const files = kind === "client" ? clientFiles : serverFiles;
|
|
33
|
+
const rawId = id.split("?")[0];
|
|
34
|
+
const resolvedId = rawId;
|
|
35
|
+
const relativePath = rawId.slice("/".length);
|
|
36
|
+
const fullPath = path.resolve(projectRootDir, relativePath);
|
|
37
|
+
const isNodeModule = id.includes("node_modules");
|
|
38
|
+
const hadFile = files.has(id);
|
|
39
|
+
log("Adding %s module to %s and invalidating cache: id=%s", kind, files, resolvedId);
|
|
40
|
+
files.add(resolvedId);
|
|
41
|
+
if (devServer && isNodeModule) {
|
|
42
|
+
const lookupModule = kind === "client"
|
|
43
|
+
? "virtual:use-client-lookup"
|
|
44
|
+
: "virtual:use-server-lookup";
|
|
45
|
+
log("Registering missing import for %s module resolvedId=%s in environment %s, fullPath=%s", kind, resolvedId, environment, fullPath);
|
|
46
|
+
devServer.environments[environment].depsOptimizer?.registerMissingImport(resolvedId, fullPath);
|
|
47
|
+
if (isAfterFirstResponse && !hadFile) {
|
|
48
|
+
log("Invalidating cache for lookup module %s after adding module id=%s", lookupModule, id);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const addClientModule = (environment, id) => {
|
|
53
|
+
addModule("client", environment, id);
|
|
54
|
+
};
|
|
55
|
+
const addServerModule = (environment, id) => {
|
|
56
|
+
addModule("server", environment, id);
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
name: "rwsdk:rsc-directives",
|
|
60
|
+
configureServer(server) {
|
|
61
|
+
devServer = server;
|
|
62
|
+
devServer.middlewares.use((_req, res, next) => {
|
|
63
|
+
// context(justinvdm, 15 Jun 2025): We want to watch for new client and server modules
|
|
64
|
+
// and invalidate their respective module lookups when this happens, but
|
|
65
|
+
// we only want to do this after the first render
|
|
66
|
+
if (!isAfterFirstResponse) {
|
|
67
|
+
res.on("finish", () => {
|
|
68
|
+
if (!isAfterFirstResponse) {
|
|
69
|
+
isAfterFirstResponse = true;
|
|
70
|
+
log("Dev server first response completed");
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
next();
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
async transform(code, id) {
|
|
78
|
+
process.env.VERBOSE &&
|
|
79
|
+
log("Transform called for id=%s, environment=%s", id, this.environment.name);
|
|
80
|
+
const normalizedId = normalizeModulePath(projectRootDir, id);
|
|
81
|
+
const clientResult = await transformClientComponents(code, normalizedId, {
|
|
82
|
+
environmentName: this.environment.name,
|
|
83
|
+
clientFiles,
|
|
84
|
+
addClientModule,
|
|
85
|
+
});
|
|
86
|
+
if (clientResult) {
|
|
87
|
+
log("Client component transformation successful for id=%s", id);
|
|
88
|
+
return {
|
|
89
|
+
code: clientResult.code,
|
|
90
|
+
map: clientResult.map,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const serverResult = transformServerFunctions(code, normalizedId, this.environment.name, serverFiles, addServerModule);
|
|
94
|
+
if (serverResult) {
|
|
95
|
+
log("Server function transformation successful for id=%s", id);
|
|
96
|
+
return {
|
|
97
|
+
code: serverResult.code,
|
|
98
|
+
map: serverResult.map,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
process.env.VERBOSE && log("No transformation applied for id=%s", id);
|
|
102
|
+
},
|
|
103
|
+
configEnvironment(env, config) {
|
|
104
|
+
log("Configuring environment: env=%s", env);
|
|
105
|
+
config.optimizeDeps ??= {};
|
|
106
|
+
config.optimizeDeps.esbuildOptions ??= {};
|
|
107
|
+
config.optimizeDeps.esbuildOptions.plugins ??= [];
|
|
108
|
+
config.optimizeDeps.esbuildOptions.plugins.push({
|
|
109
|
+
name: "rsc-directives-esbuild-transform",
|
|
110
|
+
setup(build) {
|
|
111
|
+
log("Setting up esbuild plugin for environment: %s", env);
|
|
112
|
+
build.onLoad({ filter: /\.(js|ts|jsx|tsx|mts|mjs|cjs)$/ }, async (args) => {
|
|
113
|
+
process.env.VERBOSE &&
|
|
114
|
+
log("Esbuild onLoad called for environment=%s, path=%s", env, args.path);
|
|
115
|
+
const normalizedPath = normalizeModulePath(projectRootDir, args.path);
|
|
116
|
+
// context(justinvdm,2025-06-15): If we're in app code,
|
|
117
|
+
// we will be doing the transform work in the vite plugin hooks,
|
|
118
|
+
// the only reason we're in esbuild land for app code is for
|
|
119
|
+
// dependency discovery, so we can skip transform work
|
|
120
|
+
// and use heuristics instead - see below inside if block
|
|
121
|
+
if (!args.path.includes("node_modules")) {
|
|
122
|
+
log("Esbuild onLoad found app code, path=%s", args.path);
|
|
123
|
+
if (clientFiles.has(normalizedPath)) {
|
|
124
|
+
// context(justinvdm,2025-06-15): If this is a client file:
|
|
125
|
+
// * for ssr and client envs we can skip so esbuild looks at the
|
|
126
|
+
// original source code to discovery dependencies
|
|
127
|
+
// * for worker env, the transform would have just created
|
|
128
|
+
// references and dropped all imports, so we can just return empty code
|
|
129
|
+
if (env === "client" || env === "ssr") {
|
|
130
|
+
log("Esbuild onLoad skipping client module in app code for client or ssr env, path=%s", args.path);
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
log("Esbuild onLoad returning empty code for server module in app code for worker env, path=%s to bypass esbuild dependency discovery", args.path);
|
|
135
|
+
return {
|
|
136
|
+
contents: "",
|
|
137
|
+
loader: "js",
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (serverFiles.has(normalizedPath)) {
|
|
142
|
+
// context(justinvdm,2025-06-15): If this is a server file:
|
|
143
|
+
// * for worker env, we can skip so esbuild looks at the
|
|
144
|
+
// original source code to discovery dependencies
|
|
145
|
+
// * for ssr and client envs, the transform would have just created
|
|
146
|
+
// references and dropped all imports, so we can just return empty code
|
|
147
|
+
if (env === "worker") {
|
|
148
|
+
log("Esbuild onLoad skipping server module in app code for worker env, path=%s", args.path);
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
else if (env === "ssr" || env === "client") {
|
|
152
|
+
log("Esbuild onLoad returning empty code for server module in app code for ssr or client env, path=%s", args.path);
|
|
153
|
+
return {
|
|
154
|
+
contents: "",
|
|
155
|
+
loader: "js",
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
let code;
|
|
161
|
+
try {
|
|
162
|
+
code = await fs.readFile(args.path, "utf-8");
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
process.env.VERBOSE &&
|
|
166
|
+
log("Failed to read file: %s, environment=%s", args.path, env);
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
const clientResult = await transformClientComponents(code, normalizedPath, {
|
|
170
|
+
environmentName: env,
|
|
171
|
+
clientFiles,
|
|
172
|
+
isEsbuild: true,
|
|
173
|
+
addClientModule,
|
|
174
|
+
});
|
|
175
|
+
if (clientResult) {
|
|
176
|
+
log("Esbuild client component transformation successful for environment=%s, path=%s", env, args.path);
|
|
177
|
+
process.env.VERBOSE &&
|
|
178
|
+
log("Esbuild client component transformation for environment=%s, path=%s, code: %j", env, args.path, clientResult.code);
|
|
179
|
+
return {
|
|
180
|
+
contents: clientResult.code,
|
|
181
|
+
loader: getLoader(args.path),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const serverResult = transformServerFunctions(code, normalizedPath, env, serverFiles, addServerModule);
|
|
185
|
+
if (serverResult) {
|
|
186
|
+
log("Esbuild server function transformation successful for environment=%s, path=%s", env, args.path);
|
|
187
|
+
return {
|
|
188
|
+
contents: serverResult.code,
|
|
189
|
+
loader: getLoader(args.path),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
process.env.VERBOSE &&
|
|
193
|
+
log("Esbuild no transformation applied for environment=%s, path=%s", env, args.path);
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
log("Environment configuration complete for env=%s", env);
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
};
|