@lolyjs/core 0.3.0-alpha.2 → 0.3.0-alpha.4

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/index.mjs CHANGED
@@ -9993,6 +9993,9 @@ var INIT_FILE_NAME = "init.server";
9993
9993
  async function runInitIfExists(projectRoot, serverData) {
9994
9994
  try {
9995
9995
  let mod = await getServerFile(projectRoot, INIT_FILE_NAME);
9996
+ if (!mod) {
9997
+ return {};
9998
+ }
9996
9999
  if (typeof mod?.init === "function") {
9997
10000
  const serverContext = { ...serverData };
9998
10001
  await mod.init({ serverContext });
@@ -10118,7 +10121,7 @@ var NAMING = {
10118
10121
  // Files
10119
10122
  SERVER_HOOK: "server.hook"
10120
10123
  };
10121
- async function loadServerHookForDir(currentDir) {
10124
+ async function loadServerHookForDir(currentDir, projectRoot) {
10122
10125
  const isDev = process.env.NODE_ENV === "development";
10123
10126
  const isBuild = process.env.LOLY_BUILD === "1";
10124
10127
  let file = null;
@@ -10144,10 +10147,24 @@ async function loadServerHookForDir(currentDir) {
10144
10147
  generateStaticParams: null
10145
10148
  };
10146
10149
  }
10150
+ let resolvedProjectRoot = projectRoot;
10151
+ if (!resolvedProjectRoot) {
10152
+ let current = path4.resolve(currentDir);
10153
+ while (current !== path4.dirname(current)) {
10154
+ if (fs4.existsSync(path4.join(current, "package.json"))) {
10155
+ resolvedProjectRoot = current;
10156
+ break;
10157
+ }
10158
+ current = path4.dirname(current);
10159
+ }
10160
+ if (!resolvedProjectRoot) {
10161
+ resolvedProjectRoot = currentDir;
10162
+ }
10163
+ }
10147
10164
  let mod;
10148
10165
  try {
10149
10166
  const { loadModule: loadModule2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
10150
- mod = await loadModule2(file, { projectRoot: currentDir });
10167
+ mod = await loadModule2(file, { projectRoot: resolvedProjectRoot });
10151
10168
  } catch (error) {
10152
10169
  console.error(
10153
10170
  `[framework][server-hook] Error loading server hook from ${file}:`,
@@ -10190,7 +10207,7 @@ async function loadServerHookForDir(currentDir) {
10190
10207
  generateStaticParams
10191
10208
  };
10192
10209
  }
10193
- async function loadLayoutServerHook(layoutFile) {
10210
+ async function loadLayoutServerHook(layoutFile, projectRoot) {
10194
10211
  const layoutDir = path4.dirname(layoutFile);
10195
10212
  const layoutExt = path4.extname(layoutFile);
10196
10213
  const layoutBasename = path4.basename(layoutFile, layoutExt);
@@ -10216,7 +10233,20 @@ async function loadLayoutServerHook(layoutFile) {
10216
10233
  }
10217
10234
  try {
10218
10235
  const { loadModule: loadModule2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
10219
- const mod = await loadModule2(file, { projectRoot: path4.dirname(file) });
10236
+ if (!projectRoot) {
10237
+ let current = path4.dirname(layoutFile);
10238
+ while (current !== path4.dirname(current)) {
10239
+ if (fs4.existsSync(path4.join(current, "package.json"))) {
10240
+ projectRoot = current;
10241
+ break;
10242
+ }
10243
+ current = path4.dirname(current);
10244
+ }
10245
+ if (!projectRoot) {
10246
+ projectRoot = path4.dirname(layoutFile);
10247
+ }
10248
+ }
10249
+ const mod = await loadModule2(file, { projectRoot });
10220
10250
  const serverHook = typeof mod?.getServerSideProps === "function" ? mod.getServerSideProps : null;
10221
10251
  let middlewares = [];
10222
10252
  const rawMiddlewares = mod?.[NAMING.BEFORE_MIDDLEWARES];
@@ -10304,6 +10334,15 @@ async function loadRoutes(appDir) {
10304
10334
  if (!fs5.existsSync(appDir)) {
10305
10335
  return [];
10306
10336
  }
10337
+ let projectRoot = appDir;
10338
+ let current = path5.resolve(appDir);
10339
+ while (current !== path5.dirname(current)) {
10340
+ if (fs5.existsSync(path5.join(current, "package.json"))) {
10341
+ projectRoot = current;
10342
+ break;
10343
+ }
10344
+ current = path5.dirname(current);
10345
+ }
10307
10346
  const routes = [];
10308
10347
  async function walk(currentDir) {
10309
10348
  const entries = fs5.readdirSync(currentDir, { withFileTypes: true });
@@ -10322,7 +10361,7 @@ async function loadRoutes(appDir) {
10322
10361
  const { regex, paramNames } = buildRegexFromRoutePath(routePath);
10323
10362
  const { loadDefaultExport: loadDefaultExport2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
10324
10363
  const component = await loadDefaultExport2(fullPath, {
10325
- projectRoot: appDir
10364
+ projectRoot
10326
10365
  });
10327
10366
  if (!component) {
10328
10367
  continue;
@@ -10334,7 +10373,7 @@ async function loadRoutes(appDir) {
10334
10373
  const layoutServerHooks = [];
10335
10374
  const layoutMiddlewares = [];
10336
10375
  for (const layoutFile of layoutFiles) {
10337
- const layoutHookData = await loadLayoutServerHook(layoutFile);
10376
+ const layoutHookData = await loadLayoutServerHook(layoutFile, projectRoot);
10338
10377
  if (layoutHookData) {
10339
10378
  layoutServerHooks.push(layoutHookData.serverHook);
10340
10379
  layoutMiddlewares.push(layoutHookData.middlewares);
@@ -10343,7 +10382,7 @@ async function loadRoutes(appDir) {
10343
10382
  layoutMiddlewares.push([]);
10344
10383
  }
10345
10384
  }
10346
- const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(currentDir);
10385
+ const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(currentDir, projectRoot);
10347
10386
  routes.push({
10348
10387
  pattern: routePath,
10349
10388
  regex,
@@ -10939,7 +10978,7 @@ async function loadRoutesFromManifest(projectRoot) {
10939
10978
  const layoutServerHooks = [];
10940
10979
  const layoutMiddlewares = [];
10941
10980
  for (const layoutFile of layoutFiles) {
10942
- const layoutHookData = await loadLayoutServerHook(layoutFile);
10981
+ const layoutHookData = await loadLayoutServerHook(layoutFile, projectRoot);
10943
10982
  if (layoutHookData) {
10944
10983
  layoutServerHooks.push(layoutHookData.serverHook);
10945
10984
  layoutMiddlewares.push(layoutHookData.middlewares);
@@ -10948,7 +10987,7 @@ async function loadRoutesFromManifest(projectRoot) {
10948
10987
  layoutMiddlewares.push([]);
10949
10988
  }
10950
10989
  }
10951
- const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(pageDir);
10990
+ const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(pageDir, projectRoot);
10952
10991
  pageRoutes.push({
10953
10992
  pattern: entry.pattern,
10954
10993
  regex,
@@ -11332,7 +11371,14 @@ var FilesystemRouteLoader = class {
11332
11371
  if (hasFilesChanged(this.appDir, this.projectRoot, this.cache.fileStats) || this.cache.apiRoutes.length === 0) {
11333
11372
  const files = getRelevantFiles(this.appDir, this.projectRoot);
11334
11373
  const fileStats = buildFileStats(files);
11335
- this.cache.apiRoutes = await loadApiRoutes(this.appDir);
11374
+ const apiRoutes = await loadApiRoutes(this.appDir);
11375
+ if (!this.cache) {
11376
+ await this.loadRoutes();
11377
+ if (!this.cache) {
11378
+ throw new Error("Failed to initialize route cache");
11379
+ }
11380
+ }
11381
+ this.cache.apiRoutes = apiRoutes;
11336
11382
  this.cache.fileStats = fileStats;
11337
11383
  this.cache.timestamp = Date.now();
11338
11384
  }
@@ -11349,7 +11395,14 @@ var FilesystemRouteLoader = class {
11349
11395
  if (hasFilesChanged(this.appDir, this.projectRoot, this.cache.fileStats) || this.cache.wssRoutes.length === 0) {
11350
11396
  const files = getRelevantFiles(this.appDir, this.projectRoot);
11351
11397
  const fileStats = buildFileStats(files);
11352
- this.cache.wssRoutes = await loadWssRoutes(this.appDir);
11398
+ const wssRoutes = await loadWssRoutes(this.appDir);
11399
+ if (!this.cache) {
11400
+ await this.loadRoutes();
11401
+ if (!this.cache) {
11402
+ throw new Error("Failed to initialize route cache");
11403
+ }
11404
+ }
11405
+ this.cache.wssRoutes = wssRoutes;
11353
11406
  this.cache.fileStats = fileStats;
11354
11407
  this.cache.timestamp = Date.now();
11355
11408
  }
@@ -11366,7 +11419,14 @@ var FilesystemRouteLoader = class {
11366
11419
  if (hasFilesChanged(this.appDir, this.projectRoot, this.cache.fileStats) || this.cache.notFoundRoute === void 0) {
11367
11420
  const files = getRelevantFiles(this.appDir, this.projectRoot);
11368
11421
  const fileStats = buildFileStats(files);
11369
- this.cache.notFoundRoute = await loadNotFoundRouteFromFilesystem(this.appDir);
11422
+ const notFoundRoute = await loadNotFoundRouteFromFilesystem(this.appDir, this.projectRoot);
11423
+ if (!this.cache) {
11424
+ await this.loadRoutes();
11425
+ if (!this.cache) {
11426
+ throw new Error("Failed to initialize route cache");
11427
+ }
11428
+ }
11429
+ this.cache.notFoundRoute = notFoundRoute;
11370
11430
  this.cache.fileStats = fileStats;
11371
11431
  this.cache.timestamp = Date.now();
11372
11432
  }
@@ -11383,7 +11443,14 @@ var FilesystemRouteLoader = class {
11383
11443
  if (hasFilesChanged(this.appDir, this.projectRoot, this.cache.fileStats) || this.cache.errorRoute === void 0) {
11384
11444
  const files = getRelevantFiles(this.appDir, this.projectRoot);
11385
11445
  const fileStats = buildFileStats(files);
11386
- this.cache.errorRoute = await loadErrorRouteFromFilesystem(this.appDir);
11446
+ const errorRoute = await loadErrorRouteFromFilesystem(this.appDir, this.projectRoot);
11447
+ if (!this.cache) {
11448
+ await this.loadRoutes();
11449
+ if (!this.cache) {
11450
+ throw new Error("Failed to initialize route cache");
11451
+ }
11452
+ }
11453
+ this.cache.errorRoute = errorRoute;
11387
11454
  this.cache.fileStats = fileStats;
11388
11455
  this.cache.timestamp = Date.now();
11389
11456
  }
@@ -11459,7 +11526,7 @@ var ManifestRouteLoader = class {
11459
11526
  return chunks;
11460
11527
  }
11461
11528
  };
11462
- async function loadNotFoundRouteFromFilesystem(appDir) {
11529
+ async function loadNotFoundRouteFromFilesystem(appDir, projectRoot) {
11463
11530
  const notFoundCandidates = [
11464
11531
  path11.join(appDir, `${NOT_FOUND_FILE_PREFIX}.tsx`),
11465
11532
  path11.join(appDir, `${NOT_FOUND_FILE_PREFIX}.ts`),
@@ -11483,7 +11550,7 @@ async function loadNotFoundRouteFromFilesystem(appDir) {
11483
11550
  }
11484
11551
  const { loadDefaultExport: loadDefaultExport2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
11485
11552
  const component = await loadDefaultExport2(notFoundFile, {
11486
- projectRoot: appDir
11553
+ projectRoot
11487
11554
  });
11488
11555
  if (!component) {
11489
11556
  return null;
@@ -11496,7 +11563,7 @@ async function loadNotFoundRouteFromFilesystem(appDir) {
11496
11563
  const layoutServerHooks = [];
11497
11564
  const layoutMiddlewares = [];
11498
11565
  for (const layoutFile of layoutFiles) {
11499
- const layoutHookData = await loadLayoutServerHook(layoutFile);
11566
+ const layoutHookData = await loadLayoutServerHook(layoutFile, projectRoot);
11500
11567
  if (layoutHookData) {
11501
11568
  layoutServerHooks.push(layoutHookData.serverHook);
11502
11569
  layoutMiddlewares.push(layoutHookData.middlewares);
@@ -11505,7 +11572,7 @@ async function loadNotFoundRouteFromFilesystem(appDir) {
11505
11572
  layoutMiddlewares.push([]);
11506
11573
  }
11507
11574
  }
11508
- const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(notFoundDir);
11575
+ const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(notFoundDir, projectRoot);
11509
11576
  return {
11510
11577
  pattern: NOT_FOUND_PATTERN,
11511
11578
  regex: new RegExp(`^${NOT_FOUND_PATTERN}/?$`),
@@ -11525,7 +11592,7 @@ async function loadNotFoundRouteFromFilesystem(appDir) {
11525
11592
  generateStaticParams
11526
11593
  };
11527
11594
  }
11528
- async function loadErrorRouteFromFilesystem(appDir) {
11595
+ async function loadErrorRouteFromFilesystem(appDir, projectRoot) {
11529
11596
  const errorCandidates = [
11530
11597
  path11.join(appDir, `${ERROR_FILE_PREFIX}.tsx`),
11531
11598
  path11.join(appDir, `${ERROR_FILE_PREFIX}.ts`),
@@ -11544,7 +11611,7 @@ async function loadErrorRouteFromFilesystem(appDir) {
11544
11611
  }
11545
11612
  const { loadDefaultExport: loadDefaultExport2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
11546
11613
  const component = await loadDefaultExport2(errorFile, {
11547
- projectRoot: appDir
11614
+ projectRoot
11548
11615
  });
11549
11616
  if (!component) {
11550
11617
  return null;
@@ -11556,7 +11623,7 @@ async function loadErrorRouteFromFilesystem(appDir) {
11556
11623
  const layoutServerHooks = [];
11557
11624
  const layoutMiddlewares = [];
11558
11625
  for (const layoutFile of layoutFiles) {
11559
- const layoutHookData = await loadLayoutServerHook(layoutFile);
11626
+ const layoutHookData = await loadLayoutServerHook(layoutFile, projectRoot);
11560
11627
  if (layoutHookData) {
11561
11628
  layoutServerHooks.push(layoutHookData.serverHook);
11562
11629
  layoutMiddlewares.push(layoutHookData.middlewares);
@@ -11565,7 +11632,7 @@ async function loadErrorRouteFromFilesystem(appDir) {
11565
11632
  layoutMiddlewares.push([]);
11566
11633
  }
11567
11634
  }
11568
- const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(appDir);
11635
+ const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(appDir, projectRoot);
11569
11636
  return {
11570
11637
  pattern: ERROR_PATTERN,
11571
11638
  regex: new RegExp(`^${ERROR_PATTERN}/?$`),
@@ -18867,8 +18934,8 @@ async function buildApp(options = {}) {
18867
18934
  const apiRoutes = await loadApiRoutes(appDir);
18868
18935
  const wssRoutes = await loadWssRoutes(appDir);
18869
18936
  const { outDir: serverOutDir } = await buildServerApp(projectRoot, appDir, config);
18870
- const notFoundRoute = await loadNotFoundRouteFromFilesystem(appDir);
18871
- const errorRoute = await loadErrorRouteFromFilesystem(appDir);
18937
+ const notFoundRoute = await loadNotFoundRouteFromFilesystem(appDir, projectRoot);
18938
+ const errorRoute = await loadErrorRouteFromFilesystem(appDir, projectRoot);
18872
18939
  if (!notFoundRoute) {
18873
18940
  console.warn(
18874
18941
  `[framework][build] No not-found route found. Consider creating ${config.directories.app}/${config.conventions.notFound}.tsx`