@bobfrankston/importgen 0.1.36 → 0.1.37

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.
Files changed (2) hide show
  1. package/index.js +26 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -211,13 +211,35 @@ function collectDependencies(packageJsonPath, visited, dependencies, htmlDir, no
211
211
  searchDir = parent;
212
212
  }
213
213
  if (!depPrefix) {
214
- // Not in any ancestor node_modules (e.g. file: dep outside
215
- // the workspace, no junction yet). Use the resolved real
216
- // path relative to htmlDir strictly more correct than
217
- // the old `nodeModulesPrefix/depName` guess.
214
+ // Transitive dep that npm placed in a NESTED node_modules
215
+ // (not hoisted to an ancestor of htmlDir). nodeModulesPrefix
216
+ // tracks that nested location through the recursion. Use it
217
+ // when the package physically resolves there — this is a
218
+ // path under the app's node_modules tree that the browser
219
+ // can actually fetch (junctions traverse transparently).
220
+ const nestedCandidate = `${nodeModulesPrefix}/${depName}`;
221
+ if (fs.existsSync(path.join(htmlDir, nestedCandidate))) {
222
+ depPrefix = nestedCandidate;
223
+ }
224
+ }
225
+ if (!depPrefix) {
226
+ // Last resort: not found in any node_modules at all (e.g. a
227
+ // file: dep with no junction). Use the resolved real path —
228
+ // which may escape the app dir; the RED FLAG below catches that.
218
229
  const rel = path.relative(htmlDir, depPath).split(path.sep).join('/');
219
230
  depPrefix = rel.startsWith('.') ? rel : `./${rel}`;
220
231
  }
232
+ // RED FLAG: a `../`-escaping path leaves the HTML file's
233
+ // directory. The dev server's served root rarely extends
234
+ // above the app dir, so the browser will almost certainly
235
+ // 404 on it and the page breaks. We still emit the entry
236
+ // (it's the only real location we found), but flag it in
237
+ // the generated HTML so the breakage is not silent.
238
+ if (depPrefix.startsWith('../')) {
239
+ const warning = `${depName} -> ${depPrefix}/${entryFile} escapes the app directory; the browser will likely fail to load it unless the dev server serves a high-enough root. Add ${depName} to the app's node_modules.`;
240
+ console.warn(`${ts()} [generate-importmap] Warning: ${warning}`);
241
+ warnings.push(warning);
242
+ }
221
243
  dependencies.set(depName, `${depPrefix}/${entryFile}`);
222
244
  // Bug 3: trailing-slash variant for deep imports
223
245
  // (`import "pkg/sub.js"` requires `"pkg/": "<root>/"`).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/importgen",
3
- "version": "0.1.36",
3
+ "version": "0.1.37",
4
4
  "description": "Generate ES Module import maps from package.json dependencies for native browser module loading",
5
5
  "main": "index.js",
6
6
  "types": "./index.d.ts",