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
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
NormalizedOutputOptions,
|
|
3
2
|
OutputBundle,
|
|
4
3
|
PluginContext,
|
|
4
|
+
OutputChunk,
|
|
5
5
|
} from "rollup";
|
|
6
6
|
import { createInputNormalizer } from "./inputNormalizer.js";
|
|
7
7
|
import { join } from "path";
|
|
8
|
+
import { DEFAULT_CONFIG, resolveOptions } from "../config/index.js";
|
|
8
9
|
|
|
9
10
|
interface BundleManifestEntry {
|
|
10
11
|
file: string;
|
|
@@ -23,53 +24,90 @@ interface BundleManifestEntry {
|
|
|
23
24
|
* @param preserveModulesRoot - The preserve modules root
|
|
24
25
|
* @returns The bundle manifest
|
|
25
26
|
*/
|
|
26
|
-
export function getBundleManifest(
|
|
27
|
+
export function getBundleManifest({
|
|
28
|
+
pluginContext,
|
|
29
|
+
bundle,
|
|
30
|
+
moduleBase,
|
|
31
|
+
preserveModulesRoot,
|
|
32
|
+
}: {
|
|
27
33
|
pluginContext: PluginContext,
|
|
28
34
|
bundle: OutputBundle,
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
moduleBase?: string,
|
|
36
|
+
preserveModulesRoot?: boolean,
|
|
37
|
+
}): Record<string, BundleManifestEntry> {
|
|
31
38
|
|
|
32
39
|
const normalizer = createInputNormalizer({
|
|
33
40
|
root: pluginContext.environment.config.root,
|
|
34
|
-
removeExtension:
|
|
35
|
-
preserveModulesRoot:
|
|
36
|
-
typeof preserveModulesRoot === "string" ? preserveModulesRoot : undefined,
|
|
41
|
+
removeExtension: DEFAULT_CONFIG.FILE_REGEX,
|
|
42
|
+
preserveModulesRoot: preserveModulesRoot === true ? moduleBase : undefined,
|
|
37
43
|
});
|
|
38
44
|
|
|
39
|
-
if (!bundle) {
|
|
40
|
-
return {};
|
|
41
|
-
}
|
|
45
|
+
if (!bundle) return {};
|
|
42
46
|
|
|
43
|
-
|
|
47
|
+
// Track virtual modules to prevent duplicates
|
|
48
|
+
const virtualModules = new Map<string, string>();
|
|
49
|
+
|
|
50
|
+
const bundleManifest = Object.fromEntries(
|
|
44
51
|
Object.entries(bundle)
|
|
45
52
|
.map(([fileName, chunk]) => {
|
|
46
53
|
if (chunk.type !== "chunk") return null as never;
|
|
54
|
+
const chunkWithFacade = chunk as OutputChunk;
|
|
55
|
+
|
|
56
|
+
// Get the module ID, preferring facadeModuleId
|
|
57
|
+
const moduleId = chunkWithFacade.facadeModuleId || chunkWithFacade.moduleIds[0] || fileName;
|
|
58
|
+
|
|
59
|
+
// Handle commonjs helpers specially - must be done before normalization
|
|
60
|
+
if (moduleId.includes('commonjsHelpers')) {
|
|
61
|
+
return [
|
|
62
|
+
moduleId,
|
|
63
|
+
{
|
|
64
|
+
file: 'commonjs-runtime.js',
|
|
65
|
+
name: 'commonjsHelpers',
|
|
66
|
+
src: moduleId,
|
|
67
|
+
isEntry: false
|
|
68
|
+
}
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Normalize both paths, removing the root prefix
|
|
73
|
+
const [normalizedId, sourcePath] = normalizer(moduleId);
|
|
47
74
|
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
75
|
+
// For virtual modules, use a consistent naming scheme
|
|
76
|
+
let finalFileName = fileName;
|
|
77
|
+
if (moduleId.includes('?')) {
|
|
78
|
+
const [basePath, query] = moduleId.split('?');
|
|
79
|
+
const virtualPath = basePath.includes('node_modules')
|
|
80
|
+
? basePath.split('node_modules/')[1]
|
|
81
|
+
: basePath;
|
|
82
|
+
|
|
83
|
+
// Create a unique key for this virtual module
|
|
84
|
+
const virtualKey = `${virtualPath}?${query}`;
|
|
85
|
+
|
|
86
|
+
if (!virtualModules.has(virtualKey)) {
|
|
87
|
+
// First time seeing this virtual module
|
|
88
|
+
const virtualFileName = `${virtualPath.replace(/\.js$/, '')}.${query}.js`;
|
|
89
|
+
virtualModules.set(virtualKey, virtualFileName);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
finalFileName = virtualModules.get(virtualKey)!;
|
|
93
|
+
}
|
|
94
|
+
const bundleManifestEntry = [
|
|
95
|
+
sourcePath,
|
|
54
96
|
{
|
|
55
|
-
file:
|
|
56
|
-
name:
|
|
57
|
-
src: sourcePath.startsWith(
|
|
97
|
+
file: finalFileName,
|
|
98
|
+
name: normalizedId.startsWith('\x00') ? normalizedId.replace('\x00', '') : normalizedId,
|
|
99
|
+
src: sourcePath.startsWith('/') ? sourcePath.slice(1) : sourcePath,
|
|
58
100
|
isEntry: chunk.isEntry,
|
|
59
|
-
...(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
: {}),
|
|
65
|
-
...(chunk.viteMetadata?.importedCss
|
|
66
|
-
? {
|
|
67
|
-
css: Array.from(chunk.viteMetadata.importedCss),
|
|
68
|
-
}
|
|
69
|
-
: {}),
|
|
101
|
+
...(chunk.imports?.length > 0 ? { imports: chunk.imports } : {}),
|
|
102
|
+
...(chunk.dynamicImports?.length > 0 ? { dynamicImports: chunk.dynamicImports } : {}),
|
|
103
|
+
...(chunk.viteMetadata?.importedCss?.size ? {
|
|
104
|
+
css: Array.from(chunk.viteMetadata.importedCss),
|
|
105
|
+
} : {}),
|
|
70
106
|
},
|
|
71
107
|
];
|
|
108
|
+
return bundleManifestEntry;
|
|
72
109
|
})
|
|
73
110
|
.filter(Boolean)
|
|
74
111
|
);
|
|
112
|
+
return bundleManifest;
|
|
75
113
|
}
|
|
@@ -7,7 +7,6 @@ type CreateInputNormalizerProps = {
|
|
|
7
7
|
root: string;
|
|
8
8
|
preserveModulesRoot?: string | undefined;
|
|
9
9
|
removeExtension?: boolean | RegExp | string | ((path: string) => boolean);
|
|
10
|
-
moduleBasePath?: string;
|
|
11
10
|
};
|
|
12
11
|
|
|
13
12
|
const resolveExtensionOptions = (
|
|
@@ -48,120 +47,119 @@ const resolveRootOption = (
|
|
|
48
47
|
root: CreateInputNormalizerProps["root"],
|
|
49
48
|
preserveModulesRoot: CreateInputNormalizerProps["preserveModulesRoot"]
|
|
50
49
|
) => {
|
|
51
|
-
if (typeof root !== "string" && typeof preserveModulesRoot !== "string") {
|
|
52
|
-
return "";
|
|
53
|
-
}
|
|
54
50
|
if (typeof preserveModulesRoot === "string" && typeof root === "string") {
|
|
55
|
-
const normalizedRoot = normalizePath(root);
|
|
56
|
-
const normalizedPreserveModulesRoot = normalizePath(preserveModulesRoot);
|
|
57
|
-
if (normalizedPreserveModulesRoot.startsWith(normalizedRoot)) {
|
|
58
|
-
return join(normalizedPreserveModulesRoot, "/");
|
|
59
|
-
}
|
|
60
|
-
return join(normalizedRoot, normalizedPreserveModulesRoot, "/");
|
|
61
|
-
}
|
|
62
|
-
if (typeof preserveModulesRoot === "string" && typeof root !== "string") {
|
|
63
|
-
const normalizedRoot = normalizePath(root);
|
|
64
51
|
const normalizedPreserveModulesRoot = normalizePath(preserveModulesRoot);
|
|
65
|
-
if (
|
|
66
|
-
return
|
|
52
|
+
if (normalizedPreserveModulesRoot.startsWith(root)) {
|
|
53
|
+
return normalizedPreserveModulesRoot.slice(root.length + 1);
|
|
67
54
|
}
|
|
68
|
-
return
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const normalizedRoot = normalizePath(root);
|
|
72
|
-
return join(normalizedRoot, "/");
|
|
55
|
+
return "";
|
|
56
|
+
} else if (typeof preserveModulesRoot === "string" && typeof root !== "string") {
|
|
57
|
+
return normalizePath(preserveModulesRoot);
|
|
73
58
|
}
|
|
74
|
-
return "
|
|
59
|
+
return "";
|
|
75
60
|
};
|
|
76
61
|
const createKeyNormalizer =
|
|
77
62
|
({
|
|
78
|
-
normalizedRoot,
|
|
63
|
+
root: normalizedRoot,
|
|
79
64
|
preserveModulesRoot,
|
|
80
65
|
handleExtension,
|
|
81
66
|
}: {
|
|
82
|
-
|
|
67
|
+
root: string;
|
|
83
68
|
preserveModulesRoot: string | undefined;
|
|
84
69
|
handleExtension: (path: string) => string;
|
|
85
70
|
}) =>
|
|
86
71
|
(key: string) => {
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
72
|
+
// Handle virtual modules first
|
|
73
|
+
const virtualPrefix = key.match(/^\0+/)?.[0] || '';
|
|
74
|
+
const actualKey = key.slice(virtualPrefix.length);
|
|
75
|
+
|
|
76
|
+
let moduleId = normalizePath(actualKey);
|
|
77
|
+
|
|
78
|
+
if (moduleId.startsWith(normalizedRoot)) {
|
|
79
|
+
moduleId = moduleId.slice(normalizedRoot.length);
|
|
80
|
+
}
|
|
92
81
|
|
|
93
|
-
|
|
94
|
-
while (
|
|
95
|
-
|
|
96
|
-
noRelativeNorAbsolute.startsWith(".")
|
|
97
|
-
) {
|
|
98
|
-
noRelativeNorAbsolute = noRelativeNorAbsolute.slice(1);
|
|
82
|
+
moduleId = handleExtension(moduleId);
|
|
83
|
+
while (moduleId.startsWith("/") || moduleId.startsWith(".")) {
|
|
84
|
+
moduleId = moduleId.slice(1);
|
|
99
85
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
preserveModulesRoot
|
|
103
|
-
)
|
|
104
|
-
? noRelativeNorAbsolute.slice(preserveModulesRoot.length)
|
|
105
|
-
: noRelativeNorAbsolute;
|
|
106
|
-
return removedModulesRoot.startsWith(normalizedRoot)
|
|
107
|
-
? removedModulesRoot.slice(normalizedRoot.length)
|
|
108
|
-
: removedModulesRoot;
|
|
86
|
+
while (moduleId.endsWith("/")) {
|
|
87
|
+
moduleId = moduleId.slice(0, -1);
|
|
109
88
|
}
|
|
110
|
-
|
|
89
|
+
if (typeof preserveModulesRoot === "string" && preserveModulesRoot !== "") {
|
|
90
|
+
moduleId = moduleId.startsWith(preserveModulesRoot)
|
|
91
|
+
? moduleId.slice(preserveModulesRoot.length)
|
|
92
|
+
: moduleId;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Add virtual prefix back
|
|
96
|
+
return virtualPrefix + moduleId;
|
|
111
97
|
};
|
|
112
98
|
|
|
113
99
|
const createPathNormalizer =
|
|
114
|
-
({
|
|
100
|
+
({
|
|
101
|
+
root,
|
|
102
|
+
preserveModulesRoot,
|
|
103
|
+
}: {
|
|
104
|
+
root: string;
|
|
105
|
+
preserveModulesRoot: string | undefined;
|
|
106
|
+
}) =>
|
|
115
107
|
(path: string) => {
|
|
116
|
-
if(typeof path !==
|
|
108
|
+
if (typeof path !== "string") {
|
|
117
109
|
throw new Error(`Invalid path: ${JSON.stringify(path)}`);
|
|
118
110
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
111
|
+
let normalPath = normalizePath(path);
|
|
112
|
+
if(normalPath.startsWith(root)) {
|
|
113
|
+
normalPath = normalPath.slice(root.length);
|
|
114
|
+
}
|
|
115
|
+
if (typeof preserveModulesRoot === "string" && preserveModulesRoot !== "") {
|
|
116
|
+
normalPath = normalPath.startsWith(preserveModulesRoot)
|
|
117
|
+
? normalPath.slice(preserveModulesRoot.length)
|
|
118
|
+
: normalPath;
|
|
126
119
|
}
|
|
127
|
-
|
|
120
|
+
while (normalPath.endsWith("/")) {
|
|
121
|
+
normalPath = normalPath.slice(0, -1);
|
|
122
|
+
}
|
|
123
|
+
return normalPath;
|
|
128
124
|
};
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
125
|
+
/**
|
|
126
|
+
* @description Create a function that normalizes the input
|
|
127
|
+
* @param root - The root of the project
|
|
128
|
+
* @param preserveModulesRoot - The root of the preserve modules
|
|
129
|
+
* @param removeExtension - Whether to remove the extension of the file
|
|
130
|
+
* @returns A function that normalizes the input
|
|
131
|
+
*/
|
|
136
132
|
export function createInputNormalizer({
|
|
137
133
|
root,
|
|
138
134
|
preserveModulesRoot = undefined,
|
|
139
135
|
removeExtension = DEFAULT_CONFIG.FILE_REGEX,
|
|
140
|
-
moduleBasePath = "/",
|
|
141
136
|
}: CreateInputNormalizerProps): InputNormalizer {
|
|
142
|
-
const
|
|
137
|
+
const relativeRoot = resolveRootOption(root, preserveModulesRoot);
|
|
143
138
|
const handleExtension = resolveExtensionOptions(removeExtension);
|
|
144
139
|
|
|
145
140
|
const normalizeEntryKey = createKeyNormalizer({
|
|
146
|
-
|
|
147
|
-
preserveModulesRoot,
|
|
141
|
+
root: root,
|
|
142
|
+
preserveModulesRoot: relativeRoot,
|
|
148
143
|
handleExtension,
|
|
149
144
|
});
|
|
150
145
|
|
|
151
|
-
const normalizeEntryPath = createPathNormalizer({
|
|
152
|
-
|
|
146
|
+
const normalizeEntryPath = createPathNormalizer({
|
|
147
|
+
root: root,
|
|
148
|
+
preserveModulesRoot: relativeRoot,
|
|
149
|
+
});
|
|
150
|
+
function normalizeInput(id: NormalizerInput): [string, string] {
|
|
153
151
|
// Normalize both paths to use POSIX separators
|
|
154
152
|
if (Array.isArray(id)) {
|
|
155
153
|
const [key, path] = id;
|
|
156
|
-
if(typeof key ===
|
|
154
|
+
if (typeof key === "string" && Array.isArray(path) && path.length === 2) {
|
|
157
155
|
const isNumber = !isNaN(Number(key));
|
|
158
|
-
if(isNumber) {
|
|
156
|
+
if (isNumber) {
|
|
159
157
|
// ignore it
|
|
160
158
|
return normalizeInput([path[0], path[1]]);
|
|
161
159
|
}
|
|
162
160
|
return normalizeInput([join(key, path[0]), path[1]]);
|
|
163
161
|
}
|
|
164
|
-
if(typeof key !==
|
|
162
|
+
if (typeof key !== "string" || typeof path !== "string") {
|
|
165
163
|
throw new Error(`Invalid input: ${JSON.stringify(id)}`);
|
|
166
164
|
}
|
|
167
165
|
return [normalizeEntryKey(key), normalizeEntryPath(path)];
|
|
@@ -183,4 +181,18 @@ export function createInputNormalizer({
|
|
|
183
181
|
}
|
|
184
182
|
throw new Error(`Invalid input type: ${typeof id}`);
|
|
185
183
|
};
|
|
184
|
+
|
|
185
|
+
return (input: NormalizerInput): [string, string] => {
|
|
186
|
+
const [key, path] = normalizeInput(input);
|
|
187
|
+
// Apply the same normalization to both key and path
|
|
188
|
+
const normalizedPath = path.startsWith('/') ? path.slice(1) : path;
|
|
189
|
+
const virtualPrefix = key.match(/^\0+/)?.[0] || '';
|
|
190
|
+
|
|
191
|
+
// If key has virtual prefix, ensure path has it too
|
|
192
|
+
const finalPath = virtualPrefix ?
|
|
193
|
+
(normalizedPath.startsWith(virtualPrefix) ? normalizedPath : virtualPrefix + normalizedPath) :
|
|
194
|
+
normalizedPath;
|
|
195
|
+
|
|
196
|
+
return [key, finalPath];
|
|
197
|
+
};
|
|
186
198
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { join, resolve } from "node:path";
|
|
1
|
+
import { join, relative, resolve } from "node:path";
|
|
2
2
|
import type { PluginContext } from "rollup";
|
|
3
3
|
import type { ResolvedUserConfig, ResolvedUserOptions } from "../../server.js";
|
|
4
4
|
import type { Manifest } from "vite";
|
|
@@ -14,54 +14,51 @@ export interface BuildLoaderOptions {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export function createBuildLoader({
|
|
17
|
+
root,
|
|
17
18
|
userConfig,
|
|
18
19
|
userOptions,
|
|
19
20
|
pluginContext,
|
|
20
|
-
clientManifest,
|
|
21
21
|
serverManifest,
|
|
22
|
+
clientManifest,
|
|
22
23
|
}: BuildLoaderOptions) {
|
|
23
|
-
const root = pluginContext.environment.config.root ?? userConfig.root ?? userOptions.projectRoot ?? process.cwd();
|
|
24
24
|
const normalizer = createInputNormalizer({
|
|
25
|
-
root
|
|
26
|
-
preserveModulesRoot:
|
|
27
|
-
|
|
28
|
-
? userOptions.moduleBase
|
|
29
|
-
: undefined,
|
|
25
|
+
root,
|
|
26
|
+
preserveModulesRoot: undefined,
|
|
27
|
+
removeExtension: false,
|
|
30
28
|
});
|
|
31
|
-
|
|
32
29
|
return async function buildLoader(id: string) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
30
|
+
const [key, value] = normalizer(id);
|
|
31
|
+
// Remove leading slash if present
|
|
32
|
+
const distDir = userOptions.build.outDir;
|
|
33
|
+
const manifests = [clientManifest, serverManifest];
|
|
34
|
+
// Try to find the module in the manifest
|
|
35
|
+
for (const n of [0, 1]) {
|
|
36
|
+
const manifest = manifests[n];
|
|
37
|
+
const manifestEntry = manifest[key]
|
|
38
|
+
if (!manifestEntry) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const isClient = userOptions.autoDiscover.clientComponents(id);
|
|
42
|
+
const isServer = userOptions.autoDiscover.serverFunctions(id);
|
|
43
|
+
const outDir = isClient
|
|
44
|
+
? userOptions.build.client
|
|
45
|
+
: isServer
|
|
46
|
+
? userOptions.build.server
|
|
47
|
+
: n === 0
|
|
48
|
+
? userOptions.build.client
|
|
49
|
+
: userOptions.build.server;
|
|
50
|
+
if (manifestEntry.file.startsWith(`${root}/${distDir}/${outDir}/`)) {
|
|
51
|
+
return import(manifestEntry.file);
|
|
52
|
+
}
|
|
53
|
+
if (manifestEntry.file.startsWith(`${distDir}/`)) {
|
|
54
|
+
return import(join(root, manifestEntry.file));
|
|
55
|
+
}
|
|
56
|
+
if (manifestEntry.file.startsWith(`${outDir}/`)) {
|
|
57
|
+
return import(join(root, distDir, outDir, manifestEntry.file));
|
|
58
|
+
}
|
|
59
|
+
// Load the module
|
|
60
|
+
return import(join(root, distDir, outDir, manifestEntry.file));
|
|
48
61
|
}
|
|
49
|
-
|
|
50
|
-
const serverEntry = Object.entries(serverManifest).find(([key]) => {
|
|
51
|
-
const [keyNormalized] = normalizer(key);
|
|
52
|
-
return keyNormalized === normalizedId;
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
if (serverEntry) {
|
|
56
|
-
const module = await import(
|
|
57
|
-
join(root, serverEntry[1].file)
|
|
58
|
-
);
|
|
59
|
-
return module;
|
|
60
|
-
}
|
|
61
|
-
const availableModules = [...Object.keys(clientManifest), ...Object.keys(serverManifest)]
|
|
62
|
-
|
|
63
|
-
throw new Error(
|
|
64
|
-
`Module not found: ${id}, available modules: ${availableModules.join(", ")}`
|
|
65
|
-
);
|
|
62
|
+
throw new Error(`Module not found: ${id}`);
|
|
66
63
|
};
|
|
67
64
|
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
import { basename } from 'path';
|
|
3
|
+
import postcss from 'postcss';
|
|
4
|
+
import type { MessagePort } from 'node:worker_threads';
|
|
5
|
+
import type { CssFileMessage } from '../worker/types.js';
|
|
6
|
+
import type { LoadHookContext } from 'node:module';
|
|
7
|
+
|
|
8
|
+
type LoaderContext = {
|
|
9
|
+
data?: { port: MessagePort };
|
|
10
|
+
};
|
|
11
|
+
// Store port globally for CSS loader
|
|
12
|
+
let cssLoaderPort: MessagePort | undefined;
|
|
13
|
+
|
|
14
|
+
// Initialize hook
|
|
15
|
+
export async function initialize(data: { port: MessagePort }) {
|
|
16
|
+
cssLoaderPort = data.port; // Store port
|
|
17
|
+
data.port.postMessage({ type: 'INITIALIZED' });
|
|
18
|
+
data.port.unref();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// CSS file tracking per page
|
|
22
|
+
const cssFilesByPage = new Map<string, Set<string>>();
|
|
23
|
+
let currentPage: string | null = null;
|
|
24
|
+
|
|
25
|
+
export function setCurrentPage(page: string | null) {
|
|
26
|
+
currentPage = page;
|
|
27
|
+
if (page && !cssFilesByPage.has(page)) {
|
|
28
|
+
cssFilesByPage.set(page, new Set());
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function getCssFilesForPage(page: string): string[] {
|
|
33
|
+
return Array.from(cssFilesByPage.get(page) || []);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Modify the CSS handling in the load function
|
|
37
|
+
export async function load(url: string, context: LoadHookContext & LoaderContext, defaultLoad: any) {
|
|
38
|
+
// Handle CSS files
|
|
39
|
+
if(url.endsWith(".css")) {
|
|
40
|
+
const source = await readFile(new URL(url), 'utf-8');
|
|
41
|
+
|
|
42
|
+
// Process CSS with PostCSS
|
|
43
|
+
const result = await postcss().process(source, {
|
|
44
|
+
from: url,
|
|
45
|
+
to: url,
|
|
46
|
+
map: {
|
|
47
|
+
inline: false,
|
|
48
|
+
annotation: false
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Generate both transformed CSS and class mappings
|
|
53
|
+
const moduleName = basename(url, '.css').replace('.', '_');
|
|
54
|
+
const classes: Record<string, string> = {};
|
|
55
|
+
|
|
56
|
+
// Transform selectors
|
|
57
|
+
result.root.walkRules(rule => {
|
|
58
|
+
const selector = rule.selector.replace('.', '');
|
|
59
|
+
const className = `${moduleName}_${selector}`;
|
|
60
|
+
classes[selector] = className;
|
|
61
|
+
rule.selector = `.${className}`;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Get transformed CSS using root.toString()
|
|
65
|
+
const transformedCss = result.root.toString();
|
|
66
|
+
|
|
67
|
+
// Send processed CSS to worker
|
|
68
|
+
if (cssLoaderPort) {
|
|
69
|
+
cssLoaderPort.postMessage({
|
|
70
|
+
type: 'CSS_FILE',
|
|
71
|
+
id: url,
|
|
72
|
+
cssFile: transformedCss
|
|
73
|
+
} satisfies CssFileMessage);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Return CSS module
|
|
77
|
+
const moduleSource = `
|
|
78
|
+
const styles = ${JSON.stringify(classes)};
|
|
79
|
+
export default styles;
|
|
80
|
+
export const css = ${JSON.stringify(transformedCss)};
|
|
81
|
+
`;
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
format: 'module',
|
|
85
|
+
source: moduleSource,
|
|
86
|
+
shortCircuit: true
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return defaultLoad(url, context, defaultLoad);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function resolve(specifier: string, context: any, defaultResolve: any) {
|
|
93
|
+
return defaultResolve(specifier, context, defaultResolve);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|