Skip to content

Commit

Permalink
fix: improve remove sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
kshutkin committed Oct 18, 2024
1 parent 14842f8 commit 737885b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/little-tomatoes-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"pkgbld": patch
"pkgbld-internal": patch
---

ignore node_modules when doing prune with `--remove-sourcemaps`
6 changes: 3 additions & 3 deletions pkgbld/src/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function prunePkg(pkg: PackageJson, options: { kind: 'prune', profi
}

if (options.removeSourcemaps) {
const sourceMaps = await walkDir('.').then(files => files.filter(file => file.endsWith('.map')));
const sourceMaps = await walkDir('.', ['node_modules']).then(files => files.filter(file => file.endsWith('.map')));
for (const sourceMap of sourceMaps) {
// find corresponding file
const sourceFile = sourceMap.slice(0, -4);
Expand Down Expand Up @@ -339,12 +339,12 @@ async function isDirectory(file: string) {
return fileStat.isDirectory();
}

async function walkDir(dir: string) {
async function walkDir(dir: string, ignoreDirs: string[] = []) {
const entries = await readdir(dir, { withFileTypes: true });
const files = [] as string[];
await Promise.all(entries.map(entry => {
const childPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.isDirectory() && !ignoreDirs.includes(entry.name)) {
return walkDir(childPath)
.then(childFiles => {
files.push(...childFiles);
Expand Down
9 changes: 9 additions & 0 deletions pkgbld/tests/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@
"output": "dist\n index.cjs\n|\"use strict\";\n|//! Copyright (c) 2024 Konstantin Shutkin MIT License\n|exports.test=0;\n|\n index.d.ts\n|export declare const test = 0;\n|\n index.mjs\n|//! Copyright (c) 2024 Konstantin Shutkin MIT License\n|const o=0;export{o as test};\n|\npackage.json\n|{\n| \"exports\": {\n| \".\": {\n| \"types\": \"./dist/index.d.ts\",\n| \"import\": \"./dist/index.mjs\",\n| \"require\": \"./dist/index.cjs\",\n| \"default\": \"./dist/index.cjs\"\n| },\n| \"./package.json\": \"./package.json\"\n| },\n| \"files\": [\n| \"dist\"\n| ],\n| \"scripts\": {\n| \"prepack\": \"pkgbld prune\"\n| },\n| \"types\": \"./dist/index.d.ts\",\n| \"main\": \"./dist/index.cjs\",\n| \"module\": \"./dist/index.mjs\",\n| \"typesVersions\": {\n| \"*\": {\n| \".\": [\n| \"dist/index.d.ts\"\n| ],\n| \"*\": [\n| \"dist/index.d.ts\",\n| \"dist/*\"\n| ]\n| }\n| }\n|}\n|\nsrc\n index.ts\n|//! Copyright (c) 2024 Konstantin Shutkin MIT License\n|export const test = 0;\ntsconfig.json\n|{\n| \"include\": [\n| \"src\",\n| \"types\"\n| ],\n| \"compilerOptions\": {\n| \"lib\": [\n| \"dom\",\n| \"esnext\"\n| ],\n| \"target\": \"esnext\",\n| \"module\": \"esnext\",\n| \"esModuleInterop\": true,\n| \"allowJs\": true,\n| \"skipLibCheck\": true,\n| \"strict\": true,\n| \"sourceMap\": true,\n| \"noUncheckedIndexedAccess\": true,\n| \"declaration\": true,\n| \"moduleResolution\": \"node\"\n| }\n|}\n|",
"stdout": "preparing..\n\npreparing...\nno tsconfig.json or jsconfig.json and --no-ts-config not specified, writing tsconfig...\ndone\nsrc → dist 0 / 1\n✓ index [es, cjs]\nsrc → dist 1 / 1\n✓ src → dist 1 / 1 in XXX\n",
"stderr": ""
},
{
"id": 32,
"name": "remove sourcemaps should ignore node_modules",
"args": "prune --removeSourcemaps",
"input": "dist\n index.js\n|// something\n|\n|console.log('Hello, world!');\n|\n|\nnode_modules\n test.map\n|asd\npackage.json\n|{\n| \"name\": \"mylib\",\n| \"main\": \"./dist/index.js\"\n|}\n|",
"output": "dist\n index.js\n|// something\n|\n|console.log('Hello, world!');\n|\n|\npackage.json\n|{\n| \"name\": \"mylib\",\n| \"main\": \"./dist/index.js\"\n|}\n|",
"stdout": "preparing..\n\n",
"stderr": ""
}
],
"capture": [
Expand Down

0 comments on commit 737885b

Please sign in to comment.