From 737885b951d374e6fad1b7f25a2b7daae0e22a83 Mon Sep 17 00:00:00 2001 From: Konstantin Shutkin Date: Sat, 19 Oct 2024 00:39:52 +0200 Subject: [PATCH] fix: improve remove sourcemaps --- .changeset/little-tomatoes-brake.md | 6 ++++++ pkgbld/src/prune.ts | 6 +++--- pkgbld/tests/tests.json | 9 +++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 .changeset/little-tomatoes-brake.md diff --git a/.changeset/little-tomatoes-brake.md b/.changeset/little-tomatoes-brake.md new file mode 100644 index 0000000..f2c0533 --- /dev/null +++ b/.changeset/little-tomatoes-brake.md @@ -0,0 +1,6 @@ +--- +"pkgbld": patch +"pkgbld-internal": patch +--- + +ignore node_modules when doing prune with `--remove-sourcemaps` diff --git a/pkgbld/src/prune.ts b/pkgbld/src/prune.ts index 838acdc..f920c58 100644 --- a/pkgbld/src/prune.ts +++ b/pkgbld/src/prune.ts @@ -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); @@ -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); diff --git a/pkgbld/tests/tests.json b/pkgbld/tests/tests.json index 5f0e0f0..2ccee8f 100644 --- a/pkgbld/tests/tests.json +++ b/pkgbld/tests/tests.json @@ -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": [