@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/cli.mjs CHANGED
@@ -10073,7 +10073,7 @@ var NAMING = {
10073
10073
  // Files
10074
10074
  SERVER_HOOK: "server.hook"
10075
10075
  };
10076
- async function loadServerHookForDir(currentDir) {
10076
+ async function loadServerHookForDir(currentDir, projectRoot) {
10077
10077
  const isDev = process.env.NODE_ENV === "development";
10078
10078
  const isBuild = process.env.LOLY_BUILD === "1";
10079
10079
  let file = null;
@@ -10099,10 +10099,24 @@ async function loadServerHookForDir(currentDir) {
10099
10099
  generateStaticParams: null
10100
10100
  };
10101
10101
  }
10102
+ let resolvedProjectRoot = projectRoot;
10103
+ if (!resolvedProjectRoot) {
10104
+ let current = path3.resolve(currentDir);
10105
+ while (current !== path3.dirname(current)) {
10106
+ if (fs3.existsSync(path3.join(current, "package.json"))) {
10107
+ resolvedProjectRoot = current;
10108
+ break;
10109
+ }
10110
+ current = path3.dirname(current);
10111
+ }
10112
+ if (!resolvedProjectRoot) {
10113
+ resolvedProjectRoot = currentDir;
10114
+ }
10115
+ }
10102
10116
  let mod;
10103
10117
  try {
10104
10118
  const { loadModule: loadModule2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
10105
- mod = await loadModule2(file, { projectRoot: currentDir });
10119
+ mod = await loadModule2(file, { projectRoot: resolvedProjectRoot });
10106
10120
  } catch (error) {
10107
10121
  console.error(
10108
10122
  `[framework][server-hook] Error loading server hook from ${file}:`,
@@ -10145,7 +10159,7 @@ async function loadServerHookForDir(currentDir) {
10145
10159
  generateStaticParams
10146
10160
  };
10147
10161
  }
10148
- async function loadLayoutServerHook(layoutFile) {
10162
+ async function loadLayoutServerHook(layoutFile, projectRoot) {
10149
10163
  const layoutDir = path3.dirname(layoutFile);
10150
10164
  const layoutExt = path3.extname(layoutFile);
10151
10165
  const layoutBasename = path3.basename(layoutFile, layoutExt);
@@ -10171,7 +10185,20 @@ async function loadLayoutServerHook(layoutFile) {
10171
10185
  }
10172
10186
  try {
10173
10187
  const { loadModule: loadModule2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
10174
- const mod = await loadModule2(file, { projectRoot: path3.dirname(file) });
10188
+ if (!projectRoot) {
10189
+ let current = path3.dirname(layoutFile);
10190
+ while (current !== path3.dirname(current)) {
10191
+ if (fs3.existsSync(path3.join(current, "package.json"))) {
10192
+ projectRoot = current;
10193
+ break;
10194
+ }
10195
+ current = path3.dirname(current);
10196
+ }
10197
+ if (!projectRoot) {
10198
+ projectRoot = path3.dirname(layoutFile);
10199
+ }
10200
+ }
10201
+ const mod = await loadModule2(file, { projectRoot });
10175
10202
  const serverHook = typeof mod?.getServerSideProps === "function" ? mod.getServerSideProps : null;
10176
10203
  let middlewares = [];
10177
10204
  const rawMiddlewares = mod?.[NAMING.BEFORE_MIDDLEWARES];
@@ -10259,6 +10286,15 @@ async function loadRoutes(appDir) {
10259
10286
  if (!fs4.existsSync(appDir)) {
10260
10287
  return [];
10261
10288
  }
10289
+ let projectRoot = appDir;
10290
+ let current = path4.resolve(appDir);
10291
+ while (current !== path4.dirname(current)) {
10292
+ if (fs4.existsSync(path4.join(current, "package.json"))) {
10293
+ projectRoot = current;
10294
+ break;
10295
+ }
10296
+ current = path4.dirname(current);
10297
+ }
10262
10298
  const routes = [];
10263
10299
  async function walk(currentDir) {
10264
10300
  const entries = fs4.readdirSync(currentDir, { withFileTypes: true });
@@ -10277,7 +10313,7 @@ async function loadRoutes(appDir) {
10277
10313
  const { regex, paramNames } = buildRegexFromRoutePath(routePath);
10278
10314
  const { loadDefaultExport: loadDefaultExport2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
10279
10315
  const component = await loadDefaultExport2(fullPath, {
10280
- projectRoot: appDir
10316
+ projectRoot
10281
10317
  });
10282
10318
  if (!component) {
10283
10319
  continue;
@@ -10289,7 +10325,7 @@ async function loadRoutes(appDir) {
10289
10325
  const layoutServerHooks = [];
10290
10326
  const layoutMiddlewares = [];
10291
10327
  for (const layoutFile of layoutFiles) {
10292
- const layoutHookData = await loadLayoutServerHook(layoutFile);
10328
+ const layoutHookData = await loadLayoutServerHook(layoutFile, projectRoot);
10293
10329
  if (layoutHookData) {
10294
10330
  layoutServerHooks.push(layoutHookData.serverHook);
10295
10331
  layoutMiddlewares.push(layoutHookData.middlewares);
@@ -10298,7 +10334,7 @@ async function loadRoutes(appDir) {
10298
10334
  layoutMiddlewares.push([]);
10299
10335
  }
10300
10336
  }
10301
- const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(currentDir);
10337
+ const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(currentDir, projectRoot);
10302
10338
  routes.push({
10303
10339
  pattern: routePath,
10304
10340
  regex,
@@ -10894,7 +10930,7 @@ async function loadRoutesFromManifest(projectRoot) {
10894
10930
  const layoutServerHooks = [];
10895
10931
  const layoutMiddlewares = [];
10896
10932
  for (const layoutFile of layoutFiles) {
10897
- const layoutHookData = await loadLayoutServerHook(layoutFile);
10933
+ const layoutHookData = await loadLayoutServerHook(layoutFile, projectRoot);
10898
10934
  if (layoutHookData) {
10899
10935
  layoutServerHooks.push(layoutHookData.serverHook);
10900
10936
  layoutMiddlewares.push(layoutHookData.middlewares);
@@ -10903,7 +10939,7 @@ async function loadRoutesFromManifest(projectRoot) {
10903
10939
  layoutMiddlewares.push([]);
10904
10940
  }
10905
10941
  }
10906
- const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(pageDir);
10942
+ const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(pageDir, projectRoot);
10907
10943
  pageRoutes.push({
10908
10944
  pattern: entry.pattern,
10909
10945
  regex,
@@ -11287,7 +11323,14 @@ var FilesystemRouteLoader = class {
11287
11323
  if (hasFilesChanged(this.appDir, this.projectRoot, this.cache.fileStats) || this.cache.apiRoutes.length === 0) {
11288
11324
  const files = getRelevantFiles(this.appDir, this.projectRoot);
11289
11325
  const fileStats = buildFileStats(files);
11290
- this.cache.apiRoutes = await loadApiRoutes(this.appDir);
11326
+ const apiRoutes = await loadApiRoutes(this.appDir);
11327
+ if (!this.cache) {
11328
+ await this.loadRoutes();
11329
+ if (!this.cache) {
11330
+ throw new Error("Failed to initialize route cache");
11331
+ }
11332
+ }
11333
+ this.cache.apiRoutes = apiRoutes;
11291
11334
  this.cache.fileStats = fileStats;
11292
11335
  this.cache.timestamp = Date.now();
11293
11336
  }
@@ -11304,7 +11347,14 @@ var FilesystemRouteLoader = class {
11304
11347
  if (hasFilesChanged(this.appDir, this.projectRoot, this.cache.fileStats) || this.cache.wssRoutes.length === 0) {
11305
11348
  const files = getRelevantFiles(this.appDir, this.projectRoot);
11306
11349
  const fileStats = buildFileStats(files);
11307
- this.cache.wssRoutes = await loadWssRoutes(this.appDir);
11350
+ const wssRoutes = await loadWssRoutes(this.appDir);
11351
+ if (!this.cache) {
11352
+ await this.loadRoutes();
11353
+ if (!this.cache) {
11354
+ throw new Error("Failed to initialize route cache");
11355
+ }
11356
+ }
11357
+ this.cache.wssRoutes = wssRoutes;
11308
11358
  this.cache.fileStats = fileStats;
11309
11359
  this.cache.timestamp = Date.now();
11310
11360
  }
@@ -11321,7 +11371,14 @@ var FilesystemRouteLoader = class {
11321
11371
  if (hasFilesChanged(this.appDir, this.projectRoot, this.cache.fileStats) || this.cache.notFoundRoute === void 0) {
11322
11372
  const files = getRelevantFiles(this.appDir, this.projectRoot);
11323
11373
  const fileStats = buildFileStats(files);
11324
- this.cache.notFoundRoute = await loadNotFoundRouteFromFilesystem(this.appDir);
11374
+ const notFoundRoute = await loadNotFoundRouteFromFilesystem(this.appDir, this.projectRoot);
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.notFoundRoute = notFoundRoute;
11325
11382
  this.cache.fileStats = fileStats;
11326
11383
  this.cache.timestamp = Date.now();
11327
11384
  }
@@ -11338,7 +11395,14 @@ var FilesystemRouteLoader = class {
11338
11395
  if (hasFilesChanged(this.appDir, this.projectRoot, this.cache.fileStats) || this.cache.errorRoute === void 0) {
11339
11396
  const files = getRelevantFiles(this.appDir, this.projectRoot);
11340
11397
  const fileStats = buildFileStats(files);
11341
- this.cache.errorRoute = await loadErrorRouteFromFilesystem(this.appDir);
11398
+ const errorRoute = await loadErrorRouteFromFilesystem(this.appDir, this.projectRoot);
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.errorRoute = errorRoute;
11342
11406
  this.cache.fileStats = fileStats;
11343
11407
  this.cache.timestamp = Date.now();
11344
11408
  }
@@ -11414,7 +11478,7 @@ var ManifestRouteLoader = class {
11414
11478
  return chunks;
11415
11479
  }
11416
11480
  };
11417
- async function loadNotFoundRouteFromFilesystem(appDir) {
11481
+ async function loadNotFoundRouteFromFilesystem(appDir, projectRoot) {
11418
11482
  const notFoundCandidates = [
11419
11483
  path10.join(appDir, `${NOT_FOUND_FILE_PREFIX}.tsx`),
11420
11484
  path10.join(appDir, `${NOT_FOUND_FILE_PREFIX}.ts`),
@@ -11438,7 +11502,7 @@ async function loadNotFoundRouteFromFilesystem(appDir) {
11438
11502
  }
11439
11503
  const { loadDefaultExport: loadDefaultExport2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
11440
11504
  const component = await loadDefaultExport2(notFoundFile, {
11441
- projectRoot: appDir
11505
+ projectRoot
11442
11506
  });
11443
11507
  if (!component) {
11444
11508
  return null;
@@ -11451,7 +11515,7 @@ async function loadNotFoundRouteFromFilesystem(appDir) {
11451
11515
  const layoutServerHooks = [];
11452
11516
  const layoutMiddlewares = [];
11453
11517
  for (const layoutFile of layoutFiles) {
11454
- const layoutHookData = await loadLayoutServerHook(layoutFile);
11518
+ const layoutHookData = await loadLayoutServerHook(layoutFile, projectRoot);
11455
11519
  if (layoutHookData) {
11456
11520
  layoutServerHooks.push(layoutHookData.serverHook);
11457
11521
  layoutMiddlewares.push(layoutHookData.middlewares);
@@ -11460,7 +11524,7 @@ async function loadNotFoundRouteFromFilesystem(appDir) {
11460
11524
  layoutMiddlewares.push([]);
11461
11525
  }
11462
11526
  }
11463
- const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(notFoundDir);
11527
+ const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(notFoundDir, projectRoot);
11464
11528
  return {
11465
11529
  pattern: NOT_FOUND_PATTERN,
11466
11530
  regex: new RegExp(`^${NOT_FOUND_PATTERN}/?$`),
@@ -11480,7 +11544,7 @@ async function loadNotFoundRouteFromFilesystem(appDir) {
11480
11544
  generateStaticParams
11481
11545
  };
11482
11546
  }
11483
- async function loadErrorRouteFromFilesystem(appDir) {
11547
+ async function loadErrorRouteFromFilesystem(appDir, projectRoot) {
11484
11548
  const errorCandidates = [
11485
11549
  path10.join(appDir, `${ERROR_FILE_PREFIX}.tsx`),
11486
11550
  path10.join(appDir, `${ERROR_FILE_PREFIX}.ts`),
@@ -11499,7 +11563,7 @@ async function loadErrorRouteFromFilesystem(appDir) {
11499
11563
  }
11500
11564
  const { loadDefaultExport: loadDefaultExport2 } = await Promise.resolve().then(() => (init_module_loader(), module_loader_exports));
11501
11565
  const component = await loadDefaultExport2(errorFile, {
11502
- projectRoot: appDir
11566
+ projectRoot
11503
11567
  });
11504
11568
  if (!component) {
11505
11569
  return null;
@@ -11511,7 +11575,7 @@ async function loadErrorRouteFromFilesystem(appDir) {
11511
11575
  const layoutServerHooks = [];
11512
11576
  const layoutMiddlewares = [];
11513
11577
  for (const layoutFile of layoutFiles) {
11514
- const layoutHookData = await loadLayoutServerHook(layoutFile);
11578
+ const layoutHookData = await loadLayoutServerHook(layoutFile, projectRoot);
11515
11579
  if (layoutHookData) {
11516
11580
  layoutServerHooks.push(layoutHookData.serverHook);
11517
11581
  layoutMiddlewares.push(layoutHookData.middlewares);
@@ -11520,7 +11584,7 @@ async function loadErrorRouteFromFilesystem(appDir) {
11520
11584
  layoutMiddlewares.push([]);
11521
11585
  }
11522
11586
  }
11523
- const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(appDir);
11587
+ const { middlewares, serverHook, dynamic, generateStaticParams } = await loadServerHookForDir(appDir, projectRoot);
11524
11588
  return {
11525
11589
  pattern: ERROR_PATTERN,
11526
11590
  regex: new RegExp(`^${ERROR_PATTERN}/?$`),
@@ -14536,6 +14600,9 @@ var INIT_FILE_NAME = "init.server";
14536
14600
  async function runInitIfExists(projectRoot, serverData) {
14537
14601
  try {
14538
14602
  let mod = await getServerFile(projectRoot, INIT_FILE_NAME);
14603
+ if (!mod) {
14604
+ return {};
14605
+ }
14539
14606
  if (typeof mod?.init === "function") {
14540
14607
  const serverContext = { ...serverData };
14541
14608
  await mod.init({ serverContext });
@@ -15283,8 +15350,8 @@ async function buildApp(options = {}) {
15283
15350
  const apiRoutes = await loadApiRoutes(appDir);
15284
15351
  const wssRoutes = await loadWssRoutes(appDir);
15285
15352
  const { outDir: serverOutDir } = await buildServerApp(projectRoot, appDir, config);
15286
- const notFoundRoute = await loadNotFoundRouteFromFilesystem(appDir);
15287
- const errorRoute = await loadErrorRouteFromFilesystem(appDir);
15353
+ const notFoundRoute = await loadNotFoundRouteFromFilesystem(appDir, projectRoot);
15354
+ const errorRoute = await loadErrorRouteFromFilesystem(appDir, projectRoot);
15288
15355
  if (!notFoundRoute) {
15289
15356
  console.warn(
15290
15357
  `[framework][build] No not-found route found. Consider creating ${config.directories.app}/${config.conventions.notFound}.tsx`