Skip to content

Add support for .mjs#128

Open
amatiasq wants to merge 4 commits intodependents:mainfrom
amatiasq:patch-1
Open

Add support for .mjs#128
amatiasq wants to merge 4 commits intodependents:mainfrom
amatiasq:patch-1

Conversation

@amatiasq
Copy link
Copy Markdown

This enables proper dependency parsing for .mjs files.

@amatiasq
Copy link
Copy Markdown
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
Copy Markdown
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