owebjs 1.3.4 → 1.3.7

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);
@@ -7,13 +7,54 @@ import generate from "@babel/generator";
7
7
  import path from "node:path";
8
8
  import babel from "@babel/core";
9
9
  import { pathToFileURL } from "node:url";
10
- import { writeFile, unlink } from "node:fs/promises";
10
+ import { writeFile, unlink, readFile } from "node:fs/promises";
11
+ import { existsSync } from "node:fs";
11
12
  import { tmpdir } from "node:os";
12
13
  import { randomBytes } from "node:crypto";
13
14
  import { createRequire } from "node:module";
14
15
  import { error } from './logger.js';
15
16
  const require2 = createRequire(import.meta.url);
17
+ async function getAliasesFromTsConfig(tsConfigPath) {
18
+ if (!existsSync(tsConfigPath)) {
19
+ return null;
20
+ }
21
+ try {
22
+ const tsConfigFile = await readFile(tsConfigPath, "utf-8");
23
+ const json = JSON.parse(tsConfigFile.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ""));
24
+ const compilerOptions = json.compilerOptions || {};
25
+ const baseUrl = path.resolve(path.dirname(tsConfigPath), compilerOptions.baseUrl || ".");
26
+ const paths = compilerOptions.paths || {};
27
+ return {
28
+ paths,
29
+ baseUrl
30
+ };
31
+ } catch (error2) {
32
+ console.error("Error reading or parsing tsconfig.json:", error2);
33
+ return null;
34
+ }
35
+ }
36
+ __name(getAliasesFromTsConfig, "getAliasesFromTsConfig");
37
+ function resolveAlias(importPath, tsConfigPaths, baseUrl) {
38
+ for (const alias in tsConfigPaths) {
39
+ const aliasPattern = new RegExp(`^${alias.replace("*", "(.*)")}$`);
40
+ const match = importPath.match(aliasPattern);
41
+ if (match) {
42
+ const [_fullMatch, restOfPath] = match;
43
+ const targetPaths = tsConfigPaths[alias];
44
+ const targetPath = targetPaths[0];
45
+ if (!alias.endsWith("*")) {
46
+ return path.resolve(baseUrl, targetPath);
47
+ }
48
+ const resolvedPath = targetPath.replace("*", restOfPath);
49
+ return path.resolve(baseUrl, resolvedPath);
50
+ }
51
+ }
52
+ return null;
53
+ }
54
+ __name(resolveAlias, "resolveAlias");
16
55
  async function generateFunctionFromTypescript(tsCode, filePath) {
56
+ const tsConfigPath = path.join(process.cwd(), "tsconfig.json");
57
+ const tsConfig = await getAliasesFromTsConfig(tsConfigPath);
17
58
  const result = babel.transformSync(tsCode, {
18
59
  presets: [
19
60
  "@babel/preset-typescript"
@@ -24,32 +65,39 @@ async function generateFunctionFromTypescript(tsCode, filePath) {
24
65
  const ast = parse(jsCode, {
25
66
  sourceType: "module"
26
67
  });
68
+ const fileDir = path.dirname(filePath);
27
69
  traverse.default(ast, {
28
70
  ImportDeclaration(astPath) {
29
71
  const importSourceNode = astPath.node.source;
30
- let relativePath = importSourceNode.value;
31
- if (relativePath.startsWith(".")) {
32
- let resolvedPath = relativePath;
33
- if (path.extname(relativePath) === "") {
34
- resolvedPath = relativePath + ".ts";
72
+ const importPath = importSourceNode.value;
73
+ let resolvedUrl = null;
74
+ if (tsConfig && tsConfig.paths) {
75
+ const resolvedAliasPath = resolveAlias(importPath, tsConfig.paths, tsConfig.baseUrl);
76
+ if (resolvedAliasPath) {
77
+ const finalPath = path.extname(resolvedAliasPath) === "" ? resolvedAliasPath + ".ts" : resolvedAliasPath;
78
+ resolvedUrl = pathToFileURL(finalPath).href;
35
79
  }
36
- const originalFileDir = path.dirname(filePath);
37
- const absoluteDepPath = path.resolve(originalFileDir, resolvedPath);
38
- const absoluteDepUrl = pathToFileURL(absoluteDepPath).href;
39
- importSourceNode.value = absoluteDepUrl;
40
- } else {
80
+ }
81
+ if (!resolvedUrl && importPath.startsWith(".")) {
82
+ const resolvedPathWithExt = path.extname(importPath) === "" ? importPath + ".ts" : importPath;
83
+ const absoluteDepPath = path.resolve(fileDir, resolvedPathWithExt);
84
+ resolvedUrl = pathToFileURL(absoluteDepPath).href;
85
+ }
86
+ if (!resolvedUrl) {
41
87
  try {
42
- const resolvedPath = require2.resolve(relativePath, {
88
+ const resolvedNodeModulePath = require2.resolve(importPath, {
43
89
  paths: [
44
- path.dirname(filePath)
90
+ fileDir
45
91
  ]
46
92
  });
47
- const resolvedUrl = pathToFileURL(resolvedPath).href;
48
- importSourceNode.value = resolvedUrl;
93
+ resolvedUrl = pathToFileURL(resolvedNodeModulePath).href;
49
94
  } catch (_) {
50
- error(`Could not resolve module "${relativePath}". Please ensure it is installed.`, "HMR");
95
+ error(`Could not resolve import path for: ${importPath} in ${filePath}.`, "HMR");
51
96
  }
52
97
  }
98
+ if (resolvedUrl) {
99
+ importSourceNode.value = resolvedUrl;
100
+ }
53
101
  }
54
102
  });
55
103
  const { code: modifiedCode } = generate.default(ast);
@@ -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);
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "owebjs",
3
- "version": "1.3.4",
3
+ "version": "1.3.7",
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",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "scripts": {
20
20
  "start": "node .",
21
- "build": "tsup",
21
+ "build": "tsup && node scripts/copyBinaries",
22
22
  "dev": "tsup && node .",
23
23
  "test": "tsup && node scripts/copyBinaries && node test/index.js",
24
24
  "format": "prettier --write . --ignore-path .gitignore"