binhend 2.1.3 → 2.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
package/src/alias.js ADDED
@@ -0,0 +1,64 @@
1
+
2
+ const path = require('path');
3
+ const DefaultModule = require('module');
4
+
5
+ // Protect against improperly customized module constructors by other frameworks/packages
6
+ const Module = module.constructor.length > 1 ? module.constructor : DefaultModule;
7
+
8
+ /** Step 1: Load and parse jsconfig.json */
9
+ const jsconfigPath = path.resolve('jsconfig.json');
10
+
11
+ var jsconfig;
12
+
13
+ try {
14
+ jsconfig = require(jsconfigPath);
15
+ }
16
+ catch (error) {
17
+ return console.log(`[BINEND] Not load 'jsconfig.json' for alias paths.`);
18
+ }
19
+
20
+ const baseUrl = jsconfig?.compilerOptions?.baseUrl || '.';
21
+ const paths = jsconfig?.compilerOptions?.paths || {};
22
+
23
+ /** Step 2: Create an alias map with all possible paths */
24
+ const aliasMap = {};
25
+
26
+ for (const alias in paths) {
27
+ const aliasPath = alias.replace('/*', ''); // Strip the '/*' for matching
28
+ const originalPaths = paths[alias]; // Get the array of paths
29
+
30
+ aliasMap[aliasPath] = originalPaths.map((originalPath) => {
31
+ // Resolve each path to an absolute path
32
+ return path.resolve(baseUrl, originalPath.replace('/*', ''));
33
+ });
34
+ }
35
+
36
+ /** Step 3: Override Module._resolveFilename */
37
+ const originalResolveFilename = Module._resolveFilename;
38
+
39
+ Module._resolveFilename = function (request, parent, isMain, options) {
40
+ // console.log(request, parent, isMain, options);
41
+ for (const alias in aliasMap) {
42
+ if (!request.startsWith(alias)) continue;
43
+ // Loop through the array of resolved paths for this alias
44
+ for (const resolvedPath of aliasMap[alias]) {
45
+ // Construct the new request path
46
+ var newRequest = request.replace(alias, resolvedPath);
47
+
48
+ if (parent.filename.startsWith(binh.webModulePath)) {
49
+ newRequest = newRequest.replace(binh.webSourcePath, binh.webModulePath);
50
+ }
51
+
52
+ // Attempt to resolve the new request
53
+ try {
54
+ return originalResolveFilename.call(this, newRequest, parent, isMain, options);
55
+ } catch (err) {
56
+ // If it fails, continue to the next path
57
+ continue;
58
+ }
59
+ }
60
+ }
61
+
62
+ // Fallback to the original resolution if no alias matches
63
+ return originalResolveFilename.call(this, request, parent, isMain, options);
64
+ };
package/src/binh.js CHANGED
@@ -11,6 +11,9 @@ const { binh } = require('./web/component.method');
11
11
  const { HttpCodes } = require('./utils/httpCodes');
12
12
  const { HTTPS } = require('./https');
13
13
 
14
+ // Run scripts
15
+ require('./alias');
16
+
14
17
  module.exports = {
15
18
  config: {},
16
19
  typeOf,
package/src/web/index.js CHANGED
@@ -41,6 +41,9 @@ function WebBuilder(source, { output } = {}) {
41
41
  server.get('/*', (req, res) => res.sendFile('index.html', { root: web }));
42
42
  });
43
43
 
44
+ binh.webModulePath = module;
45
+ binh.webSourcePath = source;
46
+
44
47
  }
45
48
 
46
49
  module.exports = {