@eserstack/laroux-bundler 0.0.0-stage → 5.0.0

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.
Files changed (103) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +431 -2
  3. package/dist/adapters/fonts/font-optimizer.d.ts +28 -0
  4. package/dist/adapters/fonts/font-optimizer.js +74 -0
  5. package/dist/adapters/fonts/index.d.ts +18 -0
  6. package/dist/adapters/fonts/index.js +78 -0
  7. package/dist/adapters/fonts/mod.d.ts +6 -0
  8. package/dist/adapters/fonts/mod.js +7 -0
  9. package/dist/adapters/lightningcss/css-processor.d.ts +39 -0
  10. package/dist/adapters/lightningcss/css-processor.js +103 -0
  11. package/dist/adapters/lightningcss/mod.d.ts +5 -0
  12. package/dist/adapters/lightningcss/mod.js +6 -0
  13. package/dist/adapters/mod.d.ts +13 -0
  14. package/dist/adapters/mod.js +21 -0
  15. package/dist/adapters/prebuilt-bundler/mod.d.ts +5 -0
  16. package/dist/adapters/prebuilt-bundler/mod.js +6 -0
  17. package/dist/adapters/prebuilt-bundler/prebuilt-bundler.d.ts +22 -0
  18. package/dist/adapters/prebuilt-bundler/prebuilt-bundler.js +60 -0
  19. package/dist/adapters/react/mod.d.ts +10 -0
  20. package/dist/adapters/react/mod.js +16 -0
  21. package/dist/adapters/react/plugin.d.ts +17 -0
  22. package/dist/adapters/react/plugin.js +62 -0
  23. package/dist/adapters/react/rsc-analyze.d.ts +29 -0
  24. package/dist/adapters/react/rsc-analyze.js +116 -0
  25. package/dist/adapters/react/rsc-client-entry.d.ts +26 -0
  26. package/dist/adapters/react/rsc-client-entry.js +145 -0
  27. package/dist/adapters/react/rsc-module-map.d.ts +41 -0
  28. package/dist/adapters/react/rsc-module-map.js +81 -0
  29. package/dist/adapters/react/rsc-rewrite-imports.d.ts +38 -0
  30. package/dist/adapters/react/rsc-rewrite-imports.js +409 -0
  31. package/dist/adapters/react/rsc-transform.d.ts +26 -0
  32. package/dist/adapters/react/rsc-transform.js +101 -0
  33. package/dist/adapters/runtime-bundler/mod.d.ts +6 -0
  34. package/dist/adapters/runtime-bundler/mod.js +6 -0
  35. package/dist/adapters/runtime-bundler/runtime-bundler.d.ts +85 -0
  36. package/dist/adapters/runtime-bundler/runtime-bundler.js +244 -0
  37. package/dist/adapters/sharp/image-optimizer.d.ts +52 -0
  38. package/dist/adapters/sharp/image-optimizer.js +261 -0
  39. package/dist/adapters/sharp/mod.d.ts +6 -0
  40. package/dist/adapters/sharp/mod.js +6 -0
  41. package/dist/adapters/tailwindcss/compile.d.ts +22 -0
  42. package/dist/adapters/tailwindcss/compile.js +29 -0
  43. package/dist/adapters/tailwindcss/critical-css-config.d.ts +46 -0
  44. package/dist/adapters/tailwindcss/critical-css-config.js +78 -0
  45. package/dist/adapters/tailwindcss/critical-page-css.d.ts +61 -0
  46. package/dist/adapters/tailwindcss/critical-page-css.js +431 -0
  47. package/dist/adapters/tailwindcss/critical-universal-css.d.ts +68 -0
  48. package/dist/adapters/tailwindcss/critical-universal-css.js +250 -0
  49. package/dist/adapters/tailwindcss/mod.d.ts +10 -0
  50. package/dist/adapters/tailwindcss/mod.js +11 -0
  51. package/dist/adapters/tailwindcss/plugin.d.ts +16 -0
  52. package/dist/adapters/tailwindcss/plugin.js +107 -0
  53. package/dist/adapters/tailwindcss/stylesheet-loader.d.ts +15 -0
  54. package/dist/adapters/tailwindcss/stylesheet-loader.js +83 -0
  55. package/dist/config.d.ts +27 -0
  56. package/dist/config.js +31 -0
  57. package/dist/css-modules.d.ts +49 -0
  58. package/dist/css-modules.js +146 -0
  59. package/dist/domain/build-cache.d.ts +107 -0
  60. package/dist/domain/build-cache.js +128 -0
  61. package/dist/domain/build-validator.d.ts +68 -0
  62. package/dist/domain/build-validator.js +207 -0
  63. package/dist/domain/bundler.d.ts +136 -0
  64. package/dist/domain/bundler.js +225 -0
  65. package/dist/domain/chunk-manifest.d.ts +40 -0
  66. package/dist/domain/chunk-manifest.js +188 -0
  67. package/dist/domain/client-action-stub.d.ts +30 -0
  68. package/dist/domain/client-action-stub.js +167 -0
  69. package/dist/domain/copy-path.d.ts +15 -0
  70. package/dist/domain/copy-path.js +29 -0
  71. package/dist/domain/css-plugin.d.ts +91 -0
  72. package/dist/domain/css-plugin.js +6 -0
  73. package/dist/domain/css-processor.d.ts +57 -0
  74. package/dist/domain/css-processor.js +33 -0
  75. package/dist/domain/font-optimizer.d.ts +46 -0
  76. package/dist/domain/font-optimizer.js +17 -0
  77. package/dist/domain/framework-plugin.d.ts +106 -0
  78. package/dist/domain/framework-plugin.js +20 -0
  79. package/dist/domain/image-optimizer.d.ts +74 -0
  80. package/dist/domain/image-optimizer.js +16 -0
  81. package/dist/domain/import-map.d.ts +90 -0
  82. package/dist/domain/import-map.js +187 -0
  83. package/dist/domain/mod.d.ts +32 -0
  84. package/dist/domain/mod.js +20 -0
  85. package/dist/domain/route-generator.d.ts +34 -0
  86. package/dist/domain/route-generator.js +305 -0
  87. package/dist/domain/route-scanner.d.ts +72 -0
  88. package/dist/domain/route-scanner.js +282 -0
  89. package/dist/domain/server-action-transform.d.ts +28 -0
  90. package/dist/domain/server-action-transform.js +147 -0
  91. package/dist/domain/virtual-source.d.ts +63 -0
  92. package/dist/domain/virtual-source.js +238 -0
  93. package/dist/import-map-resolver-plugin.d.ts +47 -0
  94. package/dist/import-map-resolver-plugin.js +109 -0
  95. package/dist/mod.d.ts +57 -0
  96. package/dist/mod.js +56 -0
  97. package/dist/server-externals-plugin.d.ts +29 -0
  98. package/dist/server-externals-plugin.js +63 -0
  99. package/dist/system.d.ts +157 -0
  100. package/dist/system.js +1401 -0
  101. package/dist/types.d.ts +60 -0
  102. package/dist/types.js +38 -0
  103. package/package.json +83 -3
@@ -0,0 +1,145 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ /**
3
+ * Create Client Entry Point
4
+ * Generates a temporary entry file that includes:
5
+ * 1. All client components (so they're in the bundle)
6
+ * 2. The bootstrap code from @eserstack/laroux-react/client/bootstrap
7
+ * 3. A global module registry for RSC client
8
+ */
9
+ import { runtime } from "@eserstack/standards/cross-runtime";
10
+ /** The laroux-react export that locates the package root. */
11
+ const BOOTSTRAP_EXPORT = "@eserstack/laroux-react/client/bootstrap";
12
+ /** What BOOTSTRAP_EXPORT resolves to, below the package root. */
13
+ const BOOTSTRAP_EXPORT_SUFFIX = /\/client\/bootstrap(?:\/index\.ts)?$/;
14
+ /** Directory under distDir that holds the copied laroux-react files. */
15
+ export const BOOTSTRAP_COPY_DIR = "_laroux-react";
16
+ /** The bootstrap module the generated entry imports, inside the copy. */
17
+ const BOOTSTRAP_ENTRY_FILE = "client/bootstrap/entry.ts";
18
+ /**
19
+ * laroux-react files the client entry bundles, relative to the package root.
20
+ *
21
+ * They are copied with this layout so the relative imports between them
22
+ * (entry.ts imports ../../client.ts) resolve inside the copy. The copy sits
23
+ * in the app's dist directory instead of being bundled from the package
24
+ * location so that bare imports such as react resolve from the app, and the
25
+ * bootstrap and the app's client components share one React instance. The
26
+ * app therefore needs the packages these files import by bare specifier:
27
+ * react, react-dom and @eserstack/laroux (@eserstack/logging is replaced by
28
+ * its browser shim).
29
+ *
30
+ * A file that joins this import graph must be listed here;
31
+ * rsc-client-entry.test.ts bundles the entry and fails on a missing one.
32
+ */
33
+ const BOOTSTRAP_SOURCE_FILES = [
34
+ "client.ts",
35
+ "protocol.ts",
36
+ "runtime/image/fallback.ts",
37
+ "runtime/navigation/link-navigation.ts",
38
+ "client/bootstrap/entry.ts",
39
+ "client/bootstrap/error-boundary.ts",
40
+ "client/bootstrap/error-overlay.ts",
41
+ "client/bootstrap/globals.d.ts",
42
+ "client/bootstrap/hmr-client.ts",
43
+ "client/bootstrap/index.ts",
44
+ "client/bootstrap/lazy-loader.ts",
45
+ "client/bootstrap/smart-refresh.ts",
46
+ ];
47
+ /** A file URL pathname of a Windows path, such as /C:/dir. */
48
+ const WINDOWS_DRIVE_PATHNAME = /^\/[A-Za-z]:\//;
49
+ /**
50
+ * Convert a file:// URL to a filesystem path, decoding percent-escapes such
51
+ * as %20, and return any other string unchanged.
52
+ */
53
+ export function fileUrlToPath(urlString) {
54
+ if (!urlString.startsWith("file://")) {
55
+ return urlString;
56
+ }
57
+ const pathname = decodeURIComponent(new URL(urlString).pathname);
58
+ return WINDOWS_DRIVE_PATHNAME.test(pathname) ? pathname.slice(1) : pathname;
59
+ }
60
+ /**
61
+ * Locate the laroux-react package root, which node_modules holds for the
62
+ * bundler as one of its dependencies.
63
+ *
64
+ * @returns Absolute path of the package root
65
+ */
66
+ function getPackageRoot() {
67
+ const resolved = import.meta.resolve(BOOTSTRAP_EXPORT);
68
+ if (!resolved.startsWith("file://")) {
69
+ throw new Error(`locate the laroux-react package root: ${BOOTSTRAP_EXPORT} resolved to ${resolved}, which is not a file in node_modules`);
70
+ }
71
+ const bootstrapPath = fileUrlToPath(resolved).replaceAll("\\", "/");
72
+ const packageRoot = bootstrapPath.replace(BOOTSTRAP_EXPORT_SUFFIX, "");
73
+ if (packageRoot === bootstrapPath) {
74
+ throw new Error(`locate the laroux-react package root: ${BOOTSTRAP_EXPORT} resolved to ${resolved}, which does not end in /client/bootstrap/index.ts`);
75
+ }
76
+ return packageRoot;
77
+ }
78
+ /**
79
+ * Copy the bootstrap files into copyDir, keeping their package layout.
80
+ * The file list is fixed and small, so the copies run concurrently.
81
+ */
82
+ async function copyBootstrapFiles(copyDir) {
83
+ const packageRoot = getPackageRoot();
84
+ await Promise.all(BOOTSTRAP_SOURCE_FILES.map(async (relativePath) => {
85
+ const destPath = runtime.path.resolve(copyDir, relativePath);
86
+ await runtime.fs.ensureDir(runtime.path.dirname(destPath));
87
+ await runtime.fs.copyFile(runtime.path.resolve(packageRoot, relativePath), destPath);
88
+ }));
89
+ }
90
+ /**
91
+ * Write the client entry for a bundle: it imports every client component,
92
+ * registers it in the global module registry and runs the laroux-react
93
+ * bootstrap, whose files are copied to `<distDir>/_laroux-react/`.
94
+ *
95
+ * @param clientComponents - The app's "use client" components
96
+ * @param _projectRoot - Unused; kept for the FrameworkPlugin signature
97
+ * @param distDir - Directory that receives the entry and the bootstrap copy
98
+ * @returns Absolute path of the generated entry file
99
+ */
100
+ export async function createClientEntry(clientComponents, _projectRoot, distDir) {
101
+ const entryPath = runtime.path.resolve(distDir, "_client-entry.tsx");
102
+ await runtime.fs.ensureDir(distDir);
103
+ // Copy the bootstrap files to dist, keeping their relative imports intact
104
+ await copyBootstrapFiles(runtime.path.resolve(distDir, BOOTSTRAP_COPY_DIR));
105
+ // Generate import statements for all client components
106
+ const componentImports = clientComponents
107
+ .map((component, index) => {
108
+ // Use the absolute path to the component file
109
+ return `import * as __component_${index} from "${component.filePath}";`;
110
+ })
111
+ .join("\n");
112
+ // Generate registry assignments for all client components
113
+ const componentRegistry = clientComponents
114
+ .map((component, index) => {
115
+ // Register using the relative path as the key (matching module map)
116
+ // Use ./ prefix to match the module IDs sent by the server
117
+ const moduleId = `./${component.relativePath}`;
118
+ return ` __RUNTIME_MODULES__["${moduleId}"] = __component_${index};`;
119
+ })
120
+ .join("\n");
121
+ // Create the entry file content that imports components and bootstrap code
122
+ const content = `/**
123
+ * Auto-generated Client Entry Point - Runtime Bundle
124
+ * This file contains the bootstrap code and all client components
125
+ */
126
+
127
+ ${componentImports}
128
+
129
+ // Create global module registry for runtime components
130
+ if (typeof globalThis !== "undefined") {
131
+ globalThis.__RUNTIME_MODULES__ = globalThis.__RUNTIME_MODULES__ ?? {};
132
+ }
133
+ const __RUNTIME_MODULES__ = typeof globalThis !== "undefined"
134
+ ? globalThis.__RUNTIME_MODULES__
135
+ : {};
136
+
137
+ // Register all client components
138
+ ${componentRegistry}
139
+
140
+ // Import and run the bootstrap code (copied to dist/${BOOTSTRAP_COPY_DIR}/)
141
+ import "./${BOOTSTRAP_COPY_DIR}/${BOOTSTRAP_ENTRY_FILE}";
142
+ `;
143
+ await runtime.fs.writeTextFile(entryPath, content);
144
+ return entryPath;
145
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Module Map Generator
3
+ * Creates the module map required by react-server-dom-esm to resolve client components
4
+ *
5
+ * Uses @eserstack/bundler for base types and utilities.
6
+ */
7
+ import type { ModuleEntry as BaseModuleEntry, ModuleMap as BaseModuleMap } from "@eserstack/bundler";
8
+ import type { ClientComponent } from "./rsc-analyze.ts";
9
+ export type { BaseModuleEntry, BaseModuleMap };
10
+ export type ModuleMapEntry = {
11
+ /** Module ID/path for the client component */
12
+ id: string;
13
+ /** Chunk names that contain this component */
14
+ chunks: string[];
15
+ /** Export name (default, named export, or * for all exports) */
16
+ name: string;
17
+ };
18
+ export type ModuleMap = Record<string, ModuleMapEntry>;
19
+ /**
20
+ * Generate a module map from analyzed client components
21
+ * Uses relative paths that match React's module reference format
22
+ */
23
+ export declare function generateModuleMap(clientComponents: ClientComponent[]): ModuleMap;
24
+ /**
25
+ * Convert module map to the format expected by react-server-dom-esm bundler config
26
+ */
27
+ export declare function createBundlerConfig(moduleMap: ModuleMap): ModuleMap;
28
+ /**
29
+ * Save module map to a JSON file
30
+ */
31
+ export declare function saveModuleMap(moduleMap: ModuleMap, outputPath: string): Promise<void>;
32
+ /**
33
+ * Load module map from a JSON file
34
+ */
35
+ export declare function loadModuleMap(filePath: string): Promise<ModuleMap>;
36
+ /**
37
+ * Create a client manifest for the browser
38
+ * This is used by the client to know which components are available
39
+ * Uses ES Module paths for standards compliance
40
+ */
41
+ export declare function createClientManifest(clientComponents: ClientComponent[]): ModuleMap;
@@ -0,0 +1,81 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ /**
3
+ * Module Map Generator
4
+ * Creates the module map required by react-server-dom-esm to resolve client components
5
+ *
6
+ * Uses @eserstack/bundler for base types and utilities.
7
+ */
8
+ import { runtime } from "@eserstack/standards/cross-runtime";
9
+ import * as patterns from "@eserstack/standards/patterns";
10
+ import * as logging from "@eserstack/logging";
11
+ const moduleMapLogger = logging.logger.getLogger([
12
+ "laroux-bundler",
13
+ "rsc-module-map",
14
+ ]);
15
+ /**
16
+ * Generate a module map from analyzed client components
17
+ * Uses relative paths that match React's module reference format
18
+ */
19
+ export function generateModuleMap(clientComponents) {
20
+ const moduleMap = {};
21
+ for (const component of clientComponents) {
22
+ // Key format: Relative path from project root (e.g., "./src/app/counter.tsx")
23
+ // This must match the $$id that React emits for the component reference
24
+ const key = `./${component.relativePath}`;
25
+ // Convert to bundle path: "./src/app/counter.tsx" → "src/app/counter.js"
26
+ const bundlePath = patterns.replaceJsExtension(component.relativePath, ".js");
27
+ // Value: module reference with proper bundle path
28
+ // Use first export name as fallback (server.ts will use the actual export name from client reference)
29
+ moduleMap[key] = {
30
+ id: key, // Use relative path as ID (matches $$id in RSC stream)
31
+ chunks: [bundlePath], // Component file path in dist/ (without /dist/ prefix for react-server-dom-esm)
32
+ name: component.exportNames[0] ?? "default", // First export as fallback
33
+ };
34
+ }
35
+ return moduleMap;
36
+ }
37
+ /**
38
+ * Convert module map to the format expected by react-server-dom-esm bundler config
39
+ */
40
+ export function createBundlerConfig(moduleMap) {
41
+ const bundlerConfig = {};
42
+ for (const [key, entry] of Object.entries(moduleMap)) {
43
+ bundlerConfig[key] = {
44
+ id: entry.id,
45
+ chunks: entry.chunks,
46
+ name: entry.name,
47
+ };
48
+ }
49
+ return bundlerConfig;
50
+ }
51
+ /**
52
+ * Save module map to a JSON file
53
+ */
54
+ export async function saveModuleMap(moduleMap, outputPath) {
55
+ const json = JSON.stringify(moduleMap, null, 2);
56
+ await runtime.fs.writeTextFile(outputPath, json);
57
+ moduleMapLogger.debug(`📝 Module map saved to: ${outputPath}`);
58
+ }
59
+ /**
60
+ * Load module map from a JSON file
61
+ */
62
+ export async function loadModuleMap(filePath) {
63
+ const json = await runtime.fs.readTextFile(filePath);
64
+ return JSON.parse(json);
65
+ }
66
+ /**
67
+ * Create a client manifest for the browser
68
+ * This is used by the client to know which components are available
69
+ * Uses ES Module paths for standards compliance
70
+ */
71
+ export function createClientManifest(clientComponents) {
72
+ const manifest = {};
73
+ for (const component of clientComponents) {
74
+ manifest[component.relativePath] = {
75
+ id: component.relativePath,
76
+ chunks: ["client.js"],
77
+ name: component.exportNames[0] ?? "default",
78
+ };
79
+ }
80
+ return manifest;
81
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Import Rewriting for Server Components
3
+ * Automatically rewrites imports in server components to point to transformed client components
4
+ */
5
+ import type { TransformResult } from "./rsc-transform.ts";
6
+ export type ImportRewriteResult = {
7
+ originalPath: string;
8
+ rewrittenPath: string;
9
+ importsRewritten: number;
10
+ };
11
+ /**
12
+ * Rewrite imports in a server component file
13
+ */
14
+ export declare function rewriteServerComponentImports(serverComponentPath: string, clientComponentMap: Map<string, string>, cssModuleMap: Map<string, string>, outputDir: string, projectRoot: string): Promise<ImportRewriteResult>;
15
+ /**
16
+ * Create a map of client component paths to their transformed paths
17
+ */
18
+ export declare function createClientComponentMap(transformResults: TransformResult[]): Map<string, string>;
19
+ /**
20
+ * Create a map of CSS module paths to their JSON output paths
21
+ */
22
+ export declare function createCSSModuleMap(cssModulePaths: string[]): Map<string, string>;
23
+ /**
24
+ * Process all server components
25
+ */
26
+ export declare function rewriteAllServerComponents(serverComponentPaths: string[], transformResults: TransformResult[], cssModulePaths: string[], outputDir: string, projectRoot: string): Promise<ImportRewriteResult[]>;
27
+ /**
28
+ * Process all client components to rewrite CSS module imports
29
+ */
30
+ export declare function rewriteAllClientComponentCSSImports(clientComponents: Array<{
31
+ filePath: string;
32
+ }>, cssModulePaths: string[], _outputDir: string, projectRoot: string): Promise<void>;
33
+ /**
34
+ * Rewrite CSS module imports in ALL source files that might be bundled
35
+ * This is necessary because client components may import server components
36
+ * that have CSS module imports, which the bundler cannot load as CSS
37
+ */
38
+ export declare function rewriteAllSrcCSSModuleImports(srcDir: string, cssModulePaths: string[], projectRoot: string): Promise<void>;