@bobfrankston/importgen 0.1.19 → 0.1.22

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.
@@ -4,7 +4,9 @@
4
4
  "Bash(npm run build:*)",
5
5
  "Bash(npmglobalize)",
6
6
  "Bash(npx tsc)",
7
- "Bash(node index.js:*)"
7
+ "Bash(node index.js:*)",
8
+ "Bash(dir:*)",
9
+ "Bash(ls:*)"
8
10
  ]
9
11
  }
10
12
  }
package/index.js CHANGED
@@ -69,7 +69,7 @@ function resolveEntryPoint(pkg, packageDir) {
69
69
  /**
70
70
  * Recursively collect all dependencies, avoiding circular references
71
71
  */
72
- function collectDependencies(packageJsonPath, visited, dependencies, htmlDir, warnings) {
72
+ function collectDependencies(packageJsonPath, visited, dependencies, htmlDir, warnings, depPaths) {
73
73
  try {
74
74
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
75
75
  const packageDir = path.dirname(packageJsonPath);
@@ -100,6 +100,10 @@ function collectDependencies(packageJsonPath, visited, dependencies, htmlDir, wa
100
100
  warnings.push(warning);
101
101
  depPath = dependencyBackupPath;
102
102
  }
103
+ // Track resolved dependency directory for watch mode
104
+ if (depPaths) {
105
+ depPaths.add(depPath);
106
+ }
103
107
  const depPackageJsonPath = path.join(depPath, 'package.json');
104
108
  if (!fs.existsSync(depPackageJsonPath)) {
105
109
  const warning = `Could not follow path for ${depName} - no package.json found at ${depPath}`;
@@ -111,17 +115,13 @@ function collectDependencies(packageJsonPath, visited, dependencies, htmlDir, wa
111
115
  const depPackageJson = JSON.parse(fs.readFileSync(depPackageJsonPath, 'utf-8'));
112
116
  // Resolve entry point
113
117
  const entryPoint = resolveEntryPoint(depPackageJson, depPath);
114
- // Generate path relative to node_modules (peer to index.html)
115
- let relativePath = `./node_modules/${depName}/${entryPoint}`;
116
- // Normalize path separators and clean up
117
- relativePath = relativePath.replace(/\\/g, '/');
118
- // Remove any leading ./ from entryPoint that might cause double slashes
119
- if (entryPoint.startsWith('./')) {
120
- relativePath = `./node_modules/${depName}/${entryPoint.slice(2)}`;
121
- }
118
+ // Generate path relative to HTML dir using actual resolved location
119
+ const entryFile = entryPoint.startsWith('./') ? entryPoint.slice(2) : entryPoint;
120
+ const absoluteEntryPath = path.join(depPath, entryFile);
121
+ let relativePath = './' + path.relative(htmlDir, absoluteEntryPath).replace(/\\/g, '/');
122
122
  dependencies.set(depName, relativePath);
123
123
  // Recursively process this dependency's dependencies
124
- collectDependencies(depPackageJsonPath, visited, dependencies, htmlDir, warnings);
124
+ collectDependencies(depPackageJsonPath, visited, dependencies, htmlDir, warnings, depPaths);
125
125
  }
126
126
  }
127
127
  catch (e) {
@@ -129,13 +129,16 @@ function collectDependencies(packageJsonPath, visited, dependencies, htmlDir, wa
129
129
  }
130
130
  }
131
131
  function generateImportMap(packageJsonPath, htmlFilePath) {
132
+ const result = { depDirs: [] };
132
133
  try {
133
134
  const htmlDir = path.dirname(htmlFilePath);
134
135
  const visited = new Set();
135
136
  const dependencies = new Map();
137
+ const depPaths = new Set();
136
138
  const warnings = [];
137
139
  // Recursively collect all dependencies
138
- collectDependencies(packageJsonPath, visited, dependencies, htmlDir, warnings);
140
+ collectDependencies(packageJsonPath, visited, dependencies, htmlDir, warnings, depPaths);
141
+ result.depDirs = Array.from(depPaths);
139
142
  // Convert Map to plain object for JSON
140
143
  const imports = {};
141
144
  for (const [name, importPath] of dependencies) {
@@ -174,6 +177,7 @@ function generateImportMap(packageJsonPath, htmlFilePath) {
174
177
  console.error('[generate-importmap] Error:', e.message);
175
178
  process.exit(1);
176
179
  }
180
+ return result;
177
181
  }
178
182
  // Parse CLI arguments
179
183
  const args = process.argv.slice(2);
@@ -203,15 +207,45 @@ if (!htmlFilePath || !fs.existsSync(htmlFilePath)) {
203
207
  process.exit(1);
204
208
  }
205
209
  if (watchMode) {
206
- generateImportMap(packageJsonPath, htmlFilePath);
207
- console.log('[generate-importmap] Watching for changes...');
210
+ let result = generateImportMap(packageJsonPath, htmlFilePath);
211
+ let watchedDepDirs = new Set();
208
212
  const watcher = chokidar.watch([packageJsonPath], {
209
213
  persistent: true,
210
214
  ignoreInitial: true
211
215
  });
212
- watcher.on('change', () => generateImportMap(packageJsonPath, htmlFilePath));
216
+ /** Update watcher to include/remove dependency directories */
217
+ function syncWatchedDeps(depDirs) {
218
+ const newDirs = new Set(depDirs);
219
+ // Remove dirs no longer in dependency list
220
+ for (const dir of watchedDepDirs) {
221
+ if (!newDirs.has(dir)) {
222
+ watcher.unwatch(dir);
223
+ }
224
+ }
225
+ // Add new dirs
226
+ for (const dir of newDirs) {
227
+ if (!watchedDepDirs.has(dir)) {
228
+ watcher.add(dir);
229
+ }
230
+ }
231
+ watchedDepDirs = newDirs;
232
+ }
233
+ syncWatchedDeps(result.depDirs);
234
+ const onChange = () => {
235
+ result = generateImportMap(packageJsonPath, htmlFilePath);
236
+ syncWatchedDeps(result.depDirs);
237
+ };
238
+ watcher.on('change', onChange);
239
+ watcher.on('add', onChange);
240
+ watcher.on('unlink', onChange);
213
241
  watcher.on('ready', () => {
214
- console.log('[generate-importmap] Watcher is ready and running.');
242
+ const total = 1 + watchedDepDirs.size;
243
+ console.log(`[generate-importmap] Watching ${total} directories for changes...`);
244
+ if (watchedDepDirs.size > 0) {
245
+ for (const dir of watchedDepDirs) {
246
+ console.log(` ${dir}`);
247
+ }
248
+ }
215
249
  });
216
250
  watcher.on('error', (error) => {
217
251
  console.error('[generate-importmap] Watcher error:', error.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/importgen",
3
- "version": "0.1.19",
3
+ "version": "0.1.22",
4
4
  "description": "Generate ES Module import maps from package.json dependencies for native browser module loading",
5
5
  "main": "index.js",
6
6
  "bin": {