rwsdk 0.1.16 → 0.1.17
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/compileTsModule.d.mts +1 -0
- package/dist/lib/compileTsModule.mjs +27 -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/types.mjs +1 -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/navigation.d.ts +1 -0
- package/dist/runtime/entries/navigation.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/injectRSCPayload.d.ts +3 -0
- package/dist/runtime/render/injectRSCPayload.js +79 -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/build-vendor-bundles.d.mts +1 -0
- package/dist/scripts/build-vendor-bundles.mjs +92 -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/aliasByEnvPlugin.d.mts +2 -0
- package/dist/vite/aliasByEnvPlugin.mjs +11 -0
- package/dist/vite/asyncSetupPlugin.d.mts +6 -0
- package/dist/vite/asyncSetupPlugin.mjs +23 -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/copyPrismaWasmPlugin.d.mts +4 -0
- package/dist/vite/copyPrismaWasmPlugin.mjs +32 -0
- package/dist/vite/createDirectiveLookupPlugin.d.mts +21 -0
- package/dist/vite/createDirectiveLookupPlugin.mjs +231 -0
- package/dist/vite/customReactBuildPlugin.d.mts +4 -0
- package/dist/vite/customReactBuildPlugin.mjs +61 -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/injectHmrPreambleJsxPlugin.d.mts +2 -0
- package/dist/vite/injectHmrPreambleJsxPlugin.mjs +22 -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/miniflarePlugin.d.mts +9 -0
- package/dist/vite/miniflarePlugin.mjs +135 -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/requestUtils.d.mts +6 -0
- package/dist/vite/requestUtils.mjs +35 -0
- package/dist/vite/setupEnvFiles.d.mts +4 -0
- package/dist/vite/setupEnvFiles.mjs +31 -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/useClientPlugin.d.mts +8 -0
- package/dist/vite/useClientPlugin.mjs +295 -0
- package/dist/vite/useClientPlugin.test.d.mts +1 -0
- package/dist/vite/useClientPlugin.test.mjs +1204 -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/dist/worker/__ssr_bridge.js +8947 -0
- package/dist/worker/__ssr_bridge.js.map +1 -0
- package/package.json +1 -1
- 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/vite/{isJsFile.d.ts → isJsFile.d.mts} +0 -0
- /package/dist/vite/{isJsFile.js → isJsFile.mjs} +0 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import colors from "picocolors";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import debug from "debug";
|
|
5
|
+
import { VIRTUAL_SSR_PREFIX } from "./ssrBridgePlugin.mjs";
|
|
6
|
+
import { normalizeModulePath } from "./normalizeModulePath.mjs";
|
|
7
|
+
import { hasDirective as sourceHasDirective } from "./hasDirective.mjs";
|
|
8
|
+
import { isJsFile } from "./isJsFile.mjs";
|
|
9
|
+
import { invalidateModule } from "./invalidateModule.mjs";
|
|
10
|
+
import { getShortName } from "../lib/getShortName.mjs";
|
|
11
|
+
const log = debug("rwsdk:vite:hmr-plugin");
|
|
12
|
+
const hasDirective = async (filepath, directive) => {
|
|
13
|
+
if (!isJsFile(filepath)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const content = await readFile(filepath, "utf-8");
|
|
17
|
+
return sourceHasDirective(content, directive);
|
|
18
|
+
};
|
|
19
|
+
const hasEntryAsAncestor = (module, entryFile, seen = new Set()) => {
|
|
20
|
+
// Prevent infinite recursion
|
|
21
|
+
if (seen.has(module)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
seen.add(module);
|
|
25
|
+
// Check direct importers
|
|
26
|
+
for (const importer of module.importers) {
|
|
27
|
+
if (importer.file === entryFile)
|
|
28
|
+
return true;
|
|
29
|
+
// Recursively check importers
|
|
30
|
+
if (hasEntryAsAncestor(importer, entryFile, seen))
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
};
|
|
35
|
+
const isInUseClientGraph = ({ file, clientFiles, server, }) => {
|
|
36
|
+
const id = normalizeModulePath(server.config.root, file);
|
|
37
|
+
if (clientFiles.has(id)) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
const modules = server.environments.client.moduleGraph.getModulesByFile(file);
|
|
41
|
+
if (!modules) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
for (const m of modules) {
|
|
45
|
+
for (const importer of m.importers) {
|
|
46
|
+
if (importer.file &&
|
|
47
|
+
isInUseClientGraph({ file: importer.file, clientFiles, server })) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
};
|
|
54
|
+
export const miniflareHMRPlugin = (givenOptions) => [
|
|
55
|
+
{
|
|
56
|
+
name: "rwsdk:miniflare-hmr",
|
|
57
|
+
async hotUpdate(ctx) {
|
|
58
|
+
const { clientFiles, serverFiles, viteEnvironment: { name: environment }, workerEntryPathname: entry, } = givenOptions;
|
|
59
|
+
if (process.env.VERBOSE) {
|
|
60
|
+
log(`Hot update: (env=${this.environment.name}) ${ctx.file}\nModule graph:\n\n${dumpFullModuleGraph(ctx.server, this.environment.name)}`);
|
|
61
|
+
}
|
|
62
|
+
if (!isJsFile(ctx.file) && !ctx.file.endsWith(".css")) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (this.environment.name === "ssr") {
|
|
66
|
+
log("SSR update, invalidating recursively", ctx.file);
|
|
67
|
+
const isUseClientUpdate = isInUseClientGraph({
|
|
68
|
+
file: ctx.file,
|
|
69
|
+
clientFiles,
|
|
70
|
+
server: ctx.server,
|
|
71
|
+
});
|
|
72
|
+
if (!isUseClientUpdate) {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
invalidateModule(ctx.server, "ssr", ctx.file);
|
|
76
|
+
invalidateModule(ctx.server, environment, VIRTUAL_SSR_PREFIX +
|
|
77
|
+
normalizeModulePath(givenOptions.rootDir, ctx.file));
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
if (!["client", environment].includes(this.environment.name)) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
const hasClientDirective = await hasDirective(ctx.file, "use client");
|
|
84
|
+
const hasServerDirective = !hasClientDirective && (await hasDirective(ctx.file, "use server"));
|
|
85
|
+
let clientDirectiveChanged = false;
|
|
86
|
+
let serverDirectiveChanged = false;
|
|
87
|
+
if (!clientFiles.has(ctx.file) && hasClientDirective) {
|
|
88
|
+
clientFiles.add(ctx.file);
|
|
89
|
+
clientDirectiveChanged = true;
|
|
90
|
+
}
|
|
91
|
+
else if (clientFiles.has(ctx.file) && !hasClientDirective) {
|
|
92
|
+
clientFiles.delete(ctx.file);
|
|
93
|
+
clientDirectiveChanged = true;
|
|
94
|
+
}
|
|
95
|
+
if (!serverFiles.has(ctx.file) && hasServerDirective) {
|
|
96
|
+
serverFiles.add(ctx.file);
|
|
97
|
+
serverDirectiveChanged = true;
|
|
98
|
+
}
|
|
99
|
+
else if (serverFiles.has(ctx.file) && !hasServerDirective) {
|
|
100
|
+
serverFiles.delete(ctx.file);
|
|
101
|
+
serverDirectiveChanged = true;
|
|
102
|
+
}
|
|
103
|
+
if (clientDirectiveChanged) {
|
|
104
|
+
["client", "ssr", environment].forEach((environment) => {
|
|
105
|
+
invalidateModule(ctx.server, environment, "virtual:use-client-lookup.js");
|
|
106
|
+
});
|
|
107
|
+
invalidateModule(ctx.server, environment, VIRTUAL_SSR_PREFIX + "/@id/virtual:use-client-lookup.js");
|
|
108
|
+
}
|
|
109
|
+
if (serverDirectiveChanged) {
|
|
110
|
+
["client", "ssr", environment].forEach((environment) => {
|
|
111
|
+
invalidateModule(ctx.server, environment, "virtual:use-server-lookup.js");
|
|
112
|
+
});
|
|
113
|
+
invalidateModule(ctx.server, environment, VIRTUAL_SSR_PREFIX + "/@id/virtual:use-server-lookup.js");
|
|
114
|
+
}
|
|
115
|
+
// todo(justinvdm, 12 Dec 2024): Skip client references
|
|
116
|
+
const modules = Array.from(ctx.server.environments[environment].moduleGraph.getModulesByFile(ctx.file) ?? []);
|
|
117
|
+
const isWorkerUpdate = Boolean(modules);
|
|
118
|
+
// The worker needs an update, but this is the client environment
|
|
119
|
+
// => Notify for HMR update of any css files imported by in worker, that are also in the client module graph
|
|
120
|
+
// Why: There may have been changes to css classes referenced, which might css modules to change
|
|
121
|
+
if (this.environment.name === "client") {
|
|
122
|
+
if (isWorkerUpdate) {
|
|
123
|
+
for (const [_, module] of ctx.server.environments[environment]
|
|
124
|
+
.moduleGraph.idToModuleMap) {
|
|
125
|
+
// todo(justinvdm, 13 Dec 2024): We check+update _all_ css files in worker module graph,
|
|
126
|
+
// but it could just be a subset of css files that are actually affected, depending
|
|
127
|
+
// on the importers and imports of the changed file. We should be smarter about this.
|
|
128
|
+
if (module.file && module.file.endsWith(".css")) {
|
|
129
|
+
const clientModules = ctx.server.environments.client.moduleGraph.getModulesByFile(module.file);
|
|
130
|
+
for (const clientModule of clientModules ?? []) {
|
|
131
|
+
invalidateModule(ctx.server, "client", clientModule);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const isUseClientUpdate = isInUseClientGraph({
|
|
137
|
+
file: ctx.file,
|
|
138
|
+
clientFiles,
|
|
139
|
+
server: ctx.server,
|
|
140
|
+
});
|
|
141
|
+
if (!isUseClientUpdate && !ctx.file.endsWith(".css")) {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
return ctx.modules;
|
|
145
|
+
}
|
|
146
|
+
// The worker needs an update, and the hot check is for the worker environment
|
|
147
|
+
// => Notify for custom RSC-based HMR update, then short circuit HMR
|
|
148
|
+
if (isWorkerUpdate && this.environment.name === environment) {
|
|
149
|
+
const shortName = getShortName(ctx.file, ctx.server.config.root);
|
|
150
|
+
this.environment.logger.info(`${colors.green(`worker update`)} ${colors.dim(shortName)}`, {
|
|
151
|
+
clear: true,
|
|
152
|
+
timestamp: true,
|
|
153
|
+
});
|
|
154
|
+
const m = ctx.server.environments.client.moduleGraph
|
|
155
|
+
.getModulesByFile(resolve(givenOptions.rootDir, "src", "app", "style.css"))
|
|
156
|
+
?.values()
|
|
157
|
+
.next().value;
|
|
158
|
+
if (m) {
|
|
159
|
+
invalidateModule(ctx.server, environment, m);
|
|
160
|
+
}
|
|
161
|
+
const virtualSSRModule = ctx.server.environments[environment].moduleGraph.idToModuleMap.get(VIRTUAL_SSR_PREFIX +
|
|
162
|
+
normalizeModulePath(givenOptions.rootDir, ctx.file));
|
|
163
|
+
if (virtualSSRModule) {
|
|
164
|
+
invalidateModule(ctx.server, environment, virtualSSRModule);
|
|
165
|
+
}
|
|
166
|
+
ctx.server.environments.client.hot.send({
|
|
167
|
+
type: "custom",
|
|
168
|
+
event: "rsc:update",
|
|
169
|
+
data: {
|
|
170
|
+
file: ctx.file,
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
return [];
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
];
|
|
178
|
+
function dumpFullModuleGraph(server, environment, { includeDisconnected = true } = {}) {
|
|
179
|
+
const moduleGraph = server.environments[environment].moduleGraph;
|
|
180
|
+
const seen = new Set();
|
|
181
|
+
const output = [];
|
|
182
|
+
function walk(node, depth = 0) {
|
|
183
|
+
const id = node.id || node.url;
|
|
184
|
+
if (!id || seen.has(id))
|
|
185
|
+
return;
|
|
186
|
+
seen.add(id);
|
|
187
|
+
const pad = " ".repeat(depth);
|
|
188
|
+
const suffix = node.id?.startsWith("virtual:") ? " [virtual]" : "";
|
|
189
|
+
output.push(`${pad}- ${id}${suffix}`);
|
|
190
|
+
for (const dep of node.importedModules) {
|
|
191
|
+
walk(dep, depth + 1);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// Start with all modules with no importers (roots)
|
|
195
|
+
const roots = Array.from(moduleGraph.urlToModuleMap.values()).filter((mod) => mod.importers.size === 0);
|
|
196
|
+
for (const root of roots) {
|
|
197
|
+
walk(root);
|
|
198
|
+
}
|
|
199
|
+
// If requested, show disconnected modules too
|
|
200
|
+
if (includeDisconnected) {
|
|
201
|
+
for (const mod of moduleGraph.urlToModuleMap.values()) {
|
|
202
|
+
const id = mod.id || mod.url;
|
|
203
|
+
if (!seen.has(id)) {
|
|
204
|
+
output.push(`- ${id} [disconnected]`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return output.join("\n");
|
|
209
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
3
|
+
type BasePluginOptions = Parameters<typeof cloudflare>[0];
|
|
4
|
+
type MiniflarePluginOptions = BasePluginOptions & {};
|
|
5
|
+
export declare const miniflarePlugin: (givenOptions: MiniflarePluginOptions & {
|
|
6
|
+
rootDir: string;
|
|
7
|
+
workerEntryPathname: string;
|
|
8
|
+
}) => (Plugin | Plugin[])[];
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import colors from "picocolors";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { getShortName } from "../lib/getShortName.mjs";
|
|
6
|
+
import { pathExists } from "fs-extra";
|
|
7
|
+
const hasEntryAsAncestor = (module, entryFile, seen = new Set()) => {
|
|
8
|
+
// Prevent infinite recursion
|
|
9
|
+
if (seen.has(module))
|
|
10
|
+
return false;
|
|
11
|
+
seen.add(module);
|
|
12
|
+
// Check direct importers
|
|
13
|
+
for (const importer of module.importers) {
|
|
14
|
+
if (importer.file === entryFile)
|
|
15
|
+
return true;
|
|
16
|
+
// Recursively check importers
|
|
17
|
+
if (hasEntryAsAncestor(importer, entryFile, seen))
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
};
|
|
22
|
+
// Cache for "use client" status results
|
|
23
|
+
const useClientCache = new Map();
|
|
24
|
+
// Function to invalidate cache for a file
|
|
25
|
+
const invalidateUseClientCache = (file) => {
|
|
26
|
+
useClientCache.delete(file);
|
|
27
|
+
};
|
|
28
|
+
const isUseClientModule = async (ctx, file, seen = new Set()) => {
|
|
29
|
+
// Prevent infinite recursion
|
|
30
|
+
if (seen.has(file))
|
|
31
|
+
return false;
|
|
32
|
+
seen.add(file);
|
|
33
|
+
try {
|
|
34
|
+
// Check cache first
|
|
35
|
+
if (useClientCache.has(file)) {
|
|
36
|
+
return useClientCache.get(file);
|
|
37
|
+
}
|
|
38
|
+
// Read and check the file
|
|
39
|
+
const content = (await pathExists(file))
|
|
40
|
+
? await readFile(file, "utf-8")
|
|
41
|
+
: "";
|
|
42
|
+
const hasUseClient = content.includes("'use client'") || content.includes('"use client"');
|
|
43
|
+
if (hasUseClient) {
|
|
44
|
+
useClientCache.set(file, true);
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
// Get the module from the module graph to find importers
|
|
48
|
+
const module = ctx.server.moduleGraph.getModuleById(file);
|
|
49
|
+
if (!module) {
|
|
50
|
+
useClientCache.set(file, false);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
// Check all importers recursively
|
|
54
|
+
for (const importer of module.importers) {
|
|
55
|
+
if (await isUseClientModule(ctx, importer.url, seen)) {
|
|
56
|
+
useClientCache.set(file, true);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
useClientCache.set(file, false);
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
useClientCache.set(file, false);
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
export const miniflarePlugin = (givenOptions) => [
|
|
69
|
+
cloudflare(givenOptions),
|
|
70
|
+
{
|
|
71
|
+
name: "rwsdk:miniflare-hmr",
|
|
72
|
+
async hotUpdate(ctx) {
|
|
73
|
+
const environment = givenOptions.viteEnvironment?.name ?? "worker";
|
|
74
|
+
const entry = givenOptions.workerEntryPathname;
|
|
75
|
+
if (!["client", environment].includes(this.environment.name)) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
// todo(justinvdm, 12 Dec 2024): Skip client references
|
|
79
|
+
const modules = Array.from(ctx.server.environments[environment].moduleGraph.getModulesByFile(ctx.file) ?? []);
|
|
80
|
+
const isWorkerUpdate = ctx.file === entry ||
|
|
81
|
+
modules.some((module) => hasEntryAsAncestor(module, entry));
|
|
82
|
+
// The worker doesnt need an update
|
|
83
|
+
// => Short circuit HMR
|
|
84
|
+
if (!isWorkerUpdate) {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
// The worker needs an update, but this is the client environment
|
|
88
|
+
// => Notify for HMR update of any css files imported by in worker, that are also in the client module graph
|
|
89
|
+
// Why: There may have been changes to css classes referenced, which might css modules to change
|
|
90
|
+
if (this.environment.name === "client") {
|
|
91
|
+
const cssModules = [];
|
|
92
|
+
for (const [_, module] of ctx.server.environments[environment]
|
|
93
|
+
.moduleGraph.idToModuleMap) {
|
|
94
|
+
// todo(justinvdm, 13 Dec 2024): We check+update _all_ css files in worker module graph,
|
|
95
|
+
// but it could just be a subset of css files that are actually affected, depending
|
|
96
|
+
// on the importers and imports of the changed file. We should be smarter about this.
|
|
97
|
+
if (module.file && module.file.endsWith(".css")) {
|
|
98
|
+
const clientModules = ctx.server.environments.client.moduleGraph.getModulesByFile(module.file);
|
|
99
|
+
if (clientModules) {
|
|
100
|
+
cssModules.push(...clientModules.values());
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
invalidateUseClientCache(ctx.file);
|
|
105
|
+
return (await isUseClientModule(ctx, ctx.file))
|
|
106
|
+
? [...ctx.modules, ...cssModules]
|
|
107
|
+
: cssModules;
|
|
108
|
+
}
|
|
109
|
+
// The worker needs an update, and the hot check is for the worker environment
|
|
110
|
+
// => Notify for custom RSC-based HMR update, then short circuit HMR
|
|
111
|
+
if (isWorkerUpdate && this.environment.name === environment) {
|
|
112
|
+
const shortName = getShortName(ctx.file, ctx.server.config.root);
|
|
113
|
+
this.environment.logger.info(`${colors.green(`worker update`)} ${colors.dim(shortName)}`, {
|
|
114
|
+
clear: true,
|
|
115
|
+
timestamp: true,
|
|
116
|
+
});
|
|
117
|
+
const m = ctx.server.environments.client.moduleGraph
|
|
118
|
+
.getModulesByFile(resolve(givenOptions.rootDir, "src", "app", "style.css"))
|
|
119
|
+
?.values()
|
|
120
|
+
.next().value;
|
|
121
|
+
if (m) {
|
|
122
|
+
ctx.server.environments.client.moduleGraph.invalidateModule(m, new Set(), ctx.timestamp, true);
|
|
123
|
+
}
|
|
124
|
+
ctx.server.environments.client.hot.send({
|
|
125
|
+
type: "custom",
|
|
126
|
+
event: "rsc:update",
|
|
127
|
+
data: {
|
|
128
|
+
file: ctx.file,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { $sh } from "../lib/$.mjs";
|
|
2
|
+
export const moveStaticAssetsPlugin = ({ rootDir, }) => ({
|
|
3
|
+
name: "rwsdk:move-static-assets",
|
|
4
|
+
apply: "build",
|
|
5
|
+
async closeBundle() {
|
|
6
|
+
if (this.environment.name === "worker") {
|
|
7
|
+
await $sh({
|
|
8
|
+
cwd: rootDir,
|
|
9
|
+
}) `mv dist/worker/assets/*.css dist/client/assets/ || true`;
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const normalizeModulePath: (projectRootDir: string, modulePath: string) => string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
export const normalizeModulePath = (projectRootDir, modulePath) => {
|
|
3
|
+
// /Users/path/to/project/src/foo/bar.ts -> /src/foo/bar.ts
|
|
4
|
+
if (modulePath.startsWith(projectRootDir)) {
|
|
5
|
+
return "/" + path.relative(projectRootDir, modulePath);
|
|
6
|
+
}
|
|
7
|
+
// /src/foo/bar.ts -> /src/foo/bar.ts
|
|
8
|
+
if (modulePath.startsWith("/")) {
|
|
9
|
+
return modulePath;
|
|
10
|
+
}
|
|
11
|
+
// src/foo/bar.ts -> /src/foo/bar.ts
|
|
12
|
+
return "/" + modulePath;
|
|
13
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { invalidateCacheIfPrismaClientChanged } from "./invalidateCacheIfPrismaClientChanged.mjs";
|
|
3
|
+
import { checkPrismaStatus } from "./checkIsUsingPrisma.mjs";
|
|
4
|
+
import { ensureAliasArray } from "./ensureAliasArray.mjs";
|
|
5
|
+
export const prismaPlugin = async ({ projectRootDir, }) => {
|
|
6
|
+
if (!checkPrismaStatus({ projectRootDir }).isUsingPrisma) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
// context(justinvdm, 10 Mar 2025): We need to use vite optimizeDeps for all deps to work with @cloudflare/vite-plugin.
|
|
10
|
+
// Thing is, @prisma/client has generated code. So users end up with a stale @prisma/client
|
|
11
|
+
// when they change their prisma schema and regenerate the client, until clearing out node_modules/.vite
|
|
12
|
+
// We can't exclude @prisma/client from optimizeDeps since we need it there for @cloudflare/vite-plugin to work.
|
|
13
|
+
// But we can manually invalidate the cache if the prisma schema changes.
|
|
14
|
+
await invalidateCacheIfPrismaClientChanged({
|
|
15
|
+
projectRootDir,
|
|
16
|
+
});
|
|
17
|
+
return {
|
|
18
|
+
name: "rwsdk:prisma",
|
|
19
|
+
configEnvironment(name, config) {
|
|
20
|
+
if (name !== "worker") {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const wasmPath = resolve(projectRootDir, "node_modules/.prisma/client/wasm.js");
|
|
24
|
+
config.optimizeDeps ??= {};
|
|
25
|
+
config.optimizeDeps.esbuildOptions ??= {};
|
|
26
|
+
config.optimizeDeps.esbuildOptions.plugins ??= [];
|
|
27
|
+
config.optimizeDeps.esbuildOptions.plugins.push({
|
|
28
|
+
name: "rwsdk:prisma",
|
|
29
|
+
setup(build) {
|
|
30
|
+
build.onResolve({ filter: /^.prisma\/client\/default/ }, async () => {
|
|
31
|
+
return {
|
|
32
|
+
path: wasmPath,
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
ensureAliasArray(config).push({
|
|
38
|
+
find: /^\.prisma\/client\/default/,
|
|
39
|
+
replacement: wasmPath,
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
import enhancedResolve from "enhanced-resolve";
|
|
3
|
+
export declare const ENV_REACT_IMPORTS: {
|
|
4
|
+
worker: string[];
|
|
5
|
+
ssr: string[];
|
|
6
|
+
client: string[];
|
|
7
|
+
};
|
|
8
|
+
export declare const ENV_RESOLVERS: {
|
|
9
|
+
ssr: enhancedResolve.ResolveFunction;
|
|
10
|
+
worker: enhancedResolve.ResolveFunction;
|
|
11
|
+
client: enhancedResolve.ResolveFunction;
|
|
12
|
+
};
|
|
13
|
+
export declare const ENV_IMPORT_MAPPINGS: {
|
|
14
|
+
[k: string]: Map<string, string>;
|
|
15
|
+
};
|
|
16
|
+
export declare const reactConditionsResolverPlugin: () => Plugin[];
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import debug from "debug";
|
|
2
|
+
import { ROOT_DIR } from "../lib/constants.mjs";
|
|
3
|
+
import enhancedResolve from "enhanced-resolve";
|
|
4
|
+
import { ensureAliasArray } from "./ensureAliasArray.mjs";
|
|
5
|
+
const log = debug("rwsdk:vite:react-conditions-resolver-plugin");
|
|
6
|
+
export const ENV_REACT_IMPORTS = {
|
|
7
|
+
worker: [
|
|
8
|
+
"react",
|
|
9
|
+
"react-dom",
|
|
10
|
+
"react/jsx-runtime",
|
|
11
|
+
"react/jsx-dev-runtime",
|
|
12
|
+
"react-server-dom-webpack/server.edge",
|
|
13
|
+
],
|
|
14
|
+
ssr: [
|
|
15
|
+
"react",
|
|
16
|
+
"react-dom",
|
|
17
|
+
"react/jsx-runtime",
|
|
18
|
+
"react/jsx-dev-runtime",
|
|
19
|
+
"react-dom/server.edge",
|
|
20
|
+
"react-dom/server",
|
|
21
|
+
"react-server-dom-webpack/client.edge",
|
|
22
|
+
],
|
|
23
|
+
client: [
|
|
24
|
+
"react",
|
|
25
|
+
"react-dom",
|
|
26
|
+
"react-dom/client",
|
|
27
|
+
"react/jsx-runtime",
|
|
28
|
+
"react/jsx-dev-runtime",
|
|
29
|
+
"react-server-dom-webpack/client.browser",
|
|
30
|
+
"react-server-dom-webpack/client.edge",
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
export const ENV_RESOLVERS = {
|
|
34
|
+
ssr: enhancedResolve.create.sync({
|
|
35
|
+
conditionNames: ["workerd", "worker", "edge", "default"],
|
|
36
|
+
}),
|
|
37
|
+
worker: enhancedResolve.create.sync({
|
|
38
|
+
conditionNames: ["react-server", "workerd", "worker", "edge", "default"],
|
|
39
|
+
}),
|
|
40
|
+
client: enhancedResolve.create.sync({
|
|
41
|
+
conditionNames: ["browser", "default"],
|
|
42
|
+
}),
|
|
43
|
+
};
|
|
44
|
+
export const ENV_IMPORT_MAPPINGS = Object.fromEntries(Object.keys(ENV_RESOLVERS).map((env) => [
|
|
45
|
+
env,
|
|
46
|
+
resolveEnvImportMappings(env),
|
|
47
|
+
]));
|
|
48
|
+
function resolveEnvImportMappings(env) {
|
|
49
|
+
process.env.VERBOSE &&
|
|
50
|
+
log("Resolving environment import mappings for env=%s", env);
|
|
51
|
+
const mappings = new Map();
|
|
52
|
+
const reactImports = ENV_REACT_IMPORTS[env];
|
|
53
|
+
for (const importRequest of reactImports) {
|
|
54
|
+
process.env.VERBOSE &&
|
|
55
|
+
log("Resolving import request=%s for env=%s", importRequest, env);
|
|
56
|
+
let resolved = false;
|
|
57
|
+
try {
|
|
58
|
+
resolved = ENV_RESOLVERS[env](ROOT_DIR, importRequest);
|
|
59
|
+
process.env.VERBOSE &&
|
|
60
|
+
log("Successfully resolved %s to %s for env=%s", importRequest, resolved, env);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
process.env.VERBOSE &&
|
|
64
|
+
log("Failed to resolve %s for env=%s", importRequest, env);
|
|
65
|
+
}
|
|
66
|
+
if (resolved) {
|
|
67
|
+
mappings.set(importRequest, resolved);
|
|
68
|
+
log("Added mapping for %s -> %s in env=%s", importRequest, resolved, env);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
log("Environment import mappings complete for env=%s: %d mappings", env, mappings.size);
|
|
72
|
+
return mappings;
|
|
73
|
+
}
|
|
74
|
+
function createEsbuildResolverPlugin(envName) {
|
|
75
|
+
const mappings = ENV_IMPORT_MAPPINGS[envName];
|
|
76
|
+
if (!mappings) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
name: `rwsdk:react-conditions-resolver-esbuild-${envName}`,
|
|
81
|
+
setup(build) {
|
|
82
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
83
|
+
process.env.VERBOSE &&
|
|
84
|
+
log("ESBuild resolving %s for env=%s, args=%O", args.path, envName, args);
|
|
85
|
+
const resolved = mappings.get(args.path);
|
|
86
|
+
if (resolved && args.importer !== "") {
|
|
87
|
+
process.env.VERBOSE &&
|
|
88
|
+
log("ESBuild resolving %s -> %s for env=%s", args.path, resolved, envName);
|
|
89
|
+
if (args.path === "react-server-dom-webpack/client.edge") {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
path: resolved,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
process.env.VERBOSE &&
|
|
98
|
+
log("ESBuild no resolution found for %s for env=%s", args.path, envName);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
export const reactConditionsResolverPlugin = () => {
|
|
105
|
+
log("Initializing react conditions resolver plugin");
|
|
106
|
+
let isBuild = false;
|
|
107
|
+
return [
|
|
108
|
+
{
|
|
109
|
+
name: "rwsdk:react-conditions-resolver:config",
|
|
110
|
+
enforce: "post",
|
|
111
|
+
config(config, { command }) {
|
|
112
|
+
isBuild = command === "build";
|
|
113
|
+
log("Configuring plugin for command=%s", command);
|
|
114
|
+
},
|
|
115
|
+
configResolved(config) {
|
|
116
|
+
log("Setting up resolve aliases and optimizeDeps for each environment");
|
|
117
|
+
// Set up aliases and optimizeDeps for each environment
|
|
118
|
+
for (const [envName, mappings] of Object.entries(ENV_IMPORT_MAPPINGS)) {
|
|
119
|
+
const reactImports = ENV_REACT_IMPORTS[envName];
|
|
120
|
+
// Ensure environment config exists
|
|
121
|
+
if (!config.environments) {
|
|
122
|
+
config.environments = {};
|
|
123
|
+
}
|
|
124
|
+
if (!config.environments[envName]) {
|
|
125
|
+
config.environments[envName] = {};
|
|
126
|
+
}
|
|
127
|
+
const envConfig = config.environments[envName];
|
|
128
|
+
const esbuildPlugin = createEsbuildResolverPlugin(envName);
|
|
129
|
+
if (esbuildPlugin && mappings) {
|
|
130
|
+
envConfig.optimizeDeps ??= {};
|
|
131
|
+
envConfig.optimizeDeps.esbuildOptions ??= {};
|
|
132
|
+
envConfig.optimizeDeps.esbuildOptions.define ??= {};
|
|
133
|
+
envConfig.optimizeDeps.esbuildOptions.define["process.env.NODE_ENV"] = JSON.stringify(process.env.NODE_ENV);
|
|
134
|
+
envConfig.optimizeDeps.esbuildOptions.plugins ??= [];
|
|
135
|
+
envConfig.optimizeDeps.esbuildOptions.plugins.push(esbuildPlugin);
|
|
136
|
+
envConfig.optimizeDeps.include ??= [];
|
|
137
|
+
envConfig.optimizeDeps.include.push(...reactImports);
|
|
138
|
+
log("Added esbuild plugin and optimizeDeps includes for environment: %s", envName);
|
|
139
|
+
}
|
|
140
|
+
const aliases = ensureAliasArray(envConfig);
|
|
141
|
+
for (const [find, replacement] of mappings) {
|
|
142
|
+
const findRegex = new RegExp(`^${find.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")}$`);
|
|
143
|
+
aliases.push({ find: findRegex, replacement });
|
|
144
|
+
log("Added alias for env=%s: %s -> %s", envName, find, replacement);
|
|
145
|
+
}
|
|
146
|
+
log("Environment %s configured with %d aliases and %d optimizeDeps includes", envName, mappings.size, reactImports.length);
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: "rwsdk:react-conditions-resolver:resolveId",
|
|
152
|
+
enforce: "pre",
|
|
153
|
+
async resolveId(id, importer) {
|
|
154
|
+
if (!isBuild) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const envName = this.environment?.name;
|
|
158
|
+
if (!envName) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
process.env.VERBOSE &&
|
|
162
|
+
log("Resolving id=%s, environment=%s, importer=%s", id, envName, importer);
|
|
163
|
+
const mappings = ENV_IMPORT_MAPPINGS[envName];
|
|
164
|
+
if (!mappings) {
|
|
165
|
+
process.env.VERBOSE &&
|
|
166
|
+
log("No mappings found for environment: %s", envName);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const resolved = mappings.get(id);
|
|
170
|
+
if (resolved) {
|
|
171
|
+
log("Resolved %s -> %s for env=%s", id, resolved, envName);
|
|
172
|
+
return resolved;
|
|
173
|
+
}
|
|
174
|
+
process.env.VERBOSE &&
|
|
175
|
+
log("No resolution found for id=%s in env=%s", id, envName);
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
];
|
|
179
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { InlineConfig } from "vite";
|
|
2
|
+
export type RedwoodPluginOptions = {
|
|
3
|
+
silent?: boolean;
|
|
4
|
+
rootDir?: string;
|
|
5
|
+
includeCloudflarePlugin?: boolean;
|
|
6
|
+
configPath?: string;
|
|
7
|
+
entry?: {
|
|
8
|
+
client?: string | string[];
|
|
9
|
+
worker?: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export declare const redwoodPlugin: (options?: RedwoodPluginOptions) => Promise<InlineConfig["plugins"]>;
|