Skip to content

Instantly share code, notes, and snippets.

@ahmadawais
Created April 1, 2026 16:47
Show Gist options
  • Select an option

  • Save ahmadawais/42f7a24d784ee5b19ef84739d02f06c9 to your computer and use it in GitHub Desktop.

Select an option

Save ahmadawais/42f7a24d784ee5b19ef84739d02f06c9 to your computer and use it in GitHub Desktop.
remove-inline-sourcemaps.mjs
#!/usr/bin/env node
import { readdir, readFile, stat, writeFile } from 'node:fs/promises';
import path from 'node:path';
const rootDir = path.resolve(process.argv[2] ?? process.cwd());
const skipDirs = new Set(['.git', 'node_modules']);
const sourceMapLine = /^[ \t]*\/\/# sourceMappingURL=data:app[^\r\n]*(?:\r?\n)?/gm;
let filesChecked = 0;
let filesUpdated = 0;
async function walk(dir) {
const entries = await readdir(dir, {
withFileTypes: true
});
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (!skipDirs.has(entry.name)) {
await walk(fullPath);
}
continue;
}
if (!entry.isFile()) {
continue;
}
filesChecked += 1;
let content;
try {
content = await readFile(fullPath, 'utf8');
} catch {
continue;
}
if (!sourceMapLine.test(content)) {
sourceMapLine.lastIndex = 0;
continue;
}
sourceMapLine.lastIndex = 0;
const nextContent = content.replace(sourceMapLine, '');
if (nextContent !== content) {
await writeFile(fullPath, nextContent, 'utf8');
filesUpdated += 1;
console.log(path.relative(rootDir, fullPath));
}
}
}
const rootStats = await stat(rootDir);
if (!rootStats.isDirectory()) {
throw new Error(`Not a directory: ${rootDir}`);
}
await walk(rootDir);
console.log(`Checked ${filesChecked} files, updated ${filesUpdated}.`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment