owebjs 1.3.3 → 1.3.5

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.d.ts CHANGED
@@ -42,6 +42,8 @@ declare class Oweb extends _FastifyInstance {
42
42
  _options: OwebOptions;
43
43
  private hmrDirectory;
44
44
  private hmrMatchersDirectory;
45
+ private directory;
46
+ private matchersDirectory;
45
47
  routes: Map<string, any>;
46
48
  constructor(options?: OwebOptions);
47
49
  /**
@@ -17,6 +17,8 @@ class Oweb extends _FastifyInstance {
17
17
  _options = {};
18
18
  hmrDirectory;
19
19
  hmrMatchersDirectory;
20
+ directory;
21
+ matchersDirectory;
20
22
  routes = /* @__PURE__ */ new Map();
21
23
  constructor(options) {
22
24
  super();
@@ -70,6 +72,8 @@ class Oweb extends _FastifyInstance {
70
72
  loadRoutes({ directory, matchersDirectory, hmr }) {
71
73
  if (hmr && !hmr.directory) hmr.directory = directory;
72
74
  if (hmr && !hmr.matchersDirectory) hmr.matchersDirectory = matchersDirectory;
75
+ this.directory = directory;
76
+ this.matchersDirectory = matchersDirectory;
73
77
  if (hmr?.enabled) {
74
78
  this.hmrDirectory = hmr.directory;
75
79
  this.hmrMatchersDirectory = hmr.matchersDirectory;
@@ -85,11 +89,11 @@ class Oweb extends _FastifyInstance {
85
89
  */
86
90
  watch() {
87
91
  watchDirectory(this.hmrDirectory, true, (op, path, content) => {
88
- applyRouteHMR(this, op, this.hmrDirectory, path, content);
92
+ applyRouteHMR(this, op, this.hmrDirectory, this.directory, path, content);
89
93
  });
90
94
  if (this.hmrMatchersDirectory) {
91
95
  watchDirectory(this.hmrMatchersDirectory, true, (op, path, content) => {
92
- applyMatcherHMR(this, op, this.hmrMatchersDirectory, path, content);
96
+ applyMatcherHMR(this, op, this.hmrMatchersDirectory, this.matchersDirectory, path, content);
93
97
  });
94
98
  }
95
99
  }
@@ -31,7 +31,7 @@ function removeExtension(filePath) {
31
31
  return filePath;
32
32
  }
33
33
  __name(removeExtension, "removeExtension");
34
- const applyMatcherHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, filePath, content) => {
34
+ const applyMatcherHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallbackDir, filePath, content) => {
35
35
  let def;
36
36
  const fileName = path.basename(filePath);
37
37
  if (op === "delete-file") {
@@ -57,7 +57,7 @@ const applyMatcherHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, file
57
57
  matcherOverrides[removeExtension(fileName)] = def;
58
58
  }
59
59
  }, "applyMatcherHMR");
60
- const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, path2, content) => {
60
+ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallbackDir, path2, content) => {
61
61
  if (path2.endsWith("hooks.js") || path2.endsWith("hooks.ts")) {
62
62
  warn(`Hot Module Replacement is not supported for hooks. Restart the server for changes to take effect.`, "HMR");
63
63
  return;
@@ -70,7 +70,7 @@ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, path2,
70
70
  }
71
71
  if (op === "new-file") {
72
72
  const start = Date.now();
73
- const files = await walk(workingDir);
73
+ const files = await walk(workingDir, [], fallbackDir);
74
74
  const routes = await generateRoutes(files);
75
75
  routesCache = routes;
76
76
  const f = routes.find((x) => x.fileInfo.filePath == path2);
@@ -79,7 +79,7 @@ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, path2,
79
79
  success(`Route ${f.method.toUpperCase()}:${f.url} created in ${end}ms`, "HMR");
80
80
  } else if (op === "modify-file") {
81
81
  const start = Date.now();
82
- const files = await walk(workingDir);
82
+ const files = await walk(workingDir, [], fallbackDir);
83
83
  const routes = await generateRoutes(files);
84
84
  routesCache = routes;
85
85
  const f = routes.find((x) => x.fileInfo.filePath == path2);
@@ -10,6 +10,9 @@ import { pathToFileURL } from "node:url";
10
10
  import { writeFile, unlink } from "node:fs/promises";
11
11
  import { tmpdir } from "node:os";
12
12
  import { randomBytes } from "node:crypto";
13
+ import { createRequire } from "node:module";
14
+ import { error } from './logger.js';
15
+ const require2 = createRequire(import.meta.url);
13
16
  async function generateFunctionFromTypescript(tsCode, filePath) {
14
17
  const result = babel.transformSync(tsCode, {
15
18
  presets: [
@@ -34,6 +37,18 @@ async function generateFunctionFromTypescript(tsCode, filePath) {
34
37
  const absoluteDepPath = path.resolve(originalFileDir, resolvedPath);
35
38
  const absoluteDepUrl = pathToFileURL(absoluteDepPath).href;
36
39
  importSourceNode.value = absoluteDepUrl;
40
+ } else {
41
+ try {
42
+ const resolvedPath = require2.resolve(relativePath, {
43
+ paths: [
44
+ path.dirname(filePath)
45
+ ]
46
+ });
47
+ const resolvedUrl = pathToFileURL(resolvedPath).href;
48
+ importSourceNode.value = resolvedUrl;
49
+ } catch (_) {
50
+ error(`Could not resolve module "${relativePath}". Please ensure it is installed.`, "HMR");
51
+ }
37
52
  }
38
53
  }
39
54
  });
@@ -15,7 +15,7 @@ const isParentOrGrandparent = /* @__PURE__ */ __name((parentFolderPath, childFol
15
15
  return false;
16
16
  }, "isParentOrGrandparent");
17
17
  const hookPaths = /* @__PURE__ */ new Set();
18
- const walk = /* @__PURE__ */ __name(async (directory, tree = []) => {
18
+ const walk = /* @__PURE__ */ __name(async (directory, tree = [], fallbackDir) => {
19
19
  const results = [];
20
20
  const readDirPriority = readdirSync(directory);
21
21
  readDirPriority.sort((a, b) => {
@@ -39,7 +39,7 @@ const walk = /* @__PURE__ */ __name(async (directory, tree = []) => {
39
39
  results.push(...await walk(filePath, [
40
40
  ...tree,
41
41
  fileName
42
- ]));
42
+ ], fallbackDir));
43
43
  } else {
44
44
  const spread = [
45
45
  ...hookPaths
@@ -63,7 +63,15 @@ const walk = /* @__PURE__ */ __name(async (directory, tree = []) => {
63
63
  } else {
64
64
  useHook = hooks;
65
65
  }
66
- const hooksImport = useHook.map((hookPath) => new URL(hookPath, `file://${__dirname}`).pathname.replaceAll("\\", "/") + "/_hooks.js");
66
+ const hooksImport = useHook.map((hookPath) => {
67
+ if (fallbackDir) {
68
+ const findLastPath = hookPath.replace(process.cwd(), "").split("\\").at(-1);
69
+ const additionNeeded = !fallbackDir.endsWith(`/${findLastPath}`);
70
+ return new URL(path.join(process.cwd(), fallbackDir, additionNeeded ? `/${findLastPath}` : "")).pathname.replaceAll("\\", "/") + "/_hooks.js";
71
+ } else {
72
+ return new URL(hookPath, `file://${__dirname}`).pathname.replaceAll("\\", "/") + "/_hooks.js";
73
+ }
74
+ });
67
75
  const hookFunctions = [];
68
76
  for (const importPath of hooksImport) {
69
77
  const imp = await import(importPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "owebjs",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "A flexible and modern web framework built on top of Fastify",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",