rwsdk 0.2.0 → 0.3.1
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/constants.d.mts +6 -1
- package/dist/lib/constants.mjs +6 -1
- package/dist/lib/smokeTests/browser.mjs +5 -21
- package/dist/lib/smokeTests/codeUpdates.d.mts +1 -1
- package/dist/lib/smokeTests/codeUpdates.mjs +41 -5
- package/dist/lib/smokeTests/development.d.mts +1 -1
- package/dist/lib/smokeTests/development.mjs +4 -10
- package/dist/lib/smokeTests/release.d.mts +1 -1
- package/dist/lib/smokeTests/release.mjs +4 -9
- package/dist/lib/smokeTests/runSmokeTests.mjs +2 -2
- package/dist/lib/smokeTests/templates/SmokeTest.template.js +3 -2
- package/dist/lib/testUtils/stubEnvVars.d.mts +2 -0
- package/dist/lib/testUtils/stubEnvVars.mjs +11 -0
- package/dist/runtime/imports/client.js +4 -9
- package/dist/runtime/imports/worker.js +2 -1
- package/dist/runtime/register/ssr.js +2 -1
- package/dist/runtime/requestInfo/worker.js +9 -1
- package/dist/runtime/worker.d.ts +0 -3
- package/dist/runtime/worker.js +1 -10
- package/dist/scripts/debug-sync.mjs +0 -23
- package/dist/scripts/smoke-test.mjs +0 -10
- package/dist/vite/buildApp.d.mts +15 -0
- package/dist/vite/buildApp.mjs +53 -0
- package/dist/vite/configPlugin.d.mts +4 -4
- package/dist/vite/configPlugin.mjs +70 -76
- package/dist/vite/constants.d.mts +2 -0
- package/dist/vite/constants.mjs +12 -0
- package/dist/vite/createDirectiveLookupPlugin.d.mts +0 -6
- package/dist/vite/createDirectiveLookupPlugin.mjs +69 -145
- package/dist/vite/createViteAwareResolver.d.mts +4 -0
- package/dist/vite/createViteAwareResolver.mjs +208 -0
- package/dist/vite/directiveModulesDevPlugin.d.mts +8 -0
- package/dist/vite/directiveModulesDevPlugin.mjs +87 -0
- package/dist/vite/directivesFilteringPlugin.d.mts +6 -0
- package/dist/vite/directivesFilteringPlugin.mjs +31 -0
- package/dist/vite/directivesPlugin.mjs +32 -42
- package/dist/vite/getViteEsbuild.d.mts +1 -0
- package/dist/vite/getViteEsbuild.mjs +12 -0
- package/dist/vite/injectVitePreamblePlugin.d.mts +3 -2
- package/dist/vite/injectVitePreamblePlugin.mjs +8 -2
- package/dist/vite/linkerPlugin.d.mts +4 -0
- package/dist/vite/linkerPlugin.mjs +41 -0
- package/dist/vite/manifestPlugin.d.mts +2 -2
- package/dist/vite/manifestPlugin.mjs +20 -37
- package/dist/vite/moveStaticAssetsPlugin.mjs +2 -1
- package/dist/vite/prismaPlugin.mjs +1 -1
- package/dist/vite/reactConditionsResolverPlugin.mjs +15 -16
- package/dist/vite/redwoodPlugin.d.mts +0 -1
- package/dist/vite/redwoodPlugin.mjs +27 -9
- package/dist/vite/runDirectivesScan.d.mts +7 -0
- package/dist/vite/runDirectivesScan.mjs +156 -0
- package/dist/vite/ssrBridgePlugin.mjs +21 -14
- package/dist/vite/transformClientComponents.d.mts +0 -1
- package/dist/vite/transformClientComponents.mjs +1 -9
- package/dist/vite/transformJsxScriptTagsPlugin.d.mts +4 -3
- package/dist/vite/transformJsxScriptTagsPlugin.mjs +66 -84
- package/dist/vite/transformJsxScriptTagsPlugin.test.mjs +67 -41
- package/dist/vite/transformServerFunctions.d.mts +1 -1
- package/dist/vite/transformServerFunctions.mjs +11 -12
- package/dist/vite/virtualPlugin.mjs +8 -0
- package/package.json +7 -1
- package/dist/runtime/clientNavigation.d.ts +0 -9
- package/dist/runtime/clientNavigation.js +0 -88
|
@@ -1,24 +1,64 @@
|
|
|
1
1
|
import path, { resolve } from "node:path";
|
|
2
|
-
import { builtinModules } from "node:module";
|
|
3
2
|
import enhancedResolve from "enhanced-resolve";
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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, }) => ({
|
|
3
|
+
import debug from "debug";
|
|
4
|
+
import { INTERMEDIATE_SSR_BRIDGE_PATH } from "../lib/constants.mjs";
|
|
5
|
+
import { buildApp } from "./buildApp.mjs";
|
|
6
|
+
import { externalModules } from "./constants.mjs";
|
|
7
|
+
const log = debug("rwsdk:vite:config");
|
|
8
|
+
export const configPlugin = ({ silent, projectRootDir, workerEntryPathname, clientFiles, serverFiles, clientEntryPoints, }) => ({
|
|
19
9
|
name: "rwsdk:config",
|
|
20
10
|
config: async (_) => {
|
|
21
11
|
const mode = process.env.NODE_ENV;
|
|
12
|
+
const workerConfig = {
|
|
13
|
+
resolve: {
|
|
14
|
+
conditions: [
|
|
15
|
+
"workerd",
|
|
16
|
+
"react-server",
|
|
17
|
+
"module",
|
|
18
|
+
// context(justinvdm, 11 Jun 2025): Some packages meant for cloudflare workers, yet
|
|
19
|
+
// their deps have only node import conditions, e.g. `agents` package (meant for CF),
|
|
20
|
+
// has `pkce-challenge` package as a dep, which has only node import conditions.
|
|
21
|
+
// https://github.com/crouchcd/pkce-challenge/blob/master/package.json#L17
|
|
22
|
+
//
|
|
23
|
+
// @cloudflare/vite-plugin should take care of any relevant polyfills for deps with
|
|
24
|
+
// node builtins imports that can be polyfilled though, so it is worth us including this condition here.
|
|
25
|
+
// However, it does mean we will try to run packages meant for node that cannot be run on cloudflare workers.
|
|
26
|
+
// That's the trade-off, but arguably worth it.
|
|
27
|
+
"node",
|
|
28
|
+
],
|
|
29
|
+
noExternal: true,
|
|
30
|
+
},
|
|
31
|
+
define: {
|
|
32
|
+
"import.meta.env.RWSDK_ENV": JSON.stringify("worker"),
|
|
33
|
+
},
|
|
34
|
+
optimizeDeps: {
|
|
35
|
+
noDiscovery: false,
|
|
36
|
+
include: ["rwsdk/worker"],
|
|
37
|
+
exclude: [],
|
|
38
|
+
entries: [workerEntryPathname],
|
|
39
|
+
esbuildOptions: {
|
|
40
|
+
jsx: "automatic",
|
|
41
|
+
jsxImportSource: "react",
|
|
42
|
+
define: {
|
|
43
|
+
"process.env.NODE_ENV": JSON.stringify(mode),
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
build: {
|
|
48
|
+
outDir: resolve(projectRootDir, "dist", "worker"),
|
|
49
|
+
emitAssets: true,
|
|
50
|
+
emptyOutDir: false,
|
|
51
|
+
ssr: true,
|
|
52
|
+
rollupOptions: {
|
|
53
|
+
output: {
|
|
54
|
+
inlineDynamicImports: true,
|
|
55
|
+
},
|
|
56
|
+
input: {
|
|
57
|
+
worker: workerEntryPathname,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
};
|
|
22
62
|
const baseConfig = {
|
|
23
63
|
appType: "custom",
|
|
24
64
|
mode,
|
|
@@ -40,7 +80,7 @@ export const configPlugin = ({ silent, projectRootDir, clientEntryPathnames, wor
|
|
|
40
80
|
outDir: resolve(projectRootDir, "dist", "client"),
|
|
41
81
|
manifest: true,
|
|
42
82
|
rollupOptions: {
|
|
43
|
-
input:
|
|
83
|
+
input: [],
|
|
44
84
|
},
|
|
45
85
|
},
|
|
46
86
|
define: {
|
|
@@ -49,6 +89,7 @@ export const configPlugin = ({ silent, projectRootDir, clientEntryPathnames, wor
|
|
|
49
89
|
optimizeDeps: {
|
|
50
90
|
noDiscovery: false,
|
|
51
91
|
include: ["rwsdk/client"],
|
|
92
|
+
entries: [],
|
|
52
93
|
esbuildOptions: {
|
|
53
94
|
jsx: "automatic",
|
|
54
95
|
jsxImportSource: "react",
|
|
@@ -87,80 +128,33 @@ export const configPlugin = ({ silent, projectRootDir, clientEntryPathnames, wor
|
|
|
87
128
|
build: {
|
|
88
129
|
lib: {
|
|
89
130
|
entry: {
|
|
90
|
-
[path.basename(
|
|
131
|
+
[path.basename(INTERMEDIATE_SSR_BRIDGE_PATH, ".js")]: enhancedResolve.sync(projectRootDir, "rwsdk/__ssr_bridge"),
|
|
91
132
|
},
|
|
92
133
|
formats: ["es"],
|
|
93
|
-
fileName: () => path.basename(
|
|
134
|
+
fileName: () => path.basename(INTERMEDIATE_SSR_BRIDGE_PATH),
|
|
94
135
|
},
|
|
95
|
-
outDir: path.dirname(
|
|
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,
|
|
136
|
+
outDir: path.dirname(INTERMEDIATE_SSR_BRIDGE_PATH),
|
|
137
137
|
rollupOptions: {
|
|
138
138
|
output: {
|
|
139
139
|
inlineDynamicImports: true,
|
|
140
140
|
},
|
|
141
|
-
input: {
|
|
142
|
-
worker: workerEntryPathname,
|
|
143
|
-
},
|
|
144
141
|
},
|
|
145
142
|
},
|
|
146
143
|
},
|
|
144
|
+
worker: workerConfig,
|
|
147
145
|
},
|
|
148
146
|
server: {
|
|
149
147
|
hmr: true,
|
|
150
148
|
},
|
|
151
149
|
builder: {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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"]);
|
|
150
|
+
async buildApp(builder) {
|
|
151
|
+
await buildApp({
|
|
152
|
+
builder,
|
|
153
|
+
projectRootDir,
|
|
154
|
+
clientEntryPoints,
|
|
155
|
+
clientFiles,
|
|
156
|
+
serverFiles,
|
|
157
|
+
});
|
|
164
158
|
},
|
|
165
159
|
},
|
|
166
160
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { builtinModules } from "node:module";
|
|
2
|
+
export const cloudflareBuiltInModules = [
|
|
3
|
+
"cloudflare:email",
|
|
4
|
+
"cloudflare:sockets",
|
|
5
|
+
"cloudflare:workers",
|
|
6
|
+
"cloudflare:workflows",
|
|
7
|
+
];
|
|
8
|
+
export const externalModules = [
|
|
9
|
+
...cloudflareBuiltInModules,
|
|
10
|
+
...builtinModules,
|
|
11
|
+
...builtinModules.map((m) => `node:${m}`),
|
|
12
|
+
];
|
|
@@ -7,12 +7,6 @@ interface DirectiveLookupConfig {
|
|
|
7
7
|
pluginName: string;
|
|
8
8
|
optimizeForEnvironments?: string[];
|
|
9
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
10
|
export declare const createDirectiveLookupPlugin: ({ projectRootDir, files, config, }: {
|
|
17
11
|
projectRootDir: string;
|
|
18
12
|
files: Set<string>;
|
|
@@ -1,125 +1,12 @@
|
|
|
1
1
|
import MagicString from "magic-string";
|
|
2
2
|
import path from "path";
|
|
3
|
-
import { readFile } from "fs/promises";
|
|
4
3
|
import debug from "debug";
|
|
5
|
-
import { normalizeModulePath } from "../lib/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(file, projectRootDir);
|
|
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
4
|
export const createDirectiveLookupPlugin = async ({ projectRootDir, files, config, }) => {
|
|
112
5
|
const debugNamespace = `rwsdk:vite:${config.pluginName}`;
|
|
113
6
|
const log = debug(debugNamespace);
|
|
114
7
|
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
8
|
let devServer;
|
|
9
|
+
log("Initializing %s plugin with projectRootDir=%s", config.pluginName, projectRootDir);
|
|
123
10
|
return {
|
|
124
11
|
name: `rwsdk:${config.pluginName}`,
|
|
125
12
|
config(_, { command, isPreview }) {
|
|
@@ -129,16 +16,13 @@ export const createDirectiveLookupPlugin = async ({ projectRootDir, files, confi
|
|
|
129
16
|
configureServer(server) {
|
|
130
17
|
devServer = server;
|
|
131
18
|
},
|
|
132
|
-
|
|
19
|
+
configEnvironment(env, viteConfig) {
|
|
20
|
+
if (!isDev &&
|
|
21
|
+
process.env.RWSDK_BUILD_PASS &&
|
|
22
|
+
process.env.RWSDK_BUILD_PASS !== "worker") {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
133
25
|
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
26
|
viteConfig.optimizeDeps ??= {};
|
|
143
27
|
viteConfig.optimizeDeps.esbuildOptions ??= {};
|
|
144
28
|
viteConfig.optimizeDeps.esbuildOptions.plugins ??= [];
|
|
@@ -150,7 +34,7 @@ export const createDirectiveLookupPlugin = async ({ projectRootDir, files, confi
|
|
|
150
34
|
const escapedVirtualModuleName = config.virtualModuleName.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
151
35
|
const escapedPrefixedModuleName = `/@id/${config.virtualModuleName}`.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
152
36
|
build.onResolve({
|
|
153
|
-
filter: new RegExp(`^(${escapedVirtualModuleName}|${escapedPrefixedModuleName})
|
|
37
|
+
filter: new RegExp(`^(${escapedVirtualModuleName}|${escapedPrefixedModuleName})\\.js$`),
|
|
154
38
|
}, () => {
|
|
155
39
|
process.env.VERBOSE &&
|
|
156
40
|
log("Esbuild onResolve: marking %s as external", config.virtualModuleName);
|
|
@@ -167,6 +51,9 @@ export const createDirectiveLookupPlugin = async ({ projectRootDir, files, confi
|
|
|
167
51
|
log("Applying optimizeDeps and aliasing for environment: %s", env);
|
|
168
52
|
viteConfig.optimizeDeps.include ??= [];
|
|
169
53
|
for (const file of files) {
|
|
54
|
+
if (file.includes("node_modules")) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
170
57
|
const actualFilePath = path.join(projectRootDir, file);
|
|
171
58
|
process.env.VERBOSE &&
|
|
172
59
|
log("Adding to optimizeDeps.entries: %s", actualFilePath);
|
|
@@ -183,36 +70,74 @@ export const createDirectiveLookupPlugin = async ({ projectRootDir, files, confi
|
|
|
183
70
|
}
|
|
184
71
|
},
|
|
185
72
|
resolveId(source) {
|
|
186
|
-
|
|
187
|
-
if (
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
return
|
|
73
|
+
// Skip during directive scanning to avoid performance issues
|
|
74
|
+
if (process.env.RWSDK_DIRECTIVE_SCAN_ACTIVE) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (source !== `${config.virtualModuleName}.js`) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
// context(justinvdm, 3 Sep 2025): This logic determines *when* to
|
|
81
|
+
// generate and bundle the lookup map. By conditionally externalizing it,
|
|
82
|
+
// we ensure the map is only created after tree-shaking is complete and
|
|
83
|
+
// that it's correctly shared between the SSR and final worker builds.
|
|
84
|
+
log("Resolving %s module", config.virtualModuleName);
|
|
85
|
+
const envName = this.environment?.name;
|
|
86
|
+
// 1. Worker Pass -> externalize
|
|
87
|
+
if (isDev &&
|
|
88
|
+
envName === "worker" &&
|
|
89
|
+
process.env.RWSDK_BUILD_PASS === "worker") {
|
|
90
|
+
// context(justinvdm, 3 Sep 2025): We externalize the lookup during the
|
|
91
|
+
// first worker pass. This defers its bundling until after the
|
|
92
|
+
// directivesFilteringPlugin has had a chance to run and tree-shake
|
|
93
|
+
// the list of client/server files.
|
|
94
|
+
log("Marking as external for worker pass");
|
|
95
|
+
return { id: source, external: true };
|
|
192
96
|
}
|
|
193
|
-
|
|
97
|
+
// 2. SSR Pass -> externalize
|
|
98
|
+
if (isDev && envName === "ssr") {
|
|
99
|
+
// context(justinvdm, 3 Sep 2025): We also externalize during the SSR
|
|
100
|
+
// build. This ensures that both the worker and SSR artifacts refer to
|
|
101
|
+
// the same virtual module, which will be resolved into a single, shared
|
|
102
|
+
// lookup map during the final linker pass.
|
|
103
|
+
log("Marking as external for ssr pass");
|
|
104
|
+
return { id: source, external: true };
|
|
105
|
+
}
|
|
106
|
+
// 3. Client Pass & 4. Linker Pass -> resolve and bundle
|
|
107
|
+
// context(justinvdm, 3 Sep 2025): For the client build, the dev server,
|
|
108
|
+
// and the final linker pass, we resolve the module ID with a null-byte
|
|
109
|
+
// prefix. This signals to Vite/Rollup that this is a virtual module
|
|
110
|
+
// whose content should be provided by the `load` hook, bundling it in.
|
|
111
|
+
log("Resolving for bundling");
|
|
112
|
+
return source;
|
|
194
113
|
},
|
|
195
114
|
async load(id) {
|
|
196
|
-
|
|
115
|
+
// Skip during directive scanning to avoid performance issues
|
|
116
|
+
if (process.env.RWSDK_DIRECTIVE_SCAN_ACTIVE) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
197
119
|
if (id === config.virtualModuleName + ".js") {
|
|
198
120
|
log("Loading %s module with %d files", config.virtualModuleName, files.size);
|
|
199
121
|
const environment = this.environment?.name || "client";
|
|
200
122
|
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
123
|
const s = new MagicString(`
|
|
211
124
|
export const ${config.exportName} = {
|
|
212
125
|
${Array.from(files)
|
|
213
|
-
.map((file) =>
|
|
214
|
-
|
|
215
|
-
|
|
126
|
+
.map((file) => {
|
|
127
|
+
if (file.includes("node_modules") && isDev) {
|
|
128
|
+
const barrelPath = config.kind === "client"
|
|
129
|
+
? "rwsdk/__client_barrel"
|
|
130
|
+
: "rwsdk/__server_barrel";
|
|
131
|
+
return `
|
|
132
|
+
"${file}": () => import("${barrelPath}").then(m => m.default["${file}"]),
|
|
133
|
+
`;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
return `
|
|
137
|
+
"${file}": () => import("${file}"),
|
|
138
|
+
`;
|
|
139
|
+
}
|
|
140
|
+
})
|
|
216
141
|
.join("")}
|
|
217
142
|
};
|
|
218
143
|
`);
|
|
@@ -225,7 +150,6 @@ export const ${config.exportName} = {
|
|
|
225
150
|
map,
|
|
226
151
|
};
|
|
227
152
|
}
|
|
228
|
-
process.env.VERBOSE && log("No load handling for id=%s", id);
|
|
229
153
|
},
|
|
230
154
|
};
|
|
231
155
|
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import resolve, { ResolveOptions } from "enhanced-resolve";
|
|
2
|
+
import { ResolvedConfig } from "vite";
|
|
3
|
+
export declare const mapViteResolveToEnhancedResolveOptions: (viteConfig: ResolvedConfig, envName: string) => ResolveOptions;
|
|
4
|
+
export declare const createViteAwareResolver: (viteConfig: ResolvedConfig, envName: string, environment?: any) => resolve.ResolveFunctionAsync;
|