Skip to content

Instantly share code, notes, and snippets.

@kyo-ago
Created October 15, 2024 01:19
Show Gist options
  • Save kyo-ago/6abcc170c5d027c8eb36c8b56a844364 to your computer and use it in GitHub Desktop.
Save kyo-ago/6abcc170c5d027c8eb36c8b56a844364 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const { files } = require('.next/next-server.js.nft.json');
const { nodeFileTrace } = require('@vercel/nft');
const getAllFiles = async dirPath => {
let files = [];
const dirEntries = await fs.promises.readdir(dirPath, { withFileTypes: true });
for (const entry of dirEntries) {
const filePath = path.join(dirPath, entry.name);
if (entry.isDirectory()) {
files = files.concat(await getAllFiles(filePath));
} else {
files.push(filePath);
}
}
return files;
};
(async () => {
const { fileList } = await nodeFileTrace(['server.js', 'next.config.js'], {
base: basePath,
});
const filesToKeep = new Set(
files.map(file => path.resolve('.next', file))
.concat(Array.from(fileList).map((file) => path.resolve('.next', file)))
.concat(await getAllFiles(path.resolve(`${basePath}/node_modules/next/dist/compiled/`)))
);
const allFiles = await getAllFiles('node_modules');
await Promise.all(
allFiles
.filter(file => !filesToKeep.has(file))
.map(file => fs.promises.unlink(file))
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment