vite-plugin-react-server 0.3.11 → 0.3.12
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/package.json +15 -11
- package/dist/plugin/checkFilesExist.d.ts +2 -2
- package/dist/plugin/checkFilesExist.d.ts.map +1 -1
- package/dist/plugin/checkFilesExist.js +39 -57
- package/dist/plugin/checkFilesExist.js.map +1 -1
- package/dist/plugin/collect-css-manifest.d.ts.map +1 -1
- package/dist/plugin/collect-css-manifest.js +5 -0
- package/dist/plugin/collect-css-manifest.js.map +1 -1
- package/dist/plugin/components.js +10 -15
- package/dist/plugin/config/createModuleIdGenerator.js +1 -1
- package/dist/plugin/config/createModuleIdGenerator.js.map +1 -1
- package/dist/plugin/config/defaults.d.ts +5 -14
- package/dist/plugin/config/defaults.d.ts.map +1 -1
- package/dist/plugin/config/defaults.js +12 -12
- package/dist/plugin/config/defaults.js.map +1 -1
- package/dist/plugin/config/getPaths.js +1 -1
- package/dist/plugin/config/resolveOptions.d.ts.map +1 -1
- package/dist/plugin/config/resolveOptions.js +73 -52
- package/dist/plugin/config/resolveOptions.js.map +1 -1
- package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -1
- package/dist/plugin/config/resolveUserConfig.js +53 -65
- package/dist/plugin/config/resolveUserConfig.js.map +1 -1
- package/dist/plugin/helpers/getBundleManifest.d.ts +6 -1
- package/dist/plugin/helpers/getBundleManifest.d.ts.map +1 -1
- package/dist/plugin/helpers/getBundleManifest.js +48 -19
- package/dist/plugin/helpers/getBundleManifest.js.map +1 -1
- package/dist/plugin/helpers/inputNormalizer.d.ts +1 -2
- package/dist/plugin/helpers/inputNormalizer.d.ts.map +1 -1
- package/dist/plugin/helpers/inputNormalizer.js +52 -46
- package/dist/plugin/helpers/inputNormalizer.js.map +1 -1
- package/dist/plugin/helpers/tryManifest.d.ts +1 -1
- package/dist/plugin/helpers/tryManifest.d.ts.map +1 -1
- package/dist/plugin/helpers/tryManifest.js.map +1 -1
- package/dist/plugin/loader/createBuildLoader.d.ts +1 -1
- package/dist/plugin/loader/createBuildLoader.d.ts.map +1 -1
- package/dist/plugin/loader/createBuildLoader.js +29 -26
- package/dist/plugin/loader/createBuildLoader.js.map +1 -1
- package/dist/plugin/loader/css-loader.d.ts +16 -0
- package/dist/plugin/loader/css-loader.d.ts.map +1 -0
- package/dist/plugin/loader/css-loader.js +70 -0
- package/dist/plugin/loader/css-loader.js.map +1 -0
- package/dist/plugin/loader/react-loader.d.ts +17 -0
- package/dist/plugin/loader/react-loader.d.ts.map +1 -0
- package/dist/plugin/loader/react-loader.js +647 -0
- package/dist/plugin/loader/react-loader.js.map +1 -0
- package/dist/plugin/loader/rsc/messageHandler.d.ts +2 -0
- package/dist/plugin/loader/rsc/messageHandler.d.ts.map +1 -0
- package/dist/plugin/loader/rsc/messageHandler.js +1 -0
- package/dist/plugin/loader/rsc/rsc-worker.development.d.ts +2 -0
- package/dist/plugin/loader/rsc/rsc-worker.development.d.ts.map +1 -0
- package/dist/plugin/loader/rsc/rsc-worker.development.js +1 -0
- package/dist/plugin/react-client/index.js +2 -2
- package/dist/plugin/react-client/index.js.map +1 -1
- package/dist/plugin/react-client/plugin.d.ts.map +1 -1
- package/dist/plugin/react-client/plugin.js +202 -25
- package/dist/plugin/react-client/plugin.js.map +1 -1
- package/dist/plugin/react-server/createHandler.d.ts.map +1 -1
- package/dist/plugin/react-server/createHandler.js +10 -4
- package/dist/plugin/react-server/createHandler.js.map +1 -1
- package/dist/plugin/react-server/createRscStream.d.ts +15 -3
- package/dist/plugin/react-server/createRscStream.d.ts.map +1 -1
- package/dist/plugin/react-server/createRscStream.js +52 -49
- package/dist/plugin/react-server/createRscStream.js.map +1 -1
- package/dist/plugin/react-server/plugin.d.ts.map +1 -1
- package/dist/plugin/react-server/plugin.js +24 -20
- package/dist/plugin/react-server/plugin.js.map +1 -1
- package/dist/plugin/transformer/plugin.d.ts.map +1 -1
- package/dist/plugin/transformer/plugin.js +65 -52
- package/dist/plugin/transformer/plugin.js.map +1 -1
- package/dist/plugin/types.d.ts +5 -0
- package/dist/plugin/types.d.ts.map +1 -1
- package/dist/plugin/utils/logger.d.ts +9 -0
- package/dist/plugin/utils/logger.d.ts.map +1 -0
- package/dist/plugin/utils/logger.js +68 -0
- package/dist/plugin/utils/logger.js.map +1 -0
- package/dist/plugin/worker/createWorker.d.ts +1 -0
- package/dist/plugin/worker/createWorker.d.ts.map +1 -1
- package/dist/plugin/worker/createWorker.js +23 -36
- package/dist/plugin/worker/createWorker.js.map +1 -1
- package/dist/plugin/worker/html/html-worker.production.js +5 -1
- package/dist/plugin/worker/html/html-worker.production.js.map +1 -1
- package/dist/plugin/worker/html/messageHandler.d.ts.map +1 -1
- package/dist/plugin/worker/html/messageHandler.js +10 -19
- package/dist/plugin/worker/html/messageHandler.js.map +1 -1
- package/dist/plugin/worker/html/renderPages.d.ts +2 -2
- package/dist/plugin/worker/html/renderPages.d.ts.map +1 -1
- package/dist/plugin/worker/html/renderPages.js +130 -131
- package/dist/plugin/worker/html/renderPages.js.map +1 -1
- package/dist/plugin/worker/rsc/index.d.ts +1 -3
- package/dist/plugin/worker/rsc/index.d.ts.map +1 -1
- package/dist/plugin/worker/rsc/index.js +1 -9
- package/dist/plugin/worker/rsc/index.js.map +1 -1
- package/dist/plugin/worker/rsc/messageHandler.d.ts +3 -0
- package/dist/plugin/worker/rsc/messageHandler.d.ts.map +1 -0
- package/dist/plugin/worker/rsc/messageHandler.js +107 -0
- package/dist/plugin/worker/rsc/messageHandler.js.map +1 -0
- package/dist/plugin/worker/rsc/plugin.d.ts.map +1 -1
- package/dist/plugin/worker/rsc/plugin.js +74 -80
- package/dist/plugin/worker/rsc/rsc-worker.development.d.ts +32 -0
- package/dist/plugin/worker/rsc/rsc-worker.development.d.ts.map +1 -0
- package/dist/plugin/worker/rsc/rsc-worker.development.js +43 -0
- package/dist/plugin/worker/rsc/rsc-worker.development.js.map +1 -0
- package/dist/plugin/worker/rsc/rsc-worker.js +4 -106
- package/dist/plugin/worker/rsc/rsc-worker.production.d.ts +2 -0
- package/dist/plugin/worker/rsc/rsc-worker.production.d.ts.map +1 -0
- package/dist/plugin/worker/rsc/rsc-worker.production.js +14 -0
- package/dist/plugin/worker/rsc/rsc-worker.production.js.map +1 -0
- package/dist/plugin/worker/rsc/state.d.ts +11 -0
- package/dist/plugin/worker/rsc/state.d.ts.map +1 -0
- package/dist/plugin/worker/rsc/state.js +12 -0
- package/dist/plugin/worker/rsc/state.js.map +1 -0
- package/dist/plugin/worker/types.d.ts +60 -46
- package/dist/plugin/worker/types.d.ts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -11
- package/plugin/checkFilesExist.ts +42 -62
- package/plugin/collect-css-manifest.ts +5 -1
- package/plugin/config/createModuleIdGenerator.ts +1 -1
- package/plugin/config/defaults.ts +13 -15
- package/plugin/config/resolveOptions.ts +134 -76
- package/plugin/config/resolveUserConfig.ts +75 -76
- package/plugin/helpers/getBundleManifest.ts +69 -31
- package/plugin/helpers/inputNormalizer.ts +82 -70
- package/plugin/helpers/tryManifest.ts +1 -1
- package/plugin/loader/createBuildLoader.ts +38 -41
- package/plugin/loader/css-loader.ts +96 -0
- package/plugin/loader/react-loader.ts +945 -0
- package/plugin/loader/rsc/messageHandler.tsx +1 -0
- package/plugin/loader/rsc/rsc-worker.development.ts +1 -0
- package/plugin/react-client/index.ts +1 -1
- package/plugin/react-client/plugin.ts +266 -41
- package/plugin/react-server/createHandler.ts +9 -5
- package/plugin/react-server/createRscStream.ts +75 -54
- package/plugin/react-server/plugin.ts +26 -21
- package/plugin/transformer/plugin.ts +67 -76
- package/plugin/types/global.d.ts +8 -0
- package/plugin/types.ts +2 -0
- package/plugin/utils/logger.ts +52 -0
- package/plugin/worker/createWorker.ts +43 -44
- package/plugin/worker/html/html-worker.production.tsx +7 -2
- package/plugin/worker/html/messageHandler.ts +13 -21
- package/plugin/worker/html/renderPages.ts +146 -179
- package/plugin/worker/rsc/index.ts +4 -13
- package/plugin/worker/rsc/messageHandler.tsx +143 -0
- package/plugin/worker/rsc/plugin.ts +38 -37
- package/plugin/worker/rsc/rsc-worker.development.ts +107 -0
- package/plugin/worker/rsc/rsc-worker.production.ts +13 -0
- package/plugin/worker/rsc/rsc-worker.tsx +5 -128
- package/plugin/worker/rsc/state.ts +37 -0
- package/plugin/worker/types.ts +79 -55
- package/scripts/check-react-version.mjs +17 -7
- package/scripts/react+0.0.0-experimental-b3a95caf-20250113.patch +143 -4170
- package/scripts/react-dom+0.0.0-experimental-b3a95caf-20250113.patch +14271 -90079
- package/dist/plugin/components.js.map +0 -1
- package/dist/plugin/helpers/createClientInputNormalizer.d.ts +0 -8
- package/dist/plugin/helpers/createClientInputNormalizer.d.ts.map +0 -1
- package/dist/plugin/helpers/createClientInputNormalizer.js +0 -35
- package/dist/plugin/helpers/createServerInputNormalizer.d.ts +0 -9
- package/dist/plugin/helpers/createServerInputNormalizer.d.ts.map +0 -1
- package/dist/plugin/helpers/createServerInputNormalizer.js +0 -37
- package/dist/plugin/helpers/createStaticInputNormalizer.d.ts +0 -7
- package/dist/plugin/helpers/createStaticInputNormalizer.d.ts.map +0 -1
- package/dist/plugin/helpers/createStaticInputNormalizer.js +0 -18
- package/dist/plugin/helpers/getModuleManifest.d.ts +0 -17
- package/dist/plugin/helpers/getModuleManifest.d.ts.map +0 -1
- package/dist/plugin/helpers/getModuleManifest.js +0 -23
- package/dist/plugin/helpers/inputNormalizerWorker.d.ts +0 -12
- package/dist/plugin/helpers/inputNormalizerWorker.d.ts.map +0 -1
- package/dist/plugin/helpers/inputNormalizerWorker.js +0 -33
- package/dist/plugin/helpers/normalizedRelativePath.d.ts +0 -11
- package/dist/plugin/helpers/normalizedRelativePath.d.ts.map +0 -1
- package/dist/plugin/helpers/normalizedRelativePath.js +0 -36
- package/dist/plugin/helpers/resolveFilePath.d.ts +0 -13
- package/dist/plugin/helpers/resolveFilePath.d.ts.map +0 -1
- package/dist/plugin/helpers/resolveFilePath.js +0 -74
- package/dist/plugin/helpers/resolveWorkerModule.d.ts +0 -6
- package/dist/plugin/helpers/resolveWorkerModule.d.ts.map +0 -1
- package/dist/plugin/helpers/resolveWorkerModule.js +0 -24
- package/dist/plugin/helpers/validateModuleBase.d.ts +0 -3
- package/dist/plugin/helpers/validateModuleBase.d.ts.map +0 -1
- package/dist/plugin/helpers/validateModuleBase.js +0 -16
- package/dist/plugin/helpers/validateResolvedConfig.d.ts +0 -3
- package/dist/plugin/helpers/validateResolvedConfig.d.ts.map +0 -1
- package/dist/plugin/helpers/validateResolvedConfig.js +0 -17
- package/dist/plugin/transformer/transformer-client-components.d.ts +0 -30
- package/dist/plugin/transformer/transformer-client-components.d.ts.map +0 -1
- package/dist/plugin/transformer/transformer-client-components.js +0 -122
- package/dist/plugin/transformer/transformer-client-components.js.map +0 -1
- package/dist/plugin/transformer/transformer-server-actions.d.ts +0 -29
- package/dist/plugin/transformer/transformer-server-actions.d.ts.map +0 -1
- package/dist/plugin/transformer/transformer-server-actions.js +0 -90
- package/dist/plugin/worker/rsc/createRscStream.d.ts +0 -5
- package/dist/plugin/worker/rsc/createRscStream.d.ts.map +0 -1
- package/dist/plugin/worker/rsc/createRscStream.js +0 -39
- package/dist/plugin/worker/rsc/createRscStream.js.map +0 -1
- package/dist/plugin/worker/rsc/development.d.ts +0 -5
- package/dist/plugin/worker/rsc/development.d.ts.map +0 -1
- package/dist/plugin/worker/rsc/development.js +0 -13
- package/dist/plugin/worker/rsc/development.js.map +0 -1
- package/dist/plugin/worker/rsc/plugin.js.map +0 -1
- package/dist/plugin/worker/rsc/production.d.ts +0 -5
- package/dist/plugin/worker/rsc/production.d.ts.map +0 -1
- package/dist/plugin/worker/rsc/production.js +0 -13
- package/dist/plugin/worker/rsc/production.js.map +0 -1
- package/plugin/helpers/createClientInputNormalizer.ts +0 -48
- package/plugin/helpers/createServerInputNormalizer.ts +0 -52
- package/plugin/helpers/createStaticInputNormalizer.ts +0 -26
- package/plugin/helpers/getModuleManifest.ts +0 -36
- package/plugin/helpers/inputNormalizerWorker.ts +0 -52
- package/plugin/helpers/normalizedRelativePath.ts +0 -59
- package/plugin/helpers/resolveFilePath.ts +0 -108
- package/plugin/helpers/resolveWorkerModule.ts +0 -41
- package/plugin/helpers/validateModuleBase.ts +0 -30
- package/plugin/helpers/validateResolvedConfig.ts +0 -21
- package/plugin/transformer/transformer-client-components.ts +0 -168
- package/plugin/transformer/transformer-server-actions.ts +0 -125
- package/plugin/worker/rsc/createRscStream.ts +0 -42
- package/plugin/worker/rsc/development.ts +0 -6
- package/plugin/worker/rsc/production.ts +0 -6
- package/scripts/react-server-dom-esm+0.0.1.patch +0 -24775
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vite-plugin-react-server
|
|
3
|
+
* Copyright (c) Nico Brinkkemper
|
|
4
|
+
* MIT License
|
|
5
|
+
*/
|
|
6
|
+
import * as acorn from 'acorn-loose';
|
|
7
|
+
import { basename } from 'path';
|
|
8
|
+
import { setSourceMapsSupport, SourceMap } from 'node:module';
|
|
9
|
+
|
|
10
|
+
let stashedGetSource = null;
|
|
11
|
+
setSourceMapsSupport(true, {
|
|
12
|
+
nodeModules: true,
|
|
13
|
+
// Enable for node_modules files
|
|
14
|
+
generatedCode: true
|
|
15
|
+
// Enable for generated code
|
|
16
|
+
});
|
|
17
|
+
let loaderPort;
|
|
18
|
+
async function getSource(url, context, defaultGetSource) {
|
|
19
|
+
stashedGetSource = defaultGetSource;
|
|
20
|
+
return defaultGetSource(url, context, defaultGetSource);
|
|
21
|
+
}
|
|
22
|
+
function addExportedEntry(exportedEntries, localNames, localName, exportedName, type, loc) {
|
|
23
|
+
if (localNames.has(localName)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
exportedEntries.push({
|
|
27
|
+
localName,
|
|
28
|
+
exportedName,
|
|
29
|
+
type,
|
|
30
|
+
loc,
|
|
31
|
+
originalLine: -1,
|
|
32
|
+
originalColumn: -1,
|
|
33
|
+
originalSource: -1,
|
|
34
|
+
nameIndex: -1
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function addLocalExportedNames(exportedEntries, localNames, node) {
|
|
38
|
+
switch (node.type) {
|
|
39
|
+
case "Identifier":
|
|
40
|
+
addExportedEntry(
|
|
41
|
+
exportedEntries,
|
|
42
|
+
localNames,
|
|
43
|
+
node.name,
|
|
44
|
+
node.name,
|
|
45
|
+
null,
|
|
46
|
+
node.loc
|
|
47
|
+
);
|
|
48
|
+
return;
|
|
49
|
+
case "ObjectPattern":
|
|
50
|
+
for (let i = 0; i < node.properties.length; i++)
|
|
51
|
+
addLocalExportedNames(exportedEntries, localNames, node.properties[i]);
|
|
52
|
+
return;
|
|
53
|
+
case "ArrayPattern":
|
|
54
|
+
for (let i = 0; i < node.elements.length; i++) {
|
|
55
|
+
const element = node.elements[i];
|
|
56
|
+
if (element)
|
|
57
|
+
addLocalExportedNames(exportedEntries, localNames, element);
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
case "Property":
|
|
61
|
+
addLocalExportedNames(exportedEntries, localNames, node.value);
|
|
62
|
+
return;
|
|
63
|
+
case "AssignmentPattern":
|
|
64
|
+
addLocalExportedNames(exportedEntries, localNames, node.left);
|
|
65
|
+
return;
|
|
66
|
+
case "RestElement":
|
|
67
|
+
addLocalExportedNames(exportedEntries, localNames, node.argument);
|
|
68
|
+
return;
|
|
69
|
+
case "ParenthesizedExpression":
|
|
70
|
+
addLocalExportedNames(exportedEntries, localNames, node.expression);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function transformServerModule(source, program, url, sourceMap, loader, port) {
|
|
75
|
+
const body = program.body;
|
|
76
|
+
const exportedEntries = [];
|
|
77
|
+
const localNames = /* @__PURE__ */ new Set();
|
|
78
|
+
for (let i = 0; i < body.length; i++) {
|
|
79
|
+
const node = body[i];
|
|
80
|
+
switch (node.type) {
|
|
81
|
+
case "ExportAllDeclaration":
|
|
82
|
+
break;
|
|
83
|
+
case "ExportDefaultDeclaration":
|
|
84
|
+
if (node.declaration.type === "Identifier") {
|
|
85
|
+
addExportedEntry(
|
|
86
|
+
exportedEntries,
|
|
87
|
+
localNames,
|
|
88
|
+
node.declaration.name,
|
|
89
|
+
"default",
|
|
90
|
+
null,
|
|
91
|
+
node.declaration.loc
|
|
92
|
+
);
|
|
93
|
+
} else if (node.declaration.type === "FunctionDeclaration") {
|
|
94
|
+
if (node.declaration.id) {
|
|
95
|
+
addExportedEntry(
|
|
96
|
+
exportedEntries,
|
|
97
|
+
localNames,
|
|
98
|
+
node.declaration.id.name,
|
|
99
|
+
"default",
|
|
100
|
+
"function",
|
|
101
|
+
node.declaration.id.loc
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
continue;
|
|
106
|
+
case "ExportNamedDeclaration":
|
|
107
|
+
if (node.declaration) {
|
|
108
|
+
if (node.declaration.type === "VariableDeclaration") {
|
|
109
|
+
const declarations = node.declaration.declarations;
|
|
110
|
+
for (let j = 0; j < declarations.length; j++) {
|
|
111
|
+
addLocalExportedNames(
|
|
112
|
+
exportedEntries,
|
|
113
|
+
localNames,
|
|
114
|
+
declarations[j].id
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
const name = node.declaration.id.name;
|
|
119
|
+
addExportedEntry(
|
|
120
|
+
exportedEntries,
|
|
121
|
+
localNames,
|
|
122
|
+
name,
|
|
123
|
+
name,
|
|
124
|
+
node.declaration.type === "FunctionDeclaration" ? "function" : null,
|
|
125
|
+
node.declaration.id.loc
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (node.specifiers) {
|
|
130
|
+
const specifiers = node.specifiers;
|
|
131
|
+
for (let j = 0; j < specifiers.length; j++) {
|
|
132
|
+
const specifier = specifiers[j];
|
|
133
|
+
addExportedEntry(
|
|
134
|
+
exportedEntries,
|
|
135
|
+
localNames,
|
|
136
|
+
specifier.local.name,
|
|
137
|
+
specifier.exported.name,
|
|
138
|
+
null,
|
|
139
|
+
specifier.local.loc
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
let mappings = sourceMap && typeof sourceMap.mappings === "string" ? sourceMap.mappings : "";
|
|
147
|
+
let newSrc = source;
|
|
148
|
+
if (exportedEntries.length > 0) {
|
|
149
|
+
let lastSourceIndex = 0;
|
|
150
|
+
let lastOriginalLine = 0;
|
|
151
|
+
let lastOriginalColumn = 0;
|
|
152
|
+
let lastNameIndex = 0;
|
|
153
|
+
let sourceLineCount = 0;
|
|
154
|
+
let lastMappedLine = 0;
|
|
155
|
+
if (sourceMap) {
|
|
156
|
+
let nextEntryIdx = 0;
|
|
157
|
+
let nextEntryLine = exportedEntries[nextEntryIdx].loc.start.line;
|
|
158
|
+
let nextEntryColumn = exportedEntries[nextEntryIdx].loc.start.column;
|
|
159
|
+
readMappings(
|
|
160
|
+
mappings,
|
|
161
|
+
(generatedLine2, generatedColumn, sourceIndex, originalLine, originalColumn, nameIndex) => {
|
|
162
|
+
if (generatedLine2 > nextEntryLine || generatedLine2 === nextEntryLine && generatedColumn > nextEntryColumn) {
|
|
163
|
+
if (lastMappedLine === nextEntryLine) {
|
|
164
|
+
exportedEntries[nextEntryIdx].originalLine = lastOriginalLine;
|
|
165
|
+
exportedEntries[nextEntryIdx].originalColumn = lastOriginalColumn;
|
|
166
|
+
exportedEntries[nextEntryIdx].originalSource = lastSourceIndex;
|
|
167
|
+
exportedEntries[nextEntryIdx].nameIndex = lastNameIndex;
|
|
168
|
+
}
|
|
169
|
+
nextEntryIdx++;
|
|
170
|
+
if (nextEntryIdx < exportedEntries.length) {
|
|
171
|
+
nextEntryLine = exportedEntries[nextEntryIdx].loc.start.line;
|
|
172
|
+
nextEntryColumn = exportedEntries[nextEntryIdx].loc.start.column;
|
|
173
|
+
} else {
|
|
174
|
+
nextEntryLine = -1;
|
|
175
|
+
nextEntryColumn = -1;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
lastMappedLine = generatedLine2;
|
|
179
|
+
if (sourceIndex > -1) {
|
|
180
|
+
lastSourceIndex = sourceIndex;
|
|
181
|
+
}
|
|
182
|
+
if (originalLine > -1) {
|
|
183
|
+
lastOriginalLine = originalLine;
|
|
184
|
+
}
|
|
185
|
+
if (originalColumn > -1) {
|
|
186
|
+
lastOriginalColumn = originalColumn;
|
|
187
|
+
}
|
|
188
|
+
if (nameIndex > -1) {
|
|
189
|
+
lastNameIndex = nameIndex;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
);
|
|
193
|
+
if (nextEntryIdx < exportedEntries.length) {
|
|
194
|
+
if (lastMappedLine === nextEntryLine) {
|
|
195
|
+
exportedEntries[nextEntryIdx].originalLine = lastOriginalLine;
|
|
196
|
+
exportedEntries[nextEntryIdx].originalColumn = lastOriginalColumn;
|
|
197
|
+
exportedEntries[nextEntryIdx].originalSource = lastSourceIndex;
|
|
198
|
+
exportedEntries[nextEntryIdx].nameIndex = lastNameIndex;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
for (let lastIdx = mappings.length - 1; lastIdx >= 0 && mappings[lastIdx] === ";"; lastIdx--) {
|
|
202
|
+
lastMappedLine++;
|
|
203
|
+
}
|
|
204
|
+
sourceLineCount = program.loc.end.line;
|
|
205
|
+
if (sourceLineCount < lastMappedLine) {
|
|
206
|
+
throw new Error(
|
|
207
|
+
"The source map has more mappings than there are lines."
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
for (let extraLines = sourceLineCount - lastMappedLine; extraLines > 0; extraLines--) {
|
|
211
|
+
mappings += ";";
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
sourceLineCount = 1;
|
|
215
|
+
let idx = -1;
|
|
216
|
+
while ((idx = source.indexOf("\n", idx + 1)) !== -1) {
|
|
217
|
+
sourceLineCount++;
|
|
218
|
+
}
|
|
219
|
+
mappings = "AAAA" + ";AACA".repeat(sourceLineCount - 1);
|
|
220
|
+
sourceMap = new SourceMap({
|
|
221
|
+
version: 3,
|
|
222
|
+
file: basename(url),
|
|
223
|
+
sources: [url],
|
|
224
|
+
sourcesContent: [source],
|
|
225
|
+
names: [],
|
|
226
|
+
mappings,
|
|
227
|
+
sourceRoot: ""
|
|
228
|
+
});
|
|
229
|
+
lastSourceIndex = 0;
|
|
230
|
+
lastOriginalLine = sourceLineCount;
|
|
231
|
+
lastOriginalColumn = 0;
|
|
232
|
+
lastNameIndex = -1;
|
|
233
|
+
lastMappedLine = sourceLineCount;
|
|
234
|
+
for (let i = 0; i < exportedEntries.length; i++) {
|
|
235
|
+
const entry = exportedEntries[i];
|
|
236
|
+
entry.originalSource = 0;
|
|
237
|
+
entry.originalLine = entry.loc.start.line;
|
|
238
|
+
entry.originalColumn = 0;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
newSrc += "\n\n;";
|
|
242
|
+
newSrc += 'import {registerServerReference} from "react-server-dom-esm/server";\n';
|
|
243
|
+
if (mappings) {
|
|
244
|
+
mappings += ";;";
|
|
245
|
+
}
|
|
246
|
+
const createMapping = createMappingsSerializer();
|
|
247
|
+
let generatedLine = 1;
|
|
248
|
+
createMapping(
|
|
249
|
+
generatedLine,
|
|
250
|
+
0,
|
|
251
|
+
lastSourceIndex,
|
|
252
|
+
lastOriginalLine,
|
|
253
|
+
lastOriginalColumn,
|
|
254
|
+
lastNameIndex
|
|
255
|
+
);
|
|
256
|
+
for (let i = 0; i < exportedEntries.length; i++) {
|
|
257
|
+
const entry = exportedEntries[i];
|
|
258
|
+
generatedLine++;
|
|
259
|
+
if (entry.type !== "function") {
|
|
260
|
+
newSrc += "if (typeof " + entry.localName + ' === "function") ';
|
|
261
|
+
}
|
|
262
|
+
newSrc += "registerServerReference(" + entry.localName + ",";
|
|
263
|
+
newSrc += JSON.stringify(url) + ",";
|
|
264
|
+
newSrc += JSON.stringify(entry.exportedName) + ");\n";
|
|
265
|
+
mappings += createMapping(
|
|
266
|
+
generatedLine,
|
|
267
|
+
0,
|
|
268
|
+
entry.originalSource,
|
|
269
|
+
entry.originalLine,
|
|
270
|
+
entry.originalColumn,
|
|
271
|
+
entry.nameIndex
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (sourceMap) {
|
|
276
|
+
sourceMap.mappings = mappings;
|
|
277
|
+
newSrc += "//# sourceMappingURL=data:application/json;charset=utf-8;base64," + Buffer.from(JSON.stringify(sourceMap)).toString("base64");
|
|
278
|
+
}
|
|
279
|
+
if (port) {
|
|
280
|
+
port.postMessage({
|
|
281
|
+
type: "SERVER_MODULE",
|
|
282
|
+
url,
|
|
283
|
+
source: newSrc
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
return newSrc;
|
|
287
|
+
}
|
|
288
|
+
function addExportNames(names, node) {
|
|
289
|
+
switch (node.type) {
|
|
290
|
+
case "Identifier":
|
|
291
|
+
names.push(node.name);
|
|
292
|
+
return;
|
|
293
|
+
case "ObjectPattern":
|
|
294
|
+
for (let i = 0; i < node.properties.length; i++)
|
|
295
|
+
addExportNames(names, node.properties[i]);
|
|
296
|
+
return;
|
|
297
|
+
case "ArrayPattern":
|
|
298
|
+
for (let i = 0; i < node.elements.length; i++) {
|
|
299
|
+
const element = node.elements[i];
|
|
300
|
+
if (element) addExportNames(names, element);
|
|
301
|
+
}
|
|
302
|
+
return;
|
|
303
|
+
case "Property":
|
|
304
|
+
addExportNames(names, node.value);
|
|
305
|
+
return;
|
|
306
|
+
case "AssignmentPattern":
|
|
307
|
+
addExportNames(names, node.left);
|
|
308
|
+
return;
|
|
309
|
+
case "RestElement":
|
|
310
|
+
addExportNames(names, node.argument);
|
|
311
|
+
return;
|
|
312
|
+
case "ParenthesizedExpression":
|
|
313
|
+
addExportNames(names, node.expression);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function resolveClientImport(specifier, parentURL) {
|
|
318
|
+
{
|
|
319
|
+
throw new Error(
|
|
320
|
+
"Expected resolve to have been called before transformSource"
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async function parseExportNamesInto(body, names, parentURL, loader) {
|
|
325
|
+
for (let i = 0; i < body.length; i++) {
|
|
326
|
+
const node = body[i];
|
|
327
|
+
switch (node.type) {
|
|
328
|
+
case "ExportAllDeclaration":
|
|
329
|
+
if (node.exported) {
|
|
330
|
+
addExportNames(names, node.exported);
|
|
331
|
+
continue;
|
|
332
|
+
} else {
|
|
333
|
+
const _await$resolveClientI = await resolveClientImport(
|
|
334
|
+
node.source.value), url = _await$resolveClientI.url;
|
|
335
|
+
const _await$loader = await loader(
|
|
336
|
+
url,
|
|
337
|
+
{
|
|
338
|
+
format: "module",
|
|
339
|
+
conditions: [],
|
|
340
|
+
importAttributes: {}
|
|
341
|
+
},
|
|
342
|
+
loader
|
|
343
|
+
), source = _await$loader.source;
|
|
344
|
+
if (typeof source !== "string") {
|
|
345
|
+
throw new Error("Expected the transformed source to be a string.");
|
|
346
|
+
}
|
|
347
|
+
let childBody;
|
|
348
|
+
try {
|
|
349
|
+
childBody = acorn.parse(source, {
|
|
350
|
+
ecmaVersion: "2024",
|
|
351
|
+
sourceType: "module"
|
|
352
|
+
}).body;
|
|
353
|
+
} catch (x) {
|
|
354
|
+
console.error("Error parsing %s %s", url, x?.message);
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
await parseExportNamesInto(childBody, names, url, loader);
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
case "ExportDefaultDeclaration":
|
|
361
|
+
names.push("default");
|
|
362
|
+
continue;
|
|
363
|
+
case "ExportNamedDeclaration":
|
|
364
|
+
if (node.declaration) {
|
|
365
|
+
if (node.declaration.type === "VariableDeclaration") {
|
|
366
|
+
const declarations = node.declaration.declarations;
|
|
367
|
+
for (let j = 0; j < declarations.length; j++) {
|
|
368
|
+
addExportNames(names, declarations[j].id);
|
|
369
|
+
}
|
|
370
|
+
} else {
|
|
371
|
+
addExportNames(names, node.declaration.id);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (node.specifiers) {
|
|
375
|
+
const specifiers = node.specifiers;
|
|
376
|
+
for (let j = 0; j < specifiers.length; j++) {
|
|
377
|
+
addExportNames(names, specifiers[j].exported);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
async function transformClientModule(program, url, sourceMap, loader) {
|
|
385
|
+
const body = program.body;
|
|
386
|
+
const names = [];
|
|
387
|
+
await parseExportNamesInto(body, names, url, loader);
|
|
388
|
+
if (names.length === 0) {
|
|
389
|
+
console.log("[react-loader] No exports found in:", url);
|
|
390
|
+
return "";
|
|
391
|
+
}
|
|
392
|
+
let newSrc = 'import {registerClientReference} from "react-server-dom-esm/server";\n';
|
|
393
|
+
for (let i = 0; i < names.length; i++) {
|
|
394
|
+
const name = names[i];
|
|
395
|
+
const errorMessage = name === "default" ? `Attempted to call the default export of ${url} from the server but it's on the client` : `Attempted to call ${name}() from the server but ${name} is on the client`;
|
|
396
|
+
const fullError = `${errorMessage}. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.`;
|
|
397
|
+
const browserUrl = url.replace("file://", "").replace(process.cwd(), "");
|
|
398
|
+
if (name === "default") {
|
|
399
|
+
newSrc += `export default registerClientReference(function() { throw new Error(${JSON.stringify(
|
|
400
|
+
fullError
|
|
401
|
+
)}); }, ${JSON.stringify(browserUrl)}, ${JSON.stringify(name)});
|
|
402
|
+
`;
|
|
403
|
+
} else {
|
|
404
|
+
newSrc += `export const ${name} = registerClientReference(function() { throw new Error(${JSON.stringify(
|
|
405
|
+
fullError
|
|
406
|
+
)}); }, ${JSON.stringify(browserUrl)}, ${JSON.stringify(name)});
|
|
407
|
+
`;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return newSrc;
|
|
411
|
+
}
|
|
412
|
+
async function loadClientImport(url, defaultTransformSource) {
|
|
413
|
+
if (stashedGetSource === null) {
|
|
414
|
+
throw new Error(
|
|
415
|
+
"Expected getSource to have been called before transformSource"
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
const _await$stashedGetSour = await stashedGetSource(
|
|
419
|
+
url,
|
|
420
|
+
{
|
|
421
|
+
format: "module"
|
|
422
|
+
},
|
|
423
|
+
stashedGetSource
|
|
424
|
+
), source = _await$stashedGetSour.source;
|
|
425
|
+
const result = await defaultTransformSource(
|
|
426
|
+
source,
|
|
427
|
+
{
|
|
428
|
+
format: "module",
|
|
429
|
+
url
|
|
430
|
+
},
|
|
431
|
+
defaultTransformSource
|
|
432
|
+
);
|
|
433
|
+
return {
|
|
434
|
+
format: "module",
|
|
435
|
+
source: result.source
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
async function transformModuleIfNeeded(source, url, loader, port) {
|
|
439
|
+
if (source.indexOf("use client") === -1 && source.indexOf("use server") === -1) {
|
|
440
|
+
return source;
|
|
441
|
+
}
|
|
442
|
+
let program;
|
|
443
|
+
try {
|
|
444
|
+
program = acorn.parse(source, {
|
|
445
|
+
ecmaVersion: "2024",
|
|
446
|
+
sourceType: "module",
|
|
447
|
+
locations: true
|
|
448
|
+
});
|
|
449
|
+
} catch (x) {
|
|
450
|
+
console.error(
|
|
451
|
+
"[react-loader] Error parsing %s: %s",
|
|
452
|
+
url,
|
|
453
|
+
x?.message
|
|
454
|
+
);
|
|
455
|
+
return source;
|
|
456
|
+
}
|
|
457
|
+
let useClient = false;
|
|
458
|
+
let useServer = false;
|
|
459
|
+
for (const node of program.body) {
|
|
460
|
+
if (node.type !== "ExpressionStatement" || !node.directive) continue;
|
|
461
|
+
if (node.directive === "use client") {
|
|
462
|
+
useClient = true;
|
|
463
|
+
if (port) {
|
|
464
|
+
port.postMessage({
|
|
465
|
+
type: "CLIENT_COMPONENT",
|
|
466
|
+
url,
|
|
467
|
+
source
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
if (node.directive === "use server") {
|
|
473
|
+
useServer = true;
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
if (useClient) {
|
|
478
|
+
return transformClientModule(program, url, undefined, loader);
|
|
479
|
+
} else if (useServer) {
|
|
480
|
+
return transformServerModule(source, program, url, undefined, loader, port);
|
|
481
|
+
}
|
|
482
|
+
return source;
|
|
483
|
+
}
|
|
484
|
+
function readMappings(mappings, callback) {
|
|
485
|
+
let line = 1;
|
|
486
|
+
let column = 0;
|
|
487
|
+
let sourceIndex = 0;
|
|
488
|
+
let originalLine = 0;
|
|
489
|
+
let originalColumn = 0;
|
|
490
|
+
let nameIndex = 0;
|
|
491
|
+
let index = 0;
|
|
492
|
+
while (index < mappings.length) {
|
|
493
|
+
if (mappings[index] === ";") {
|
|
494
|
+
line++;
|
|
495
|
+
column = 0;
|
|
496
|
+
index++;
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
if (mappings[index] === ",") {
|
|
500
|
+
index++;
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
let [
|
|
504
|
+
generatedColumnDelta = 0,
|
|
505
|
+
sourceIndexDelta = 0,
|
|
506
|
+
originalLineDelta = 0,
|
|
507
|
+
originalColumnDelta = 0,
|
|
508
|
+
nameIndexDelta = 0
|
|
509
|
+
] = decodeVLQ(mappings.slice(index));
|
|
510
|
+
column += generatedColumnDelta;
|
|
511
|
+
sourceIndex += sourceIndexDelta;
|
|
512
|
+
originalLine += originalLineDelta;
|
|
513
|
+
originalColumn += originalColumnDelta;
|
|
514
|
+
nameIndex += nameIndexDelta;
|
|
515
|
+
while (index < mappings.length && !/[,;]/.test(mappings[index])) {
|
|
516
|
+
index++;
|
|
517
|
+
}
|
|
518
|
+
callback(
|
|
519
|
+
line,
|
|
520
|
+
column,
|
|
521
|
+
sourceIndex,
|
|
522
|
+
originalLine,
|
|
523
|
+
originalColumn,
|
|
524
|
+
nameIndex
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
function createMappingsSerializer() {
|
|
529
|
+
let previousGeneratedLine = 1;
|
|
530
|
+
let previousGeneratedColumn = 0;
|
|
531
|
+
let previousOriginalFile = 0;
|
|
532
|
+
let previousOriginalLine = 0;
|
|
533
|
+
let previousOriginalColumn = 0;
|
|
534
|
+
let previousNameIndex = 0;
|
|
535
|
+
return function(generatedLine, generatedColumn, originalFile, originalLine, originalColumn, nameIndex) {
|
|
536
|
+
if (generatedLine > previousGeneratedLine) {
|
|
537
|
+
previousGeneratedColumn = 0;
|
|
538
|
+
let lines = "";
|
|
539
|
+
for (let i = previousGeneratedLine; i < generatedLine; i++) {
|
|
540
|
+
lines += ";";
|
|
541
|
+
}
|
|
542
|
+
previousGeneratedLine = generatedLine;
|
|
543
|
+
if (lines) return lines;
|
|
544
|
+
}
|
|
545
|
+
const segment = [
|
|
546
|
+
generatedColumn - previousGeneratedColumn,
|
|
547
|
+
originalFile - previousOriginalFile,
|
|
548
|
+
originalLine - previousOriginalLine,
|
|
549
|
+
originalColumn - previousOriginalColumn
|
|
550
|
+
];
|
|
551
|
+
if (nameIndex >= 0) {
|
|
552
|
+
segment.push(nameIndex - previousNameIndex);
|
|
553
|
+
}
|
|
554
|
+
previousGeneratedColumn = generatedColumn;
|
|
555
|
+
previousOriginalFile = originalFile;
|
|
556
|
+
previousOriginalLine = originalLine;
|
|
557
|
+
previousOriginalColumn = originalColumn;
|
|
558
|
+
previousNameIndex = nameIndex;
|
|
559
|
+
return encodeVLQ(segment) + ",";
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
const VLQ_SHIFT = 5;
|
|
563
|
+
const VLQ_CONTINUATION_BIT = 1 << VLQ_SHIFT;
|
|
564
|
+
const VLQ_VALUE_MASK = VLQ_CONTINUATION_BIT - 1;
|
|
565
|
+
const BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
566
|
+
function encodeVLQ(numbers) {
|
|
567
|
+
return numbers.map((num) => {
|
|
568
|
+
const vlq = num < 0 ? -num << 1 | 1 : num << 1;
|
|
569
|
+
let result = "";
|
|
570
|
+
let value = vlq;
|
|
571
|
+
do {
|
|
572
|
+
let digit = value & VLQ_VALUE_MASK;
|
|
573
|
+
value >>>= VLQ_SHIFT;
|
|
574
|
+
if (value > 0) {
|
|
575
|
+
digit |= VLQ_CONTINUATION_BIT;
|
|
576
|
+
}
|
|
577
|
+
result += BASE64_CHARS[digit];
|
|
578
|
+
} while (value > 0);
|
|
579
|
+
return result;
|
|
580
|
+
}).join("");
|
|
581
|
+
}
|
|
582
|
+
function decodeVLQ(str) {
|
|
583
|
+
const numbers = [];
|
|
584
|
+
let value = 0;
|
|
585
|
+
let shift = 0;
|
|
586
|
+
let index = 0;
|
|
587
|
+
while (index < str.length && !/[,;]/.test(str[index])) {
|
|
588
|
+
const digit = BASE64_CHARS.indexOf(str[index]);
|
|
589
|
+
if (digit === -1) break;
|
|
590
|
+
value += (digit & VLQ_VALUE_MASK) << shift;
|
|
591
|
+
if ((digit & VLQ_CONTINUATION_BIT) === 0) {
|
|
592
|
+
const negate = value & 1;
|
|
593
|
+
value >>>= 1;
|
|
594
|
+
numbers.push(negate ? -value : value);
|
|
595
|
+
value = shift = 0;
|
|
596
|
+
} else {
|
|
597
|
+
shift += VLQ_SHIFT;
|
|
598
|
+
}
|
|
599
|
+
index++;
|
|
600
|
+
}
|
|
601
|
+
return numbers;
|
|
602
|
+
}
|
|
603
|
+
async function initialize(data) {
|
|
604
|
+
loaderPort = data.port;
|
|
605
|
+
data.port.postMessage({ type: "INITIALIZED" });
|
|
606
|
+
data.port.unref();
|
|
607
|
+
}
|
|
608
|
+
async function resolve(specifier, context, nextResolve) {
|
|
609
|
+
return nextResolve(specifier, context);
|
|
610
|
+
}
|
|
611
|
+
async function load(url, context, nextLoad) {
|
|
612
|
+
const result = await nextLoad(url, context);
|
|
613
|
+
if (result.format === "module") {
|
|
614
|
+
const newSrc = await transformModuleIfNeeded(
|
|
615
|
+
result.source,
|
|
616
|
+
url,
|
|
617
|
+
nextLoad,
|
|
618
|
+
loaderPort ?? undefined
|
|
619
|
+
);
|
|
620
|
+
return { ...result, source: newSrc };
|
|
621
|
+
}
|
|
622
|
+
return result;
|
|
623
|
+
}
|
|
624
|
+
async function transformSource(source, context, defaultTransformSource) {
|
|
625
|
+
const transformed = await defaultTransformSource(
|
|
626
|
+
source,
|
|
627
|
+
context,
|
|
628
|
+
defaultTransformSource
|
|
629
|
+
);
|
|
630
|
+
if (context.format === "module") {
|
|
631
|
+
const transformedSource = transformed.source;
|
|
632
|
+
if (typeof transformedSource !== "string") {
|
|
633
|
+
throw new Error("Expected source to have been transformed to a string.");
|
|
634
|
+
}
|
|
635
|
+
const newSrc = await transformModuleIfNeeded(
|
|
636
|
+
transformedSource,
|
|
637
|
+
context.url,
|
|
638
|
+
(url, ctx, defaultLoad) => loadClientImport(url, defaultTransformSource),
|
|
639
|
+
context.data?.port
|
|
640
|
+
);
|
|
641
|
+
return { source: newSrc };
|
|
642
|
+
}
|
|
643
|
+
return transformed;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
export { getSource, initialize, load, resolve, transformModuleIfNeeded, transformSource };
|
|
647
|
+
//# sourceMappingURL=react-loader.js.map
|