pkgprn 0.2.3 → 0.3.0

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # package-prune
2
2
 
3
- Prune `package.json` before publishing to npm. Removes `devDependencies`, strips unnecessary scripts, cleans up junk files, optimizes the `files` array, and optionally flattens dist directories so your published package is lean and clean.
3
+ Prune `package.json` before publishing to npm. Removes `devDependencies`, strips unnecessary scripts, cleans up junk files, optimizes the `files` array, and optionally flattens dist directories - so your published package is lean and clean.
4
4
 
5
5
  ## Installation
6
6
 
@@ -47,12 +47,12 @@ The tool reads the `package.json` in the current working directory, applies all
47
47
 
48
48
  By default, `pkgprn` performs the following steps:
49
49
 
50
- 1. **Removes `devDependencies`** Strips the entire `devDependencies` field from `package.json`.
51
- 2. **Removes `packageManager`** Strips the `packageManager` field.
52
- 3. **Prunes scripts** Removes scripts that are not relevant to package consumers (based on the selected [profile](#profiles)).
53
- 4. **Removes junk files** Deletes OS and editor artifacts (`.DS_Store`, `*.orig`, `.*.swp`, `._*`, etc.) from the package directory.
54
- 5. **Optimizes the `files` array** Collapses individual file entries into their parent directory when all files in that directory are already listed, and removes entries that npm always includes automatically (`package.json`, `README`, `LICENSE`).
55
- 6. **Cleans up unlisted files** Removes files and directories not covered by the `files` array, then drops the `files` field itself (since only the included files remain on disk).
50
+ 1. **Removes `devDependencies`** - Strips the entire `devDependencies` field from `package.json`.
51
+ 2. **Removes `packageManager`** - Strips the `packageManager` field.
52
+ 3. **Prunes scripts** - Removes scripts that are not relevant to package consumers (based on the selected [profile](#profiles)).
53
+ 4. **Removes junk files** - Deletes OS and editor artifacts (`.DS_Store`, `*.orig`, `.*.swp`, `._*`, etc.) from the package directory.
54
+ 5. **Optimizes the `files` array** - Collapses individual file entries into their parent directory when all files in that directory are already listed, and removes entries that npm always includes automatically (`package.json`, `README`, `LICENSE`).
55
+ 6. **Cleans up unlisted files** - Removes files and directories not covered by the `files` array, then drops the `files` field itself (since only the included files remain on disk).
56
56
 
57
57
  Additional optional features can be enabled via flags:
58
58
 
@@ -120,8 +120,9 @@ pkgprn --flatten dist,lib
120
120
  3. Moves all files to the root (preserving subdirectory structure relative to the dist directory).
121
121
  4. Removes the now-empty dist directory.
122
122
  5. Rewrites all path references in `package.json` to point to the new locations.
123
- 6. Updates the `files` array.
124
- 7. Cleans up any leftover export-map stub directories that only contain a `package.json`.
123
+ 6. **Adjusts sourcemap `sources` paths** (explicit directories only) - when `.map` files are moved, their `sources` entries are rewritten so they still resolve to the correct original files. This also handles cross-directory references (e.g. a `.d.ts.map` in `types/` pointing at files in `dist/`) and incorporates any `sourceRoot` into the individual source paths.
124
+ 7. Updates the `files` array.
125
+ 8. Cleans up any leftover export-map stub directories that only contain a `package.json`.
125
126
 
126
127
  ## Examples
127
128
 
@@ -193,9 +194,9 @@ await prunePkg(
193
194
 
194
195
  ### `prunePkg(pkg, options, logger)`
195
196
 
196
- - **`pkg`** A mutable `package.json` object. Modified in place.
197
- - **`options`** An options object matching the CLI flags.
198
- - **`logger`** A logger instance (from [`@niceties/logger`](https://www.npmjs.com/package/@niceties/logger)).
197
+ - **`pkg`** - A mutable `package.json` object. Modified in place.
198
+ - **`options`** - An options object matching the CLI flags.
199
+ - **`logger`** - A logger instance (from [`@niceties/logger`](https://www.npmjs.com/package/@niceties/logger)).
199
200
 
200
201
  ## Ignored Files
201
202
 
package/index.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":["prunePkg","Logger"],"sources":["prune.js"],"sourcesContent":[null],"mappings":";;;;iBAmEsBA,QAAQA;aA/BUC,MAAMA","ignoreList":[]}
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "pkgprn",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "author": "Konstantin Shutkin",
6
6
  "bin": "./index.js",
7
7
  "type": "module",
8
- "types": "./types/index.d.ts",
8
+ "types": "./index.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "types": "./types/index.d.ts",
11
+ "types": "./index.d.ts",
12
12
  "default": "./prune.js"
13
13
  },
14
14
  "./package.json": "./package.json"
package/prune.js CHANGED
@@ -356,16 +356,40 @@ async function flatten(pkg, flatten, logger) {
356
356
  const renamePromises = [];
357
357
  const newFiles = [];
358
358
 
359
+ /** @type {Map<string, string>} maps new path -> old path */
360
+ const movedFiles = new Map();
361
+
359
362
  for (const [, info] of distDirInfo) {
360
363
  for (const file of info.files) {
361
364
  const relativePath = path.relative(info.relativeDistDir, file);
362
365
  newFiles.push(relativePath);
366
+ movedFiles.set(relativePath, file);
363
367
  renamePromises.push(rename(file, relativePath));
364
368
  }
365
369
  }
366
370
 
367
371
  await Promise.all(renamePromises);
368
372
 
373
+ // adjust sourcemap paths for explicit flatten only
374
+ // (automatic flatten is safe because the common prefix is derived from package.json references)
375
+ if (typeof flatten === 'string') {
376
+ // build reverse map: normalized old path -> new path
377
+ // so we can fix sources that point to files which themselves moved
378
+ /** @type {Map<string, string>} */
379
+ const oldToNew = new Map();
380
+ for (const [newPath, oldPath] of movedFiles) {
381
+ oldToNew.set(path.normalize(oldPath), newPath);
382
+ }
383
+
384
+ const sourcemapFiles = newFiles.filter(f => f.endsWith('.map'));
385
+ for (const newMapPath of sourcemapFiles) {
386
+ const oldMapPath = movedFiles.get(newMapPath);
387
+ if (oldMapPath) {
388
+ await adjustSourcemapPaths(newMapPath, oldMapPath, oldToNew);
389
+ }
390
+ }
391
+ }
392
+
369
393
  // clean up empty source directories
370
394
  /** @type {string[]} */
371
395
  const cleanedDirs = [];
@@ -466,6 +490,51 @@ function cloneAndUpdate(pkg, updater) {
466
490
  return pkg;
467
491
  }
468
492
 
493
+ /**
494
+ * Adjusts the `sources` (and `sourceRoot`) in a v3 sourcemap file after it has been moved.
495
+ * Resolves each source against the old location, then makes it relative to the new location.
496
+ * If a source target was itself moved during flatten, the new location is used instead.
497
+ * @param {string} newMapPath - The new path of the .map file (relative to project root).
498
+ * @param {string} oldMapPath - The old path of the .map file (relative to project root).
499
+ * @param {Map<string, string>} oldToNew - Map from normalized old file paths to their new paths.
500
+ */
501
+ async function adjustSourcemapPaths(newMapPath, oldMapPath, oldToNew) {
502
+ const content = await readFile(newMapPath, 'utf8');
503
+
504
+ let map;
505
+ try {
506
+ map = JSON.parse(content);
507
+ } catch {
508
+ return; // not valid JSON, skip
509
+ }
510
+
511
+ if (map.version !== 3 || !Array.isArray(map.sources)) {
512
+ return;
513
+ }
514
+
515
+ const oldDir = path.dirname(oldMapPath) || '.';
516
+ const newDir = path.dirname(newMapPath) || '.';
517
+ const sourceRoot = map.sourceRoot || '';
518
+
519
+ map.sources = map.sources.map((/** @type {string} */ source) => {
520
+ // Resolve source against old map location (incorporating sourceRoot)
521
+ const resolved = path.normalize(path.join(oldDir, sourceRoot, source));
522
+ // If the resolved source was itself moved, use its new location
523
+ const effective = oldToNew.get(resolved) ?? resolved;
524
+ // Make relative to new map location
525
+ const newRelative = path.relative(newDir, effective);
526
+ // Sourcemaps always use forward slashes
527
+ return newRelative.split(path.sep).join('/');
528
+ });
529
+
530
+ // sourceRoot has been incorporated into the individual source paths
531
+ if (map.sourceRoot !== undefined) {
532
+ delete map.sourceRoot;
533
+ }
534
+
535
+ await writeFile(newMapPath, JSON.stringify(map), 'utf8');
536
+ }
537
+
469
538
  /**
470
539
  * @param {string} parent
471
540
  * @param {string} child
@@ -1,16 +0,0 @@
1
- {
2
- "version": 3,
3
- "file": "index.d.ts",
4
- "names": [
5
- "prunePkg",
6
- "Logger"
7
- ],
8
- "sources": [
9
- "../prune.js"
10
- ],
11
- "sourcesContent": [
12
- null
13
- ],
14
- "mappings": ";;;;iBAmEsBA,QAAQA;aA/BUC,MAAMA",
15
- "ignoreList": []
16
- }
File without changes