Created
March 25, 2022 16:55
-
-
Save cometkim/e81272c1ce290b75a2d5f634d831b852 to your computer and use it in GitHub Desktop.
Search all packages that doesn't support ESM from node_modules directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as path from 'node:path'; | |
import * as fs from 'node:fs/promises'; | |
const nodeModules = path.resolve('node_modules'); | |
let moduleNames = await fs.readdir(nodeModules); | |
moduleNames = (await Promise.all( | |
moduleNames.map(async name => { | |
if (name.startsWith('.')) { | |
return []; | |
} else if (name.startsWith('@')) { | |
const packages = await fs.readdir(path.join(nodeModules, name)); | |
return packages.map(pkg => `${name}/${pkg}`); | |
} else { | |
return name; | |
} | |
}), | |
)).flat(); | |
const cjsModuleNames = (await Promise.all( | |
moduleNames.map(async moduleName => { | |
const content = await fs.readFile(path.join(nodeModules, moduleName, 'package.json')); | |
const manifest = JSON.parse(content); | |
function anyEntry(exports) { | |
if (exports && typeof exports === 'object') { | |
if (exports['import']) { | |
return true; | |
} else { | |
for (const entry of Object.values(exports)) { | |
return anyEntry(entry); | |
} | |
} | |
} | |
return false; | |
} | |
const predicate = ( | |
manifest['main']?.endsWith('.mjs') || | |
manifest['type'] === 'module' || | |
anyEntry(manifest['exports']) | |
); | |
return predicate ? null : moduleName; | |
}), | |
)).filter(Boolean); | |
console.log(cjsModuleNames); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment