@ecopages/core 0.2.0-alpha.38 → 0.2.0-alpha.39

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": "@ecopages/core",
3
- "version": "0.2.0-alpha.38",
3
+ "version": "0.2.0-alpha.39",
4
4
  "description": "Core package for Ecopages",
5
5
  "keywords": [
6
6
  "ecopages",
@@ -17,7 +17,7 @@
17
17
  "directory": "packages/core"
18
18
  },
19
19
  "dependencies": {
20
- "@ecopages/file-system": "0.2.0-alpha.38",
20
+ "@ecopages/file-system": "0.2.0-alpha.39",
21
21
  "@ecopages/logger": "^0.2.3",
22
22
  "@ecopages/scripts-injector": "^0.1.5",
23
23
  "@worker-tools/html-rewriter": "0.1.0-pre.19",
@@ -1,7 +1,42 @@
1
1
  import type { NodeModuleScriptAsset } from '../../assets.types.js';
2
2
  import { BaseScriptProcessor } from '../base/base-script-processor.js';
3
+ /**
4
+ * Processes browser script assets whose entrypoint is referenced by package specifier.
5
+ *
6
+ * @remarks
7
+ * Resolution stays app-boundary-first: prefer the active build adapter, then fall back
8
+ * to ESM export-map resolution, and only then probe a small set of literal file paths.
9
+ */
3
10
  export declare class NodeModuleScriptProcessor extends BaseScriptProcessor<NodeModuleScriptAsset> {
4
11
  process(dep: NodeModuleScriptAsset): Promise<import("../../assets.types.js").ProcessedAsset>;
12
+ /**
13
+ * Resolves a node-module script entry from the current app boundary.
14
+ *
15
+ * @remarks
16
+ * The build adapter remains the primary resolution surface because Bun/native
17
+ * host-owned builds may have a more accurate view of aliases and package
18
+ * ownership than core does. The local fallback only runs when that adapter
19
+ * resolution is unavailable.
20
+ */
5
21
  private resolveModulePath;
22
+ /**
23
+ * Resolves browser-owned script specifiers without relying on CommonJS resolution.
24
+ *
25
+ * @remarks
26
+ * This path intentionally stays ESM-first because these assets are emitted as
27
+ * browser scripts. We first ask Node's ESM resolver to evaluate the package
28
+ * export map from the app boundary. If that still fails, we fall back to a
29
+ * bounded filesystem probe so direct file installs and package subpaths like
30
+ * `pkg/client/entry` still resolve when the export map does not cover them.
31
+ */
6
32
  private resolveModulePathFallback;
33
+ /**
34
+ * Returns the file candidates we accept during the final literal filesystem probe.
35
+ *
36
+ * @remarks
37
+ * This is intentionally small and browser-entry-oriented: direct files, common
38
+ * JS extensions, and `index.*` entrypoints. If a package needs anything more
39
+ * exotic, it should resolve through the adapter or ESM export-map path above.
40
+ */
41
+ private getFallbackCandidatePaths;
7
42
  }
@@ -1,4 +1,5 @@
1
1
  import path from "node:path";
2
+ import { fileURLToPath, pathToFileURL } from "node:url";
2
3
  import { fileSystem } from "@ecopages/file-system";
3
4
  import { getAppBuildAdapter } from "../../../../../build/build-adapter.js";
4
5
  import { BaseScriptProcessor } from "../base/base-script-processor.js";
@@ -46,6 +47,15 @@ class NodeModuleScriptProcessor extends BaseScriptProcessor {
46
47
  };
47
48
  });
48
49
  }
50
+ /**
51
+ * Resolves a node-module script entry from the current app boundary.
52
+ *
53
+ * @remarks
54
+ * The build adapter remains the primary resolution surface because Bun/native
55
+ * host-owned builds may have a more accurate view of aliases and package
56
+ * ownership than core does. The local fallback only runs when that adapter
57
+ * resolution is unavailable.
58
+ */
49
59
  resolveModulePath(importPath, rootDir) {
50
60
  if (path.isAbsolute(importPath) && fileSystem.exists(importPath)) {
51
61
  return importPath;
@@ -56,13 +66,28 @@ class NodeModuleScriptProcessor extends BaseScriptProcessor {
56
66
  return this.resolveModulePathFallback(importPath, rootDir);
57
67
  }
58
68
  }
69
+ /**
70
+ * Resolves browser-owned script specifiers without relying on CommonJS resolution.
71
+ *
72
+ * @remarks
73
+ * This path intentionally stays ESM-first because these assets are emitted as
74
+ * browser scripts. We first ask Node's ESM resolver to evaluate the package
75
+ * export map from the app boundary. If that still fails, we fall back to a
76
+ * bounded filesystem probe so direct file installs and package subpaths like
77
+ * `pkg/client/entry` still resolve when the export map does not cover them.
78
+ */
59
79
  resolveModulePathFallback(importPath, rootDir, maxDepth = 5) {
80
+ try {
81
+ return fileURLToPath(import.meta.resolve(importPath, pathToFileURL(path.join(rootDir, "package.json")).href));
82
+ } catch {
83
+ }
60
84
  let currentDir = rootDir;
61
85
  let remainingDepth = maxDepth;
62
86
  while (remainingDepth >= 0) {
63
- const modulePath = path.join(currentDir, "node_modules", importPath);
64
- if (fileSystem.exists(modulePath)) {
65
- return modulePath;
87
+ for (const candidatePath of this.getFallbackCandidatePaths(currentDir, importPath)) {
88
+ if (fileSystem.exists(candidatePath)) {
89
+ return candidatePath;
90
+ }
66
91
  }
67
92
  const parentDir = path.dirname(currentDir);
68
93
  if (parentDir === currentDir) {
@@ -73,6 +98,26 @@ class NodeModuleScriptProcessor extends BaseScriptProcessor {
73
98
  }
74
99
  throw new Error(`Could not resolve module '${importPath}' from '${rootDir}'`);
75
100
  }
101
+ /**
102
+ * Returns the file candidates we accept during the final literal filesystem probe.
103
+ *
104
+ * @remarks
105
+ * This is intentionally small and browser-entry-oriented: direct files, common
106
+ * JS extensions, and `index.*` entrypoints. If a package needs anything more
107
+ * exotic, it should resolve through the adapter or ESM export-map path above.
108
+ */
109
+ getFallbackCandidatePaths(rootDir, importPath) {
110
+ const moduleBasePath = path.join(rootDir, "node_modules", importPath);
111
+ return [
112
+ moduleBasePath,
113
+ `${moduleBasePath}.js`,
114
+ `${moduleBasePath}.mjs`,
115
+ `${moduleBasePath}.cjs`,
116
+ path.join(moduleBasePath, "index.js"),
117
+ path.join(moduleBasePath, "index.mjs"),
118
+ path.join(moduleBasePath, "index.cjs")
119
+ ];
120
+ }
76
121
  }
77
122
  export {
78
123
  NodeModuleScriptProcessor