Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .mjs #128

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Add support for .mjs #128

wants to merge 4 commits into from

Conversation

amatiasq
Copy link

This enables proper dependency parsing for .mjs files.

@amatiasq
Copy link
Author

If someone has this issue with madge, I've come up with this snippet to get a hacked version with this change.

Nasty but works.

// This hack is required because filing-cabinet
// (a madge dependency) doesn't support .mjs files
// https://github.com/dependents/node-filing-cabinet/pull/128

import { readFile, writeFile } from 'fs/promises';

async function getHackedMadge() {
	const file = await import.meta.resolve('filing-cabinet/index.js');
	const content = await readFile(file, 'utf8');

	if (!content.includes('.mjs')) {
		const hacked = content.replace(
			"'.jsx': jsLookup,",
			(x) => `${x}\n  '.mjs': jsLookup,`
		);

		await writeFile(file, hacked, 'utf8');
	}

	const module = await import('madge');
	const madge = module.default ?? module.madge;

	// @ts-ignore This is memoization
	getHackedMadge = () => madge;

	return madge;
}

// anywhere else
const madge = await getHackedMadge();
// following invocations are memoized
const madge2 = await getHackedMadge();
console.assert(madge === madge2)

@XhmikosR
Copy link
Member

XhmikosR commented Apr 5, 2024

By the same logic, shouldn't .cjs be supported too?

Note that this needs a test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants