dinou 9.3.0 → 10.0.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.
Files changed (166) hide show
  1. package/CHANGELOG.md +196 -0
  2. package/README.md +157 -132
  3. package/dinou/adapters/bun.js +34 -5
  4. package/dinou/adapters/cloudflare.js +110 -17
  5. package/dinou/adapters/deno.js +46 -8
  6. package/dinou/adapters/netlify.js +86 -50
  7. package/dinou/adapters/node.js +8 -0
  8. package/dinou/assets.d.ts +32 -5
  9. package/dinou/bun/build.js +190 -106
  10. package/dinou/cli.js +992 -532
  11. package/dinou/cloudflare/build.js +187 -106
  12. package/dinou/compiled/autoprefixer/index.js +509 -517
  13. package/dinou/compiled/babel-core/index.js +738 -716
  14. package/dinou/compiled/babel-generator/index.js +341 -299
  15. package/dinou/compiled/babel-loader/index.js +766 -744
  16. package/dinou/compiled/babel-parser/index.js +2 -2
  17. package/dinou/compiled/babel-plugin-react-compiler/index.js +178 -178
  18. package/dinou/compiled/babel-plugin-syntax-import-meta/index.js +4 -4
  19. package/dinou/compiled/babel-preset-react/index.js +752 -730
  20. package/dinou/compiled/babel-preset-typescript/index.js +738 -716
  21. package/dinou/compiled/babel-traverse/index.js +463 -433
  22. package/dinou/compiled/babel-types/index.js +176 -176
  23. package/dinou/compiled/chokidar/index.js +4 -4
  24. package/dinou/compiled/commander/index.js +15 -15
  25. package/dinou/compiled/copy-webpack-plugin/index.js +433 -425
  26. package/dinou/compiled/css-loader/index.js +289 -289
  27. package/dinou/compiled/css-loader/runtime/api.js +85 -0
  28. package/dinou/compiled/css-loader/runtime/getUrl.js +26 -0
  29. package/dinou/compiled/css-loader/runtime/noSourceMaps.js +5 -0
  30. package/dinou/compiled/css-loader/runtime/package.json +3 -0
  31. package/dinou/compiled/css-loader/runtime/sourceMaps.js +16 -0
  32. package/dinou/compiled/fast-glob/index.js +5810 -0
  33. package/dinou/compiled/generic-names/index.js +16 -16
  34. package/dinou/compiled/mini-css-extract-plugin/{chunk-YYEJWYXE.js → chunk-R6DP5ZYM.js} +36 -36
  35. package/dinou/compiled/mini-css-extract-plugin/hmr/hotModuleReplacement.js +253 -0
  36. package/dinou/compiled/mini-css-extract-plugin/hmr/normalize-url.js +39 -0
  37. package/dinou/compiled/mini-css-extract-plugin/hmr/package.json +3 -0
  38. package/dinou/compiled/mini-css-extract-plugin/index.js +1 -1
  39. package/dinou/compiled/mini-css-extract-plugin/loader.js +5 -5
  40. package/dinou/compiled/postcss/index.js +83 -83
  41. package/dinou/compiled/postcss-import/index.js +147 -117
  42. package/dinou/compiled/postcss-modules/index.js +216 -216
  43. package/dinou/compiled/resolve/index.js +1224 -0
  44. package/dinou/compiled/rollup-plugin-babel/index.js +753 -731
  45. package/dinou/compiled/rollup-plugin-commonjs/index.js +43 -38
  46. package/dinou/compiled/rollup-plugin-delete/index.js +615 -253
  47. package/dinou/compiled/rollup-plugin-json/index.js +22 -18
  48. package/dinou/compiled/rollup-plugin-node-resolve/index.js +130 -100
  49. package/dinou/compiled/rollup-plugin-postcss/index.js +3299 -3277
  50. package/dinou/compiled/rollup-plugin-replace/index.js +34 -29
  51. package/dinou/compiled/tailwindcss-postcss/index.js +214 -211
  52. package/dinou/compiled/vendors-manifest.json +59 -83
  53. package/dinou/core/asset-extensions.js +482 -33
  54. package/dinou/core/bailout-proxy.js +21 -2
  55. package/dinou/core/build-static-pages.js +523 -371
  56. package/dinou/core/builtin-asset-extensions.js +63 -0
  57. package/dinou/core/cache-revalidate.js +449 -256
  58. package/dinou/core/cache-slot.js +289 -79
  59. package/dinou/core/client-webpack.jsx +470 -39
  60. package/dinou/core/client.jsx +402 -39
  61. package/dinou/core/config.js +3 -0
  62. package/dinou/core/copy-static-dirs.js +98 -0
  63. package/dinou/core/createScopedName.js +22 -4
  64. package/dinou/core/dinou-paths.js +1 -1
  65. package/dinou/core/discover-layouts.js +200 -0
  66. package/dinou/core/edge-ssr.js +1 -1
  67. package/dinou/core/ensure-vendor-mode.js +78 -19
  68. package/dinou/core/env.js +544 -0
  69. package/dinou/core/export-static.js +41 -9
  70. package/dinou/core/generate-route-types.js +989 -95
  71. package/dinou/core/generate-static-rsc.js +5 -6
  72. package/dinou/core/generate-static.js +45 -6
  73. package/dinou/core/get-abs-path-with-ext.js +217 -75
  74. package/dinou/core/get-asset-from-manifest.js +19 -8
  75. package/dinou/core/get-error-jsx.js +4 -5
  76. package/dinou/core/get-file-path-and-dynamic-params.js +561 -338
  77. package/dinou/core/get-jsx.js +220 -53
  78. package/dinou/core/handler.js +1647 -466
  79. package/dinou/core/import-module.js +7 -0
  80. package/dinou/core/layout-functions.js +72 -13
  81. package/dinou/core/link.jsx +34 -6
  82. package/dinou/core/load-dinou-config.js +520 -89
  83. package/dinou/core/manifest-provider.js +4 -14
  84. package/dinou/core/navigation-utils.js +141 -3
  85. package/dinou/core/navigation.js +134 -50
  86. package/dinou/core/node-middleware-runner.js +165 -0
  87. package/dinou/core/postcss-pipeline.js +236 -0
  88. package/dinou/core/ppr-context.js +114 -22
  89. package/dinou/core/ppr-runtime.js +391 -127
  90. package/dinou/core/redirect.jsx +1 -1
  91. package/dinou/core/render-jsx-to-client-jsx.js +216 -48
  92. package/dinou/core/request-context.js +59 -3
  93. package/dinou/core/resolve-contextual-dinou.js +523 -0
  94. package/dinou/core/resolve-setup-files.js +85 -27
  95. package/dinou/core/revalidating.js +10 -2
  96. package/dinou/core/route-generator.js +6 -2
  97. package/dinou/core/rsc-renderer.js +4 -1
  98. package/dinou/core/run-ssg.js +6 -0
  99. package/dinou/core/runtime-env.js +265 -0
  100. package/dinou/core/server-css-modules-plugin.js +155 -0
  101. package/dinou/core/server-function-proxy-webpack.js +27 -2
  102. package/dinou/core/server-function-proxy.js +27 -2
  103. package/dinou/core/slot.js +47 -2
  104. package/dinou/core/static-assets.js +503 -0
  105. package/dinou/core/storage-adapter.js +452 -24
  106. package/dinou/core/transform-server-functions.js +272 -24
  107. package/dinou/core/tsconfig-aliases.js +964 -0
  108. package/dinou/core/uneject.js +640 -0
  109. package/dinou/core/url-resolver.js +1 -1
  110. package/dinou/core/vfs.js +11 -2
  111. package/dinou/css.d.ts +4 -1
  112. package/dinou/deno/build.js +205 -153
  113. package/dinou/deno/seed-kv.js +4 -0
  114. package/dinou/env.d.ts +22 -2
  115. package/dinou/esbuild/build.js +40 -50
  116. package/dinou/esbuild/dev.js +33 -7
  117. package/dinou/esbuild/helpers-esbuild/get-config-esbuild-prod.js +41 -16
  118. package/dinou/esbuild/helpers-esbuild/get-config-esbuild.js +51 -49
  119. package/dinou/esbuild/helpers-esbuild/get-esbuild-entries.js +73 -60
  120. package/dinou/esbuild/helpers-esbuild/swc-disk-cache.js +35 -16
  121. package/dinou/esbuild/helpers-esbuild/write.js +13 -3
  122. package/dinou/esbuild/plugins-esbuild/assets-plugin.js +100 -238
  123. package/dinou/esbuild/plugins-esbuild/babel-react-compiler-plugin.js +73 -29
  124. package/dinou/esbuild/plugins-esbuild/css-processor-plugin.js +283 -91
  125. package/dinou/esbuild/plugins-esbuild/manifest-generator-plugin.js +3 -5
  126. package/dinou/esbuild/plugins-esbuild/react-client-manifest-plugin.js +9 -3
  127. package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +4 -3
  128. package/dinou/esbuild/plugins-esbuild/stable-chunk-names-and-maps-plugin.js +10 -1
  129. package/dinou/esbuild/plugins-postcss/postcss-extract-plugin.js +84 -21
  130. package/dinou/esbuild/react-refresh/esm-hmr/client.js +31 -10
  131. package/dinou/esbuild/react-refresh/esm-hmr-plugin.js +48 -18
  132. package/dinou/esbuild/react-refresh/is-react-refresh-boundary.js +9 -1
  133. package/dinou/esbuild/react-refresh/react-refresh-runtime.js +13 -7
  134. package/dinou/index.d.ts +1028 -88
  135. package/dinou/node/build.js +138 -74
  136. package/dinou/node/bundle-dual-engine.js +393 -131
  137. package/dinou/node/dev.js +1005 -275
  138. package/dinou/package.json +11 -34
  139. package/dinou/rollup/postcss.config.js +3 -4
  140. package/dinou/rollup/react-refresh/esm-hmr/client.js +31 -10
  141. package/dinou/rollup/react-refresh/react-refresh-wrap-modules.js +16 -13
  142. package/dinou/rollup/react-refresh/rollup-plugin-esm-hmr.js +53 -7
  143. package/dinou/rollup/rollup-plugins/dinou-asset-plugin.js +58 -20
  144. package/dinou/rollup/rollup-plugins/manifest-generator-plugin.js +16 -7
  145. package/dinou/rollup/rollup-plugins/rollup-plugin-react-client-manifest.js +118 -42
  146. package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +48 -36
  147. package/dinou/rollup/rollup.config.js +174 -101
  148. package/dinou/scripts/build-vendors.js +123 -33
  149. package/dinou/server.d.ts +126 -15
  150. package/dinou/webpack/helpers/get-webpack-entries.js +87 -41
  151. package/dinou/webpack/helpers/patch-webpack-internals.js +93 -1
  152. package/dinou/webpack/loaders/server-functions-loader.js +1 -1
  153. package/dinou/webpack/plugins/manifest-generator-plugin.js +27 -7
  154. package/dinou/webpack/plugins/server-functions-plugin.js +39 -12
  155. package/dinou/webpack/plugins/webpack-memory-plugin.js +25 -12
  156. package/dinou/webpack/postcss.config.js +3 -4
  157. package/dinou/webpack/webpack.config.js +408 -132
  158. package/eject.js +6 -224
  159. package/package.json +7 -9
  160. package/dinou/compiled/cross-env/index.js +0 -661
  161. package/dinou/compiled/dotenv/index.js +0 -375
  162. package/dinou/compiled/esbuild-copy-static-files/index.js +0 -63
  163. package/dinou/compiled/esbuild-tsconfig-paths/index.js +0 -1314
  164. package/dinou/compiled/rollup-plugin-copy/index.js +0 -12066
  165. package/dinou/compiled/rollup-plugin-tsconfig-paths/index.js +0 -569
  166. package/dinou/compiled/tsconfig-paths-webpack-plugin/index.js +0 -3956
@@ -1,12 +1,10 @@
1
- import fs from "node:fs/promises";
1
+ import fs from "node:fs";
2
2
  import path from "node:path";
3
- import createScopedName from "../../core/createScopedName.js";
4
- import { regex } from "../../core/asset-extensions.js";
5
- import { getAbsPathWithExt } from "../../core/get-abs-path-with-ext.js";
6
- import { pathToFileURL } from "node:url";
7
- import stripNamespace from "../helpers-esbuild/strip-namespace.js";
8
-
9
- const escapeRegExp = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3
+ import {
4
+ getAssetExtensionsInfo,
5
+ getAssetInfo,
6
+ resolveAssetImport,
7
+ } from "../../core/asset-extensions.js";
10
8
 
11
9
  const normKey = (p) => {
12
10
  if (!p) return "";
@@ -17,9 +15,41 @@ const normKey = (p) => {
17
15
  return s;
18
16
  };
19
17
 
20
- export default function assetsPlugin({ include = regex, changedIds } = {}) {
21
- let isInitial = true;
22
- let cachedRenames = new Map();
18
+ export default function assetsPlugin({
19
+ include = null,
20
+ dinouConfig = null,
21
+ projectRoot = process.cwd(),
22
+ serverAssetPaths = [],
23
+ changedIds,
24
+ } = {}) {
25
+ const rootDir = path.resolve(projectRoot || process.cwd());
26
+ const emittedAssetsBySource = new Map();
27
+
28
+ function recordEmittedAsset(absSourcePath) {
29
+ if (!absSourcePath || !fs.existsSync(absSourcePath)) return null;
30
+ const info = getAssetInfo(absSourcePath, rootDir, { includeBuffer: true });
31
+ if (!info.buffer) return info;
32
+
33
+ const sourceKey = normKey(absSourcePath);
34
+ const prev = emittedAssetsBySource.get(sourceKey);
35
+ if (
36
+ prev &&
37
+ prev.fileName !== info.fileName &&
38
+ typeof globalThis !== "undefined" &&
39
+ globalThis.__DINOU_MEM_FILES__
40
+ ) {
41
+ globalThis.__DINOU_MEM_FILES__.delete(prev.fileName);
42
+ globalThis.__DINOU_MEM_FILES__.delete("/" + prev.fileName);
43
+ globalThis.__DINOU_MEM_FILES__.delete(path.basename(prev.fileName));
44
+ }
45
+
46
+ emittedAssetsBySource.set(sourceKey, {
47
+ sourcePath: path.resolve(absSourcePath),
48
+ fileName: info.fileName,
49
+ contents: info.buffer,
50
+ });
51
+ return info;
52
+ }
23
53
 
24
54
  return {
25
55
  name: "assets-plugin",
@@ -31,17 +61,29 @@ export default function assetsPlugin({ include = regex, changedIds } = {}) {
31
61
 
32
62
  build.initialOptions.assetNames = "assets/[name]-[hash]";
33
63
 
34
- // Handle asset loading with different namespaces
35
- build.onResolve({ filter: include }, async (args) => {
64
+ const extInfo = getAssetExtensionsInfo(dinouConfig, rootDir);
65
+ const cleanRegex = include || extInfo.regex;
66
+ const filterRegex = include
67
+ ? new RegExp(
68
+ include.source.replace(/\\\$$|\$$/, "(?:[?#].*)?$"),
69
+ include.flags.includes("i") ? "i" : `${include.flags}i`
70
+ )
71
+ : extInfo.resolveFilterRegex;
72
+
73
+ build.onResolve({ filter: filterRegex }, async (args) => {
36
74
  if (args.pluginData?.inAssetPlugin) return null;
37
- let resolvedAlias =
75
+
76
+ let resolvedPath =
38
77
  args.kind === "entry-point"
39
- ? args.path
40
- : getAbsPathWithExt(args.path, {
41
- parentURL: pathToFileURL(args.importer).href,
78
+ ? path.resolve(rootDir, args.path)
79
+ : resolveAssetImport(args.path, {
80
+ importer: args.importer,
81
+ resolveDir: args.resolveDir,
82
+ projectRoot: rootDir,
83
+ assetRegex: cleanRegex,
42
84
  });
43
85
 
44
- if (!resolvedAlias) {
86
+ if (!resolvedPath) {
45
87
  try {
46
88
  const res = await build.resolve(args.path, {
47
89
  resolveDir: args.resolveDir,
@@ -49,266 +91,86 @@ export default function assetsPlugin({ include = regex, changedIds } = {}) {
49
91
  pluginData: { inAssetPlugin: true },
50
92
  });
51
93
  if (res && res.path) {
52
- resolvedAlias = res.path;
94
+ resolvedPath = res.path;
53
95
  }
54
96
  } catch (e) {}
55
97
  }
56
98
 
57
- if (!resolvedAlias) return null;
99
+ if (!resolvedPath) return null;
58
100
 
59
101
  if (args.kind === "entry-point") {
60
102
  return {
61
- path: resolvedAlias,
103
+ path: resolvedPath,
62
104
  namespace: "dinou-asset-entry",
63
105
  };
64
106
  }
65
107
 
66
108
  return {
67
- path: resolvedAlias,
109
+ path: resolvedPath,
68
110
  namespace: "dinou-asset",
69
111
  };
70
112
  });
71
113
 
72
- // Cache asset file buffers in memory
73
- const assetCache = new Map();
74
- const emittedAssets = new Map();
75
-
76
- // Loader for normal assets
77
- build.onLoad({ filter: /.*/, namespace: "dinou-asset" }, async (args) => {
78
- let contents = assetCache.get(args.path);
79
- if (!contents) {
80
- contents = await fs.readFile(args.path);
81
- assetCache.set(args.path, contents);
82
- }
83
- const ext = path.extname(args.path);
84
- const base = path.basename(args.path, ext);
85
- const scoped = createScopedName(base, args.path);
86
- const assetUrl = `/assets/${scoped}${ext}`;
87
- const newLocal = `assets/${scoped}${ext}`;
88
-
89
- emittedAssets.set(newLocal, {
90
- path: path.join(outdir, newLocal),
91
- contents,
92
- get text() {
93
- return new TextDecoder().decode(this.contents);
94
- },
95
- });
96
-
114
+ const loadAssetModule = async (args) => {
115
+ const info = recordEmittedAsset(args.path);
116
+ const publicUrl =
117
+ info?.publicUrl || getAssetInfo(args.path, rootDir).publicUrl;
97
118
  return {
98
- contents: `export default "${assetUrl}";`,
119
+ contents: `export default ${JSON.stringify(publicUrl)};`,
99
120
  loader: "js",
100
121
  watchFiles: [args.path],
101
122
  };
102
- });
123
+ };
103
124
 
104
- // Loader for asset entry points
125
+ build.onLoad({ filter: /.*/, namespace: "dinou-asset" }, loadAssetModule);
105
126
  build.onLoad(
106
127
  { filter: /.*/, namespace: "dinou-asset-entry" },
107
- async (args) => {
108
- let contents = assetCache.get(args.path);
109
- if (!contents) {
110
- contents = await fs.readFile(args.path);
111
- assetCache.set(args.path, contents);
112
- }
113
- return { contents, loader: "file", watchFiles: [args.path] };
114
- }
128
+ loadAssetModule
115
129
  );
116
130
 
117
131
  build.onEnd(async (result) => {
118
132
  const tAssets0 = Date.now();
119
133
  try {
120
- if (!result.metafile || !result.outputFiles?.length) return;
121
-
122
- const normalizeRel = (p) => p.replace(/\\/g, "/");
123
- const isIncremental = !isInitial && changedIds && changedIds.size > 0;
124
- const hasAssetChanged = isIncremental && Array.from(changedIds).some((id) => include.test(id));
134
+ if (!result || !Array.isArray(result.outputFiles)) return;
125
135
 
126
- // Ensure JS-loaded assets are included in output files
127
- for (const asset of emittedAssets.values()) {
128
- if (!result.outputFiles.some((f) => f.path === asset.path)) {
129
- result.outputFiles.push(asset);
136
+ if (Array.isArray(serverAssetPaths) && serverAssetPaths.length > 0) {
137
+ for (const serverAssetPath of serverAssetPaths) {
138
+ recordEmittedAsset(serverAssetPath);
130
139
  }
131
140
  }
132
141
 
133
- let renames = cachedRenames;
134
-
135
- // Only re-scan all asset outputs on initial build or if an asset file actually changed
136
- if (isInitial || hasAssetChanged) {
137
- renames = new Map();
138
- const processedSourceFiles = new Set();
139
-
140
- // First pass: normal assets (individual files)
141
- for (const [oldRelPath, info] of Object.entries(
142
- result.metafile.outputs
143
- )) {
144
- if (info.entryPoint || Object.keys(info.inputs).length !== 1)
145
- continue;
146
-
147
- const inputPath = Object.keys(info.inputs)[0];
148
- const sourceFile = inputPath
149
- .replace(/^dinou-asset:/, "")
150
- .replace(/^dinou-asset-entry:/, "");
151
- if (!include.test(sourceFile)) continue;
152
-
153
- const ext = path.extname(sourceFile);
154
- if (!oldRelPath.endsWith(ext)) continue;
155
- const base = path.basename(sourceFile, ext);
156
- const scoped = createScopedName(base, sourceFile);
157
- const newLocal = `assets/${scoped}${ext}`;
158
- const oldLocal = normalizeRel(path.relative(outdir, oldRelPath));
159
-
160
- renames.set(oldLocal, newLocal);
161
- processedSourceFiles.add(sourceFile);
162
- }
163
-
164
- // Pre-index outputFiles for O(1) lookup
165
- const outputFilesByRelPath = new Map();
166
- for (const f of result.outputFiles) {
167
- outputFilesByRelPath.set(normalizeRel(path.relative(process.cwd(), f.path)), f);
168
- }
169
-
170
- // Second pass: find assets that are inside chunks
171
- for (const [outputPath, info] of Object.entries(
172
- result.metafile.outputs
173
- )) {
174
- if (!outputPath.endsWith(".js") || info.entryPoint) continue;
175
-
176
- for (const inputPath of Object.keys(info.inputs)) {
177
- if (!inputPath.startsWith("dinou-asset:")) continue;
178
-
179
- const sourceFile = inputPath.replace(/^dinou-asset:/, "");
180
- if (
181
- !include.test(sourceFile) ||
182
- processedSourceFiles.has(sourceFile)
183
- )
184
- continue;
185
-
186
- try {
187
- const ext = path.extname(sourceFile);
188
- const base = path.basename(sourceFile, ext);
189
- const scoped = createScopedName(base, sourceFile);
190
- const newLocal = `assets/${scoped}${ext}`;
191
-
192
- const assetContent = await fs.readFile(sourceFile);
193
-
194
- const newOutputFile = {
195
- path: path.join(outdir, newLocal),
196
- contents: assetContent,
197
- get text() {
198
- return new TextDecoder().decode(this.contents);
199
- },
200
- };
201
-
202
- result.outputFiles.push(newOutputFile);
203
- processedSourceFiles.add(sourceFile);
204
-
205
- const chunkFile = outputFilesByRelPath.get(outputPath);
206
-
207
- if (chunkFile) {
208
- let chunkContent = new TextDecoder().decode(chunkFile.contents);
209
- const assetComment = `// ${inputPath}`;
210
- const commentIndex = chunkContent.indexOf(assetComment);
211
-
212
- if (commentIndex !== -1) {
213
- const nextLineStart = chunkContent.indexOf("\n", commentIndex) + 1;
214
- const nextLineEnd = chunkContent.indexOf("\n", nextLineStart);
215
- const assignmentLine = chunkContent.substring(nextLineStart, nextLineEnd);
216
- const varMatch = assignmentLine.match(/var (\w+)_default = "([^"]+)"/);
217
-
218
- if (varMatch) {
219
- const varName = varMatch[1];
220
- const newAssignmentLine = `var ${varName}_default = "/${newLocal}";`;
221
- chunkContent =
222
- chunkContent.substring(0, nextLineStart) +
223
- newAssignmentLine +
224
- chunkContent.substring(nextLineEnd);
225
- }
226
- } else {
227
- const assetName = path.basename(sourceFile);
228
- const escapedAssetName = escapeRegExp(assetName);
229
- const pattern = new RegExp(
230
- `(var \\w+_default = ")([^"]*${escapedAssetName}[^"]*)(";)`,
231
- "g"
232
- );
233
-
234
- if (pattern.test(chunkContent)) {
235
- chunkContent = chunkContent.replace(
236
- pattern,
237
- `$1/${newLocal}$3`
238
- );
239
- }
240
- }
241
-
242
- chunkFile.contents = new TextEncoder().encode(chunkContent);
243
- }
244
- } catch (error) {
245
- console.error(
246
- `Error extracting asset ${sourceFile} from chunk:`,
247
- error
248
- );
249
- }
142
+ if (changedIds && changedIds.size > 0) {
143
+ for (const changedPath of changedIds) {
144
+ if (cleanRegex.test(changedPath) && fs.existsSync(changedPath)) {
145
+ recordEmittedAsset(changedPath);
250
146
  }
251
147
  }
252
-
253
- cachedRenames = renames;
254
148
  }
255
149
 
256
- if (renames.size === 0) return;
257
-
258
- // Update references in JS/CSS files:
259
- // On incremental rebuild, only process output files belonging to changed modules!
260
- for (const file of result.outputFiles) {
261
- const relPath = normalizeRel(path.relative(process.cwd(), file.path));
262
- if (!relPath.endsWith(".js") && !relPath.endsWith(".css")) continue;
150
+ const existingDestKeys = new Set(
151
+ result.outputFiles.map((f) => normKey(f.path))
152
+ );
263
153
 
264
- if (isIncremental && !hasAssetChanged) {
265
- const outputInfo = result.metafile.outputs[relPath];
266
- const inputFiles = Object.keys(outputInfo?.inputs || {});
267
- const touchesChanged = inputFiles.some((m) => {
268
- const clean = stripNamespace(m);
269
- return changedIds.has(normKey(clean));
270
- });
271
- if (!touchesChanged) continue;
272
- }
273
-
274
- let content = new TextDecoder().decode(file.contents);
275
- let modified = false;
276
- for (const [oldLocal, newLocal] of renames) {
277
- if (!content.includes(oldLocal)) continue;
278
- const patterns = [
279
- [`"./${escapeRegExp(oldLocal)}"`, `"/${newLocal}"`],
280
- [`"${escapeRegExp(oldLocal)}"`, `"${newLocal}"`],
281
- [`'./${escapeRegExp(oldLocal)}'`, `'/${newLocal}'`],
282
- [`'${escapeRegExp(oldLocal)}'`, `'${newLocal}'`],
283
- ];
284
-
285
- for (const [oldPattern, newPattern] of patterns) {
286
- const reg = new RegExp(oldPattern, "g");
287
- if (reg.test(content)) {
288
- content = content.replace(reg, newPattern);
289
- modified = true;
290
- }
291
- }
292
- }
293
- if (modified) {
294
- file.contents = new TextEncoder().encode(content);
295
- }
296
- }
297
-
298
- // Update paths in outputFiles
299
- if (!isIncremental) {
300
- for (const file of result.outputFiles) {
301
- const relPath = normalizeRel(path.relative(process.cwd(), file.path));
302
- const oldLocal = normalizeRel(path.relative(outdir, relPath));
303
- const newLocal = renames.get(oldLocal);
304
-
305
- if (newLocal) {
306
- file.path = path.join(outdir, newLocal);
307
- }
154
+ for (const [sourceKey, asset] of emittedAssetsBySource.entries()) {
155
+ if (!fs.existsSync(asset.sourcePath)) {
156
+ emittedAssetsBySource.delete(sourceKey);
157
+ continue;
308
158
  }
159
+ const destPath = path.resolve(outdir, asset.fileName);
160
+ const destKey = normKey(destPath);
161
+ if (existingDestKeys.has(destKey)) continue;
162
+ existingDestKeys.add(destKey);
163
+
164
+ const contents = asset.contents;
165
+ result.outputFiles.push({
166
+ path: destPath,
167
+ contents,
168
+ get text() {
169
+ return new TextDecoder().decode(this.contents);
170
+ },
171
+ });
309
172
  }
310
173
  } finally {
311
- isInitial = false;
312
174
  globalThis.__DINOU_ASSETS_TIME__ = Date.now() - tAssets0;
313
175
  }
314
176
  });
@@ -1,54 +1,98 @@
1
- import babel from "#babel/core";
1
+ import babelModule from "#babel/core";
2
+ import presetReactModule from "#babel/preset-react";
3
+ import presetTypescriptModule from "#babel/preset-typescript";
4
+ import pluginSyntaxImportMetaModule from "#babel/plugin-syntax-import-meta";
5
+ import pluginReactCompilerModule from "#babel-plugin-react-compiler";
2
6
  import fs from "node:fs/promises";
3
7
  import path from "node:path";
4
- const norm = (p) => path.resolve(p).replace(/\\/g, "/");
5
- export default function babelReactCompilerPlugin() {
8
+ import { fileURLToPath } from "node:url";
9
+ import { useClientRegex, useServerRegex } from "../../constants.js";
10
+ import { hasServerFunctions } from "../../core/transform-server-functions.js";
11
+
12
+ const unwrapBabel = (mod) => mod?.default ?? mod;
13
+ const babel = unwrapBabel(babelModule);
14
+ const presetReact = unwrapBabel(presetReactModule);
15
+ const presetTypescript = unwrapBabel(presetTypescriptModule);
16
+ const pluginSyntaxImportMeta = unwrapBabel(pluginSyntaxImportMetaModule);
17
+ const pluginReactCompiler = unwrapBabel(pluginReactCompilerModule);
18
+
19
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
20
+ const normalizeNormPath = (p) => {
21
+ let s = path.resolve(p).replace(/\\/g, "/");
22
+ if (process.platform === "win32") {
23
+ s = s.replace(/^([a-zA-Z]):/, (_, d) => d.toLowerCase() + ":");
24
+ }
25
+ return s;
26
+ };
27
+ const dinouRootPrefix =
28
+ normalizeNormPath(path.resolve(__dirname, "../..")) + "/";
29
+
30
+ export default function babelReactCompilerPlugin({ serverFiles = null } = {}) {
31
+ const serverFilesSet = serverFiles
32
+ ? new Set(Array.from(serverFiles).map((f) => normalizeNormPath(f)))
33
+ : null;
34
+
6
35
  return {
7
36
  name: "babel-react-compiler-bridge",
8
37
  setup(build) {
9
- const entryPoints = build.initialOptions.entryPoints;
10
- // Intercept JS/TS/JSX/TSX files
11
- // NOTE: Filter node_modules to avoid slowing down by processing external libraries
12
- build.onLoad({ filter: /\.[jt]sx?$/ }, async (args) => {
13
- // Double check security to avoid node_modules
14
- if (args.path.includes("node_modules")) return;
15
- const abs = path.resolve(args.path);
38
+ const root = build.initialOptions?.absWorkingDir || process.cwd();
39
+ // Intercept user JS/TS/JSX/TSX modules across the entire client graph
40
+ // (including child components and custom hooks without their own "use client" directive).
41
+ build.onLoad({ filter: /\.[jt]sx?$/, namespace: "file" }, async (args) => {
42
+ const normPath = args.path.replace(/\\/g, "/");
43
+ const relFromRoot = path.relative(root, args.path).replace(/\\/g, "/");
44
+ if (
45
+ normPath.includes("/node_modules/") ||
46
+ relFromRoot.startsWith(".dinou/")
47
+ ) {
48
+ return null;
49
+ }
16
50
 
17
- const absNorm = norm(abs);
18
- const isAnEntryPoint = Object.values(entryPoints).some(
19
- (val) => norm(path.resolve(val)) === absNorm,
20
- );
21
- if (!isAnEntryPoint) {
22
- return;
51
+ const absNorm = normalizeNormPath(args.path);
52
+ // Skip Dinou's internal framework runtime (both in library and ejected mode)
53
+ if (absNorm.startsWith(dinouRootPrefix) || relFromRoot.startsWith("dinou/")) {
54
+ return null;
55
+ }
56
+ // Skip known "use server" modules so serverFunctionsPlugin handles them exclusively
57
+ if (serverFilesSet && serverFilesSet.has(absNorm)) {
58
+ return null;
23
59
  }
60
+
24
61
  try {
25
62
  const source = await fs.readFile(args.path, "utf8");
26
- const filename = args.path;
63
+ const trimmed = source.trim();
64
+
65
+ // Defense-in-depth: never run client React Compiler on "use server" modules
66
+ if (
67
+ !useClientRegex.test(trimmed) &&
68
+ (useServerRegex.test(trimmed) || hasServerFunctions(source))
69
+ ) {
70
+ return null;
71
+ }
27
72
 
28
- // Transform the code with Babel + React Compiler
29
73
  const result = await babel.transformAsync(source, {
30
- filename,
74
+ filename: args.path,
31
75
  presets: [
32
- // We need to tell Babel to understand React and TS before compiling
33
- ["@babel/preset-react", { runtime: "automatic" }],
34
- "@babel/preset-typescript",
76
+ [presetReact, { runtime: "automatic" }],
77
+ presetTypescript,
35
78
  ],
36
79
  plugins: [
37
- "babel-plugin-react-compiler", // 👈 The jewel in the crown
80
+ pluginReactCompiler,
81
+ pluginSyntaxImportMeta,
38
82
  ],
39
- sourceMaps: true, // Vital for esbuild sourcemaps to work
40
- configFile: false, // Ignore global babel.config.js to get straight to the point
83
+ sourceMaps: build.initialOptions.sourcemap ? "inline" : false,
84
+ configFile: false,
85
+ babelrc: false,
41
86
  });
42
87
 
43
- // If for some reason Babel does not return code, let esbuild handle it
44
- if (!result || !result.code) return;
88
+ if (!result || !result.code) return null;
45
89
 
46
90
  return {
47
91
  contents: result.code,
48
- loader: "js", // Babel returns standard JS
92
+ loader: "js",
93
+ resolveDir: path.dirname(args.path),
49
94
  };
50
95
  } catch (error) {
51
- // If Babel fails, show a clean error to not break the build silently
52
96
  return {
53
97
  errors: [
54
98
  {